Skip to content
mentoor

Topic 2 of 5 · 4 min read

Strict schemas versus JSON mode versus forced tools

Goal: Choose between strict structured outputs, JSON mode and a forced tool call for an extraction task and justify the choice.

Pick the mechanism by what it guarantees: strict structured outputs guarantee the schema, JSON mode only guarantees that the text parses, and a forced tool call guarantees that the model calls a function you named. Providers differ in names and details, so read the docs for the one you use.

Check yourself · 1 of 3

A model is set to JSON mode and returns {"category": "billing"} when your code also needs priority. What does this show?

Choose one answer

Why it matters

Teams often turn on "JSON mode", see valid JSON in testing and ship. Then a field is missing in production, because valid JSON is not the same as the right JSON. Knowing the guarantee tells you how much checking you still owe.

The three mechanisms

  • Strict structured outputs (OpenAI structured outputs, Claude structured outputs, Gemini response schema). You pass a JSON Schema and the provider constrains generation to it. Types, required fields and enum values come back as specified. This is the default choice for extraction and classification.
  • JSON mode. The reply is valid JSON, but the shape is up to the model. You describe the fields in the prompt and hope. Use it when the provider offers nothing stricter, and validate everything.
  • Forced tool call. You define a function with an input schema and require the model to call it. The arguments are your structured data. This is the right fit when the output is really an action to run, or when you want the answer to be one of several tools, such as create_ticket or ask_user.

How to choose

  1. Is the output data for your code to read? Use a strict schema if the model supports it.
  2. Is the output a choice between actions? Use tools, and force the call when you need a result every time.
  3. Does the model offer neither? Use JSON mode, put the schema in the prompt, and add validation and retries (the next topics).

Streaming and function-calling support differ by model, so check the model you will actually run.

Before and after

# Weak: JSON mode, shape described in prose
resp = client.chat.completions.create(
    model=MODEL,
    response_format={"type": "json_object"},
    messages=[{"role": "user", "content": "Return JSON with category and priority for: " + text}],
)

This returns parseable JSON, but priority may be "high", 3 or missing. The fixed version passes a schema with response_format set to a strict JSON Schema, so priority can only be one of the listed values.

What goes wrong

  • Treating JSON mode as a schema. It guards syntax, not shape.
  • Forcing a tool when no action exists. The tool then becomes a schema in disguise, with extra tokens and a confusing name.
  • Assuming the guarantee covers meaning. Even strict output can hold a wrong or invented value. Validate it (see the next topic).
  • Ignoring refusals. A model may decline a request, and then the reply does not match the schema. Check for a refusal before you parse.

Examples

Comparison for a support-ticket extractor

Mechanism Valid JSON Right fields and types Notes
JSON mode yes no Shape comes from the prompt only
Strict schema yes yes Best for extraction
Forced tool yes yes (tool input schema) Best when the result triggers an action

Forced tool in Claude

resp = client.messages.create(
    model=MODEL,
    max_tokens=500,
    tools=[ticket_tool],
    tool_choice={"type": "tool", "name": "create_ticket"},
    messages=[{"role": "user", "content": text}],
)
args = next(b.input for b in resp.content if b.type == "tool_use")

The input of the tool block is the structured data.

Key points

  • Strict structured outputs guarantee the schema; JSON mode guarantees only that the text is valid JSON.
  • A forced tool call fits actions and choices between actions; the arguments are your data.
  • Choose strict schemas by default for extraction and classification when the model supports them.
  • With JSON mode, describe the fields in the prompt and validate everything yourself.
  • No mechanism guarantees the content is true, and a refusal can break the shape.

Common mistakes

  • Shipping JSON mode and assuming the fields will always be present.
  • Forcing a tool call when nothing needs to be run, just to get JSON.
  • Parsing the reply without checking for a refusal or a truncated stop reason.
  • Believing that a strict schema also makes the values correct.

Check yourself · 2 of 3

You are building a bot that either creates a ticket or asks the user a follow-up question, depending on the message. Which mechanism fits best?

Choose one answer

Check yourself · 3 of 3

Your provider supports a strict JSON Schema mode for your model. For a pure extraction task with no action to run, what should you use?

Choose one answer

Practice items, not counted towards your mastery.

Start here