Skip to content

LLM Structured Output

Natural language generated by LLMs is excellent for humans but inconvenient for programs to process. For an agent to call tools, it must output which tool to call along with the required arguments in a structured format. The structured output feature enables the LLM to generate responses in a well-defined JSON format.

You can define the desired format using the Python Pydantic library. It is used for data validation and for defining data structures and classes. You create a schema with field names, and the class must inherit from the Pydantic BaseModel class. Pydantic then automatically validates any data against this schema, raising clear errors if the data does not match. This helps enforce data validation and catch malformed inputs.

Install Pydantic Package

uv add pydantic
uv add 'pydantic[email]'

Define schema and fields

1
2
3
4
5
6
from pydantic import BaseModel, EmailStr

class PersonalInfo(BaseModel):
    name: str
    email: Emailstr
    phone: str | None = None

Invoke the LLM to get the structured output

response = completion(
    model="gpt-5-mini",
    messages=[{
        "role": "user", 
        "content": "My name is James, my email is james@example.com, and my phone is 555-123-1234."
    }],
    response_format=PersonalInfo
)

result = response.choices[0].message.content