Skip to main content
Get from zero to your first validated JSON object in about five minutes.

1. Install

pip install openai pydantic

2. Set your API key

export DOTTXT_API_KEY="your-api-key"

3. Run your first extraction

import os
from openai import OpenAI
from pydantic import BaseModel, Field

class Contact(BaseModel):
    name: str = Field(min_length=1)
    email: str = Field(pattern=r"^[^@]+@[^@]+$")
    role: str | None = None

client = OpenAI(
    base_url="https://api.dottxt.ai/v1",
    api_key=os.environ["DOTTXT_API_KEY"],
)

response = client.chat.completions.create(
    model="openai/gpt-oss-20b",
    messages=[
        {"role": "user", "content": "Extract: John Smith <john@acme.com>, VP Engineering"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "contact",
            "strict": True,
            "schema": Contact.model_json_schema(),
        },
    },
)

result = Contact.model_validate_json(response.choices[0].message.content)

print(result.model_dump())
Expected output:
{
  "name": "John Smith",
  "email": "john@acme.com",
  "role": "VP Engineering"
}

4. Next steps