0
Phase 6

JSON Mode

~2 min read

Concept & How It Works

    Why Does It Exist?

    Agents pipeline LLM output into APIs, databases, and UI components. Invalid JSON breaks the chain; JSON mode prevents that class of failure.

    Real-World Analogy

    JSON mode is a fill-in-the-blank form — the model must use the right format, not write a free-form essay you have to parse.
    Loading diagram...

    Visual Workflows

    What is JSON Mode?

    Loading diagram...

    Example

    Scenario

    An extraction agent returns {"entities": [{"name": "Acme", "type": "company"}], "sentiment": "positive"} — parsed directly into a database insert.

    Solution

    In Tool Calling & Function Calling, apply JSON Mode to this scenario: An extraction agent returns {"entities": [{"name": "Acme", "type": "company"}], "sentiment": "positive"} — parsed directly into a database insert. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

    Practice Task

    Do this before moving to the next module — reading alone is not enough.

    Open the Code Walkthrough below and run it locally. Change one parameter related to JSON Mode (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

    Code Walkthrough

    Highlighted lines show where JSON Mode happens in the code.

    JSON Mode
    1response = client.chat.completions.create(  # call the API2    model="gpt-4o-mini",  # key line for JSON Mode3    messages=[{"role": "user", "content": "Extract entities as JSON."}],  # key line for JSON Mode4    response_format={"type": "json_object"},  # key line for JSON Mode5)6data = json.loads(response.choices[0].message.content)  # key line for JSON Mode

    Commands to Remember

    Commands to Remember

    • client.chat.completions.create(..., tools=[...]) # pass tool schemas to API
    • json.loads(response.choices[0].message.tool_calls[0].function.arguments) # parse tool args
    • pip install pydantic # validate tool inputs with schemas

    Common Mistakes

    • Treating JSON Mode as a black box without evaluation
    • Ignoring cost and latency in production
    • Skipping error handling for json mode

    Cheat Sheet

    Quick recap — the most important points from this module.

    Cheat Sheet

    quick ref
    • JSON Mode
    • Structured Output
    • JSON Schema
    • response_format