AI / Core OpenAI Codex Application Fundamentals Interview Questions
What are structured outputs in the OpenAI API and how do you use them?
Structured outputs guarantee that a model's response strictly conforms to a developer-defined JSON schema. This eliminates the need for output parsing heuristics and makes AI outputs reliably machine-readable.
from openai import OpenAI from pydantic import BaseModel client = OpenAI() # Method 1: Pydantic model (Python SDK - simplest approach) class CodeReview(BaseModel): issues: list[str] severity: str # "low" | "medium" | "high" suggested_fix: str confidence_score: float # Responses API with structured output response = client.responses.create( model="gpt-5.5", input="Review this Python function for bugs: def add(a, b): return a - b", text={ "format": { "type": "json_schema", "json_schema": { "name": "code_review", "schema": CodeReview.model_json_schema(), "strict": True } } } ) review = CodeReview.model_validate_json(response.output_text) print(f"Severity: {review.severity}") print(f"Issues: {review.issues}") # Chat Completions with parse() helper (beta): completion = client.beta.chat.completions.parse( model="gpt-5.5", messages=[{"role": "user", "content": "Extract: Alice is 30, is an engineer."}], response_format=CodeReview, ) result = completion.choices[0].message.parsed
Key differences between APIs: in the Responses API, use text.format with type: json_schema. In Chat Completions, use response_format with type: json_schema. Both support strict: true which enforces the schema constraint at the grammar level, eliminating the possibility of schema violations.
More Related questions...