{ "model": "gpt-5.4", "input": "Tell me a three sentence bedtime story about a unicorn.", ... "text": { "format": { "type": "json_schema", "name": "..." "strict": true, "schema": ... } }}
Pydantic Example
from openai import OpenAIfrom pydantic import BaseModelclient = OpenAI()class CalendarEvent(BaseModel): name: str date: str participants: list[str]response = client.responses.parse( model="gpt-5.5", input=[ {"role": "system", "content": "Extract the event information."}, { "role": "user", "content": "Alice and Bob are going to a science fair on Friday.", }, ], text_format=CalendarEvent,)event = response.output_parsed
from langchain_ollama import ChatOllamafrom langchain_core.output_parsers import PydanticOutputParserfrom pydantic import BaseModel, Fieldclass Cricketer(BaseModel) : name: str = Field(description="Name of Cricketer") records: list = Field(description="Python list of records")parser = PydanticOutputParser(pydantic_object=Cricketer)# This prepares the instructions for json schema# We inject these instructions at the end of our messageprint(parser.get_format_instructions())model = ChatOllama( model="qwen2.5:7b", temperature=0)messages = [ ( "user", f"Tell me about a cricketer\n{parser.get_format_instructions()}", )]response = model.invoke(messages)# We should be able to parse the response into pydantic objectcricketer: Cricketer = parser.parse(response.content)print(cricketer)