How to intermediate · 3 min read

Fix extraction missing optional fields

Quick answer
To fix missing optional fields in AI extraction, ensure your response model defines optional fields correctly using pydantic with Optional types and default values. When calling client.chat.completions.create with response_model, the SDK will populate optional fields if present, avoiding errors if they are missing.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • pip install pydantic

Setup

Install the required packages and set your environment variable for the OpenAI API key.

  • Install OpenAI SDK v1+ and Pydantic for structured extraction:
bash
pip install openai pydantic
output
Requirement already satisfied: openai in ...
Requirement already satisfied: pydantic in ...

Step by step

Define a Pydantic model with optional fields using typing.Optional and default None. Use response_model in client.chat.completions.create to extract structured data safely, including optional fields.

python
import os
from typing import Optional
from pydantic import BaseModel
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

class User(BaseModel):
    name: str
    age: Optional[int] = None  # Optional field with default None
    email: Optional[str] = None  # Another optional field

messages = [
    {"role": "user", "content": "Extract: John is 30 years old."}
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    response_model=User
)

user = response
print(f"Name: {user.name}")
print(f"Age: {user.age}")
print(f"Email: {user.email}")
output
Name: John
Age: 30
Email: None

Common variations

You can use asynchronous calls with async and await if your environment supports it. Also, different models like gpt-4o or claude-3-5-sonnet-20241022 support structured extraction similarly.

python
import asyncio
import os
from typing import Optional
from pydantic import BaseModel
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

class User(BaseModel):
    name: str
    age: Optional[int] = None
    email: Optional[str] = None

async def main():
    messages = [{"role": "user", "content": "Extract: Alice."}]
    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=messages,
        response_model=User
    )
    user = response
    print(f"Name: {user.name}")
    print(f"Age: {user.age}")
    print(f"Email: {user.email}")

asyncio.run(main())
output
Name: Alice
Age: None
Email: None

Troubleshooting

  • If optional fields are missing in the response, ensure your prompt encourages the model to include them or handle None defaults gracefully.
  • Validate your Pydantic model matches the expected response structure.
  • Check for typos in field names and use Optional with default None to avoid validation errors.

Key Takeaways

  • Use Optional fields with default None in Pydantic models to handle missing extraction fields.
  • Pass response_model to client.chat.completions.create for safe structured extraction.
  • Encourage the model via prompt design to include optional fields when needed.
  • Validate and test your extraction model to avoid runtime errors from missing fields.
Verified 2026-04 · gpt-4o-mini, gpt-4o, claude-3-5-sonnet-20241022
Verify ↗