JSON Mode and Tool-Call Outputs
Use response_format={'type':'json_object'} or a single tool call to force machine-parseable output.
The Need for Structure
Free-form text from LLMs is hostile to code. Production agents need parseable output: JSON, XML, function arguments — never "the answer is..."
JSON Mode (OpenAI)
Tell the model "always return JSON":
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role': 'system', 'content': 'Return a JSON object with name and age.'},
{'role': 'user', 'content': 'Alice, 30 years old.'}
],
response_format={'type': 'json_object'}
)
import json
data = json.loads(response.choices[0].message.content)All lessons in this course
- JSON Mode and Tool-Call Outputs
- Pydantic Schema Validation
- Repair Loops for Malformed Output
- Instructor / Outlines for Guaranteed Structure