The chat completions API supports structured response formats through the response_format parameter, enabling you to receive consistent, predictable outputs in JSON format. This guide covers both basic JSON mode and advanced JSON schema validation with Pydantic integration.
from openai import OpenAIclient = OpenAI( api_key="your_truefoundry_api_key", base_url="<truefoundry-base-url>/api/llm/api/inference/openai")response = client.chat.completions.create( model="openai-main/gpt-4o", messages=[ {"role": "system", "content": "You are a helpful assistant designed to output JSON."}, {"role": "user", "content": "Extract information about the 2020 World Series winner"} ], response_format={"type": "json_object"})print(response.choices[0].message.content)
Output:
Copy
Ask AI
{ "team": "Los Angeles Dodgers", "year": 2020, "opponent": "Tampa Bay Rays", "games_played": 6, "series_result": "4-2"}
When using OpenAI’s response_format with JSON schema and strict mode is set to true, all properties defined in the schema must be included in the required array. If any property is defined but not marked as required, the API will return a 400 Bad Request Error”.
from openai import OpenAIfrom pydantic import BaseModel, Fieldfrom typing import Listclient = OpenAI( api_key="your_truefoundry_api_key", base_url="<truefoundry-base-url>/api/llm/api/inference/openai")# Define Pydantic modelclass UserInfo(BaseModel): name: str = Field(description="Full name of the user") age: int = Field(ge=0, description="Age in years") occupation: str = Field(description="Job title or profession") location: str = Field(description="City or location") skills: List[str] = Field(description="List of professional skills") class Config: extra = "forbid" # Prevent additional fieldsresponse = client.chat.completions.create( model="openai-main/gpt-4o", messages=[ { "role": "system", "content": "Extract user information and respond according to the provided schema." }, { "role": "user", "content": "Hi, I'm Mike Chen, a 32-year-old software architect from Seattle. I specialize in cloud computing, microservices, and Kubernetes." } ], response_format={ "type": "json_schema", "json_schema": { "name": "user_info", "schema": UserInfo.model_json_schema(), "strict": True } })# Parse and validate with Pydanticraw_content = response.choices[0].message.contentuser_data = UserInfo.model_validate_json(raw_content)print(f"Name: {user_data.name}")print(f"Age: {user_data.age}")print(f"Skills: {', '.join(user_data.skills)}")
When using OpenAI models with Pydantic Models, there should not be any optional fields in the pydantic model. This limitation exists because the corresponding JSON schema will then have missing field in the “required” section and this is NOT allowed when strict mode is true
For models with optional fields, consider using the beta parse client detailed in the following section.
The beta parse client provides the most streamlined approach for Pydantic integration, automatically handling schema conversion and parsing.
Copy
Ask AI
from openai import OpenAIfrom pydantic import BaseModel, Fieldfrom typing import List, Optionalclass UserInfo(BaseModel): name: str = Field(description="Full name of the user") age: int = Field(ge=0, description="Age in years") occupation: str = Field(description="Job title or profession") location: Optional[str] = Field(None, description="City or location") skills: List[str] = Field(default=[], description="List of professional skills")client = OpenAI( api_key="your_truefoundry_api_key", base_url="<truefoundry-base-url>/api/llm/api/inference/openai")completion = client.beta.chat.completions.parse( model="openai-main/gpt-4o", messages=[ { "role": "system", "content": "Extract user information from the provided text." }, { "role": "user", "content": "Hello, I'm Alex Rodriguez, a 29-year-old product manager from Austin. I have experience in agile methodologies, data analysis, and team leadership." } ], response_format=UserInfo,)user_result = completion.choices[0].message.parsedprint(f"Name: {user_result.name}")print(f"Age: {user_result.age}")print(f"Occupation: {user_result.occupation}")print(f"Location: {user_result.location}")print(f"Skills: {', '.join(user_result.skills)}")