Skip to content
AgenticAIWiki
  1. Learn
  2. Part 2
  3. 14. Structured output
  4. 14.6

Lesson 14.6 · 6 of 8 · 5 min read · Reviewed 24 Nov 2026

Forcing structure with tool_use

By the end, you can

  • Use tool_use to get schema-shaped output.
  • Force a specific tool with tool_choice where it is supported, and know when native structured outputs replace it.

Where your provider offers native structured outputsStructured outputs: Making a model return data that matches a defined schema, such as JSON with required fields and types. or a strict mode (14.2), use that first. This lesson covers the older pattern of forcing a tool call to get JSON. You will meet it in existing code, on providers without a strict mode, and when the “output” really is a tool call.

Tool inputs are already schema-shaped JSON. So you can define a “tool” that’s really your output format.

{
  "name": "record_ticket",
  "description": "Record the extracted ticket fields.",
  "input_schema": { ...your schema from 18.1... }
}

Then force it:

"tool_choice": {"type": "tool", "name": "record_ticket"}

Claude replies with a tool_use block whose input matches the schema. You don’t run anything. You read input and you’re done.

Why this works well

  • The output is parsed JSON, not text to scrape.
  • The schema’s descriptions guide each field.
  • It works for extraction, classification and any task with a fixed shape.

On the newest models

Forced tool choice is rejected by the newest Claude models (33.4). There, get the same result with structured outputs (14.1), or offer only record_ticket, leave tool_choice on auto, and instruct Claude to call it. Mark the tool strict: true.

Strict mode

Where supported, marking a tool definitionTool definition: The name, description and input schema that tell the model what a tool does and how to call it. as strict makes the API enforce the schema exactly. Without it, the model is very good at following the schema but you should still validate.

On other providers

OpenAI offers structured outputs through a JSON schema response format with strict mode, and strict: true on function definitions. Gemini takes a response schema with a JSON response type. Both enforce the schema with constrained decoding (14.2), so tool forcing is rarely needed just to get JSON. Schema subsets differ, so test your schema on each provider you support.

Key points

  • Defining a tool whose input_schema is your output shape makes Claude return arguments in that shape.
  • Where forced tool choice is supported, tool_choice set to that tool makes the reply always structured. The newest Claude models reject forced tool choice; use structured outputs (14.2) there.
  • You never run the 'tool'; you just read its input as the result.
Hands-on labRuns in the Playground

Read structured output from a tool call

Pull the schema-shaped result out of a tool_use block instead of scraping text, and handle a reply with no tool call.

You will have: A function that returns the input of the named tool call, tested on three simulated API responses.

Steps

  1. Read the record_ticket tool definition and the three simulated responses (content blocks as dicts).
  2. Complete tool_input so it finds the tool_use block with the given name and returns its input.
  3. Return None when no matching block exists, for example when the model replied in text only.
  4. Check the required keys of the result against the tool's input_schema.

Done when

  • Response 1 returns a dict with all required keys.
  • Response 2, which has a text block before the tool call, still returns the dict.
  • Response 3, text only, prints 'no tool call: retry or review'.

Open in the Playground No API key needed.

Starter code (Python)
TOOL = {
    "name": "record_ticket",
    "description": "Record the extracted ticket fields.",
    "input_schema": {
        "type": "object",
        "properties": {"customer_name": {"type": "string"}, "priority": {"type": "string", "enum": ["low", "medium", "high"]}},
        "required": ["customer_name", "priority"],
    },
}

RESPONSES = [
    {"stop_reason": "tool_use", "content": [{"type": "tool_use", "id": "tu_1", "name": "record_ticket", "input": {"customer_name": "Priya", "priority": "high"}}]},
    {"stop_reason": "tool_use", "content": [{"type": "text", "text": "Recording it now."}, {"type": "tool_use", "id": "tu_2", "name": "record_ticket", "input": {"customer_name": "Tom", "priority": "low"}}]},
    {"stop_reason": "end_turn", "content": [{"type": "text", "text": "The customer is Ana and it seems urgent."}]},
]

def tool_input(response, name):
    # TODO: return block["input"] for the first tool_use block whose name matches, else None.
    return response["content"][0].get("input")

for i, r in enumerate(RESPONSES, 1):
    data = tool_input(r, TOOL["name"])
    if data is None:
        print(f"response {i}: no tool call: retry or review")
        continue
    missing = [k for k in TOOL["input_schema"]["required"] if k not in data]
    print(f"response {i}:", data, "missing:", missing)
Show a solution
TOOL = {
    "name": "record_ticket",
    "description": "Record the extracted ticket fields.",
    "input_schema": {
        "type": "object",
        "properties": {"customer_name": {"type": "string"}, "priority": {"type": "string", "enum": ["low", "medium", "high"]}},
        "required": ["customer_name", "priority"],
    },
}

RESPONSES = [
    {"stop_reason": "tool_use", "content": [{"type": "tool_use", "id": "tu_1", "name": "record_ticket", "input": {"customer_name": "Priya", "priority": "high"}}]},
    {"stop_reason": "tool_use", "content": [{"type": "text", "text": "Recording it now."}, {"type": "tool_use", "id": "tu_2", "name": "record_ticket", "input": {"customer_name": "Tom", "priority": "low"}}]},
    {"stop_reason": "end_turn", "content": [{"type": "text", "text": "The customer is Ana and it seems urgent."}]},
]

def tool_input(response, name):
    for block in response["content"]:
        if block.get("type") == "tool_use" and block.get("name") == name:
            return block["input"]
    return None

for i, r in enumerate(RESPONSES, 1):
    data = tool_input(r, TOOL["name"])
    if data is None:
        print(f"response {i}: no tool call: retry or review")
        continue
    missing = [k for k in TOOL["input_schema"]["required"] if k not in data]
    print(f"response {i}:", data, "missing:", missing)
Quick checkYou define a record_ticket tool with your schema as input_schema. On a model that supports forced tool choice, how do you make it always use that tool?
How sure are you?
Pick an answer and how sure you are