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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
