structured-output
Get reliable typed JSON from LLMs using constrained decoding, JSON Schema, Zod, Pydantic, and Instructor. Use when implementing structured output, JSON schema…
от crouton-labs★ 25 звёзд на GitHub
Установить structured-output в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Structured Output
Structured output guarantees valid typed JSON from LLMs — the model can only emit tokens that conform to your schema. The question isn't how it works but when you need it.
Use when: data extraction, API responses, agent tool outputs, any downstream code that parses the response.
Don't use when: prose, creative writing, chat, or when the quality cost outweighs the reliability gain.
For code examples and provider-specific patterns, see reference.md.
The Quality Problem
Constrained decoding has a cost. Tam et al. (2024) showed JSON constraints dropped math accuracy by 26–32 percentage points across tested models. The mechanism: forced key ordering means the model must generate answer tokens before reasoning tokens, disrupting sequential reasoning.
The fix is schema design, not giving up on structured output. Enforcement is syntactic, not semantic — valid JSON can still be semantically wrong.
Schema Design: The #1 Lever
The PARSE paper (2025) found structural reorganization was the top optimization for extraction accuracy — more impactful than model choice or prompt engineering. A single ambiguous field name (final_choice alongside potential_final_choices) collapsed accuracy from 95% → 4.5% in Instructor's experiments.
The reasoning field trick
Put a reasoning or thinking field first in your schema. LLMs generate keys in order — reasoning before answer tokens restores chain-of-thought within structured output. Instructor showed this alone recovers accuracy from 33% → 92% on GSM8K.
{
"type": "object",
"properties": {
"reasoning": { "type": "string", "description": "Step-by-step analysis before concluding" },
"answer": { "type": "string" },
"confidence": { "type": "string", "enum": ["high", "medium", "low"] }
},
"required": ["reasoning", "answer", "confidence"],
"additionalProperties": false
}
Rules that matter
- Flat over nested. PARSE found flattening was the most frequent successful optimization (55% of cases).
- Specific field descriptions: "Full legal name including first and last" beats "The name". Specific descriptions reduced errors by up to 64.7% (PARSE).
- String enums for constrained choices — reduces downstream cleaning by ~70% (PARSE).
minimum/maximum/pattern— neither Anthropic nor OpenAI enforces these at the grammar level. Validate post-generation.- Max 24 optional parameters (Anthropic hard limit). Each optional field roughly doubles grammar state space.
- Recursive schemas not supported on Anthropic.
These patterns apply directly to tool-design — tool schemas are structured output schemas.
The Production Loop
Type system (Zod/Pydantic)
→ JSON Schema
→ LLM with constrained decoding
→ Validate (syntactic: already guaranteed; semantic: your validators)
→ Retry with error feedback on failure
Libraries doing this loop:
- Python: Instructor wraps OpenAI/Anthropic/Groq/etc, feeds validation errors back into the conversation, retries up to
max_retries. - TypeScript/OpenAI:
zodResponseFormat+beta.chat.completions.parse()— returns.parsedtyped directly. - TypeScript/Anthropic:
zodOutputFormathelper from@anthropic-ai/sdk/helpers/zod. - TypeScript/any provider: Vercel AI SDK
generateObject/streamObjectwith a Zod schema.
Zod v4 has native z.toJSONSchema() for converting schemas to JSON Schema format.
For mechanism details, provider comparison, streaming patterns, and failure modes, see reference.md.
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- reference.md
FAQ
Что делает скилл structured-output?
Get reliable typed JSON from LLMs using constrained decoding, JSON Schema, Zod, Pydantic, and Instructor. Use when implementing structured output, JSON schema validation, typed API responses, constrained generation, Zod schemas, Pydantic models, schema design for LLM extraction, streaming structured data, or debugging malformed JSON responses from models.
Как установить скилл structured-output?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл structured-output запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
skill-creator
AnthropicCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an…
discernment-nudge
AnthropicAfter you give a substantive answer or draft that the user may act on — advice or recommendations, drafted artifacts such as goals, plans, pitches, proposals…
data-consolidate
AnthropicJoins the plant's scheduled exports and connector data into one working dataset — ERP items (NetSuite or Infor M3), sales orders, production work orders…
inspection-to-capa
AnthropicRuns the quality chain end to end: writes the nonconformance against the requirement it violates, searches history at the moment of writing to say whether…