Learn & Build Agents

    ↑ ↓ to choose · Enter to open · Esc to close

    01 FoundationsLesson 6 of 69 minBeginnerHelps to have seen:Why the answer varies

    Structured output (JSON)

    Can the code read it?almost always
    Is the content right?—
    • + instruction asking for JSON
    • + router in code
    Customerin the store chatThe text of the calland the model's outputinstructionAnswer only in JSON: intencao and numero_pedido.customerHi, where is my order 4805?output{"intencao":"rastreio","numero_pedido":"4805"}▍LLMextracts intent and no.Routercode that reads the JSONread
    LLMcodedataperson

    Step 1 of 8: The code expected JSON and got a sentence

    You will learn
    • Why free text breaks the code
    • What a schema is, and why closed values help
    • What structured output guarantees
    • What it does not guarantee, and how the schema can force a made-up value
    • Where the content gets checked
    In the lab

    Messages with the right intent and order number, out of the store's 50 test conversations

    Coming soon: I will measure this on the example agent and publish the number with the report.

    The whole lesson as textEvery step, to read at once, search or print.
    1. The code expected JSON and got a sentence

      The store router is code. It needs two pieces of data to pick the path: the intent and the order number. The instruction tells the model "answer only in JSON".

      It almost always works: Shopify measured 0,25% unreadable answers with no structure at all. But 0,25% of a lot of messages is a lot of customers, and all it takes is a friendly sentence before the JSON.

      Asking for a format in the prompt is a request, not a guarantee.

    2. Agree on the format first: the schema

      A describes the format of the answer: which fields, of what type and with which values.

      Here, intencao can only be rastreio (tracking), troca (exchange), reembolso (refund) or outro (other). With a closed list, the code does not need to guess synonyms: each value has a path.

      Closed values for the intent remove the ambiguity from the path.

    3. Structured output forces the format

      In the Claude API, the schema goes in the request, and the model can only generate pieces that fit it: this is constrained decoding.

      Anthropic says this guarantees the schema in most cases. The exceptions: when the model refuses, when the answer is cut off by the size limit, and letter case in a closed list. The code handles those separately.

      With structured output, the format stops being a request and becomes a rule.

    4. Required field, made-up number

      The customer writes "I want my money back", with no number. But the schema says numero_pedido is required and is text.

      The model has to put something there, and the JSON comes out flawless with a number nobody wrote. Structured output guarantees the form, not the content. And here it was the schema itself that pushed toward making it up.

      If the data may be missing, the schema must let it say "I do not know".

    5. Perfect JSON, an order that does not exist

      Another case from the test set: "order 4999 never arrived, I want R$ 180". The model correctly extracts what it read: reembolso, 4999.

      But order 4999 does not exist. The JSON is right and the number was copied correctly, but the fact is wrong. No schema knows which orders exist.

      Right format does not mean right fact.

    6. Check the content in code

      After the JSON, the code checks what can be checked: does the order exist? Does it belong to this customer? Does the intent make sense with the status?

      If it does not match, it does not go on: it asks the customer to check the number, or hands off to a person. The schema takes care of the form, the code takes care of the facts.

      The schema takes care of the form; the code takes care of the facts.

    7. The same contract applies to tools

      What the source says: Anthropic describes a tool as a contract between deterministic systems and non-deterministic agents.

      Our reading: structured output is the same contract seen from the other side. The model returns the agreed format, and the code does not trust the content without checking. In the next lesson, the model starts calling tools this way.

      Between the model and the code, always a written contract.

    8. Form in the schema, facts in the code

      When the model's output goes to code, agree on the format in a schema and use the API's structured output: the format stops being a request.

      Let the schema say "I do not know" when the data may be missing, and check in code what is fact. The question that remains: what about when the model itself decides to call the tool?

    Questions in this lesson

    Why is "answer only in JSON" in the prompt not enough?
    Because it is a request: sometimes text comes along and the code breaks. Without structured output, the format depends on the model obeying.
    The message has no order number, and the schema requires one. What tends to happen?
    The model fills in some number, in valid JSON. The field is required, so some value comes out. Let the field accept null.
    The JSON came with numero_pedido "4999", which does not exist. Who catches it?
    A check in code against the orders. Facts get checked in code. The schema only looks at the form.