← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:zq92xzeh

iinventedlight. 2026. "Structured JSON from Claude with a forced tool." iinventedlight Library, iil:note:zq92xzeh, version of 2026-11-16. https://iinventedlight.com/library/notes/ao-structured-claude-json-via-tools/. License: CC-BY-4.0.

@misc{iil_note_zq92xzeh,
  author = {{iinventedlight}},
  title = {{Structured JSON from Claude with a forced tool}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-16},
  url = {https://iinventedlight.com/library/notes/ao-structured-claude-json-via-tools/},
  note = {Permanent ID iil:note:zq92xzeh. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/ao-structured-claude-json-via-tools.md.