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
- Define a tool such as
print_summarywith aninput_schemaoftype,propertiesandrequired. - Pass
toolsandtool_choicewith"type": "tool"and the toolnametoclient.messages.create. - Loop over
response.content; whencontent.type == "tool_use", readcontent.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
- ao-structured-across-providers
- ao-structured-gemini-json-schema
- 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.
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
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.},
}
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.