Skip to content

Quickstart

Get from zero to your first extraction in 5 minutes.

Install

pip install dspydantic

Set your OpenAI API key:

export OPENAI_API_KEY="sk-..."

Configure a Language Model

DSPydantic uses DSPy under the hood. Before anything else, configure the language model:

import dspy

lm = dspy.LM("openai/gpt-4o-mini", api_key="your-api-key")
dspy.configure(lm=lm)

(The API key can also come from the OPENAI_API_KEY environment variable.)

Define What to Extract

Create a Pydantic model describing the data:

from pydantic import BaseModel, Field

class Person(BaseModel):
    name: str = Field(description="Person's full name")
    age: int = Field(description="Person's age")
    email: str = Field(description="Email address")

Run Extraction

from dspydantic import Prompter

prompter = Prompter(model=Person, model_id="openai/gpt-4o-mini")

result = prompter.run("""
    John Smith
    Age: 28
    Contact: john.smith@example.com
""")

print(result)
# Person(name='John Smith', age=28, email='john.smith@example.com')

Done! You've extracted structured data from text.

Next Steps