structured-output-architect
Architect-level design of model output contracts — schema-first modelling, provider-enforced JSON versus prompted JSON, validation and repair loops, streaming partial objects, tool-call arguments as structured output, and the failure taxonomy that turns malformed responses into typed errors. Use when a program must consume what a model returns.
- License 许可
- MIT
- 最近更新
- 2026-11-13
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- structured-output-architect
- license
- MIT
- metadata
- { "crusor-path": "ai-ml-architect/llm-architect/structured-output-architect", "crusor-domain": "ai-ml-architect", "crusor-origin": "original", "crusor-sources": "platform.openai.com/docs/guides/structured-outputs; docs.anthropic.com/en/docs/build-with-claude/tool-use; ai.google.dev/gemini-api/docs/structured-output", "crusor-triggers": "structured output, json schema, json mode, response schema, pydantic, zod, parsing llm output, repair loop, function schema, invalid json, json parsing, parse error, malformed output", "crusor-verified-on": "2026-10-30" }
- description
- Architect-level design of model output contracts — schema-first modelling, provider-enforced JSON versus prompted JSON, validation and repair loops, streaming partial objects, tool-call arguments as structured output, and the failure taxonomy that turns malformed responses into typed errors. Use when a program must consume what a model returns.
- compatibility
- OpenAI strict schemas, Anthropic tool schemas, Gemini responseSchema, Pydantic/zod
03 SKILL.md 正文
Structured Output Architect
Structured output is an interface contract with a stochastic implementation. Treat it like any untrusted input boundary: enforce, validate, and fail in a typed way.
When to use
- A program consumes model output (extraction, classification, routing, tool args).
- Existing parsing is regex over prose.
- You need streaming with partial objects.
When NOT to use
- The output is prose for a human — then the contract is length and tone, not schema.
Decision 1 — Schema first
| Rule | Why |
|---|---|
required fields, additionalProperties: false |
optionality invites omission |
| enums for closed sets | free strings drift |
| nullable required fields over optional | "missing" and "null" differ |
| small and flat where possible | deep nesting increases error rate |
| no free-text blobs inside structure | they defeat the point |
- Derive the schema from your domain types (zod/Pydantic) so one source is truth for contract and validation.
- Numeric constraints (min/max, integer) are part of the schema, not prompt prose.
Decision 2 — Enforcement level
| Approach | Guarantee |
|---|---|
| provider-enforced schema | valid JSON shape, not semantic truth |
| tool-call arguments | same, with the tool contract |
| prompted JSON | best effort; parse failures expected |
| free text + regex | fragile; avoid |
- Prefer provider enforcement; validate against the same schema server-side anyway.
- Enforcement guarantees shape, never correctness: a valid object can hold the wrong id.
Decision 3 — Validation and repair
model output → schema validation → pass: proceed
→ fail: one repair attempt with the error message
→ fail again: typed error to caller
- Repair prompts include the validation error and the original output; do not re-ask blindly.
- Exactly one repair attempt — loops amplify cost and mask a schema that is too complex.
- Distinct error types:
ModelFormatError,SchemaViolation,SemanticMiss— metrics per type.
Decision 4 — Streaming
- Stream tokens for UX, but only emit structured events when the object completes, or stream a line-delimited record per item.
- Partial JSON parsing needs a tolerant parser; do not render incomplete objects as if final.
- If the UI needs progressive output, prefer an array of small objects over one large object.
Decision 5 — Semantics beyond shape
- Validate business rules after parsing: dates in range, ids exist, totals reconcile.
- Cross-field checks (start < end, sum matches lines) catch what schema cannot.
- For extraction, require the source span for each field; a field without a span is not auditable.
Failure modes at scale
- Regex parsing of prose; a formatting change silently zeroes a metric.
- Schema with nested optionals; 15% of responses missing the key field.
- Repair loop retrying five times; latency triples, cost quadruples.
- Valid JSON with an id from another tenant, accepted because it parsed.
- Streaming partial JSON rendered as final in the UI, confusing users.
Verification receipts
- Schema committed, shared by prompt, validator, and types.
- Malformed-response test: typed error surfaced, no crash.
- Repair path exercised and bounded to one attempt.
- Business-rule validation tests after parsing.
- Format-error rate monitored per model and prompt version.
04 同仓库更多 Skills
interaction-patterns-architect
Architect-level interaction design in code — choosing established patterns over invented ones, feedback and latency behaviour, destructive-action safeguards, navigation and overlay conventions, and the state model behind every interactive surface. Use when a screen has to behave, not just look.
design-system-ops-architect
Architect-level operation of a design system in code — token pipelines and versioning, component API and variant policy, the catalogue as the contract, adoption metrics, and the governance that stops a system from forking into three. Use when a component library must survive several teams.
design-handoff-architect
Architect-level design handoff — what must be specified before implementation starts, tokens over raw values, responsive and content-limit contracts, state matrices, and the review loop that catches drift between design and code. Use when a design is about to become code.
content-microcopy-architect
Architect-level UI content — where strings live, voice and terminology consistency, error and empty-state copy as part of the interface, translation-ready authoring, and the content model a product needs before it multiplies screens. Use when the words in the interface matter as much as the pixels.
ui-ux-implementation-architect
Architect-level direction for turning a design into working UI — the handoff contract, component ownership versus screen-level layout, interaction and state conventions, design QA gates, and the review habits that keep an implementation faithful without frozen pixels. Use before any specific UI implementation skill.
test-strategy-architect
Architect-level test strategy — allocating assertions across unit, integration, contract and end-to-end layers, designing for testability through dependency boundaries, fixture and data strategy, and the flake and coverage policies that keep a suite trustworthy. Use when defining how a codebase is tested.