0Pricing
AI Agents · Lesson

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

  1. JSON Mode and Tool-Call Outputs
  2. Pydantic Schema Validation
  3. Repair Loops for Malformed Output
  4. Instructor / Outlines for Guaranteed Structure
← Back to AI Agents