---
source: https://iinventedlight.com/library/notes/ao-structured-claude-json-via-tools.md
title: "Structured JSON from Claude with a forced tool"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (anthropics/claude-cookbooks (GitHub)); MIT (openai/openai-cookbook (GitHub)); MIT (anthropics/claude-cookbooks (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: anthropics/claude-cookbooks (GitHub), Extracting structured JSON using tools, MIT (https://raw.githubusercontent.com/anthropics/claude-cookbooks/main/LICENSE). Copyright (c) 2023 Anthropic; openai/openai-cookbook (GitHub), How to call functions with chat models, MIT (https://raw.githubusercontent.com/openai/openai-cookbook/main/LICENSE). Copyright (c) 2025 OpenAI; anthropics/claude-cookbooks (GitHub), How to enable JSON mode, MIT (https://raw.githubusercontent.com/anthropics/claude-cookbooks/main/LICENSE). Copyright (c) 2023 Anthropic."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: agent-operations
topic_area: "agent operations: structured output"
tags: [anthropic, claude, structured-output, tool-use, json]
client: Librarian
sources:
  - id: S1
    title: "Extracting structured JSON using tools"
    publisher: anthropics/claude-cookbooks (GitHub)
    url: https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/extracting_structured_json.ipynb
    accessed: 2026-11-16
    license: "MIT"
  - id: S2
    title: "How to call functions with chat models"
    publisher: openai/openai-cookbook (GitHub)
    url: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb
    accessed: 2026-11-16
    license: "MIT"
  - id: S3
    title: "How to enable JSON mode"
    publisher: anthropics/claude-cookbooks (GitHub)
    url: https://github.com/anthropics/claude-cookbooks/blob/main/misc/how_to_enable_json_mode.ipynb
    accessed: 2026-11-16
    license: "MIT"
---
# Structured JSON from Claude with a forced tool
**Answer:** The Claude cookbook uses the tool use feature to extract structured JSON: define custom tools that prompt Claude to generate well-structured JSON output, and force one with `tool_choice={"type": "tool", "name": ...}`. The JSON is the tool `input`.

The same trick works on OpenAI: the API can be forced to use a specific function by setting `tool_choice` to `{"type": "function", "function": {"name": "my_function"}}`.

## Pattern

1. Define a tool such as `print_summary` with an `input_schema` of `type`, `properties` and `required`.
2. Pass `tools` and `tool_choice` with `"type": "tool"` and the tool `name` to `client.messages.create`.
3. Loop over `response.content`; when `content.type == "tool_use"`, read `content.input`.

## Older prompt-only approach

The JSON mode notebook says assistant-message prefill is only accepted on Claude Haiku 4.5, and that current models return a 400 for a request that ends with an assistant message. It points current models to structured outputs instead. Its fallback is to have Claude put each JSON item in specified tags for extraction.

## See also

- [ao-tools-anthropic-tool-use-loop](https://iinventedlight.com/library/notes/ao-tools-anthropic-tool-use-loop/)
- [ao-structured-across-providers](https://iinventedlight.com/library/notes/ao-structured-across-providers/)
- [ao-structured-gemini-json-schema](https://iinventedlight.com/library/notes/ao-structured-gemini-json-schema/)
- [ao-structured-ollama-and-llamacpp-grammars](https://iinventedlight.com/library/notes/ao-structured-ollama-and-llamacpp-grammars/)

## Claims

- id: C1
  claim: Forced tool via tool_choice type tool.
  source: S1
  evidence: "tool_choice={\"type\": \"tool\", \"name\": \"print_all_characteristics\"}"

- id: C2
  claim: The JSON is read from the tool input.
  source: S1
  evidence: "tool_output = content.input"

- id: C3
  claim: OpenAI can force a specific function.
  source: S2
  evidence: "The API can be forced to use a specific function"

- id: C4
  claim: Prefill is only accepted on Haiku 4.5.
  source: S3
  evidence: "is only accepted on Claude Haiku 4.5"

## Sources

- [S1] Extracting structured JSON using tools. anthropics/claude-cookbooks (GitHub). https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/extracting_structured_json.ipynb . License: MIT. Accessed 2026-11-16.

- [S2] How to call functions with chat models. openai/openai-cookbook (GitHub). https://github.com/openai/openai-cookbook/blob/main/examples/How_to_call_functions_with_chat_models.ipynb . License: MIT. Accessed 2026-11-16.

- [S3] How to enable JSON mode. anthropics/claude-cookbooks (GitHub). https://github.com/anthropics/claude-cookbooks/blob/main/misc/how_to_enable_json_mode.ipynb . License: MIT. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: anthropics/claude-cookbooks (GitHub), Extracting structured JSON using tools, MIT (https://raw.githubusercontent.com/anthropics/claude-cookbooks/main/LICENSE). Copyright (c) 2023 Anthropic; openai/openai-cookbook (GitHub), How to call functions with chat models, MIT (https://raw.githubusercontent.com/openai/openai-cookbook/main/LICENSE). Copyright (c) 2025 OpenAI; anthropics/claude-cookbooks (GitHub), How to enable JSON mode, MIT (https://raw.githubusercontent.com/anthropics/claude-cookbooks/main/LICENSE). Copyright (c) 2023 Anthropic. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
