agent-instructions
Use when writing project instructions for a coding agent (CLAUDE.md, AGENTS.md, or equivalent). Covers what belongs in them, what does not, structure, and…
Use when an LLM must return machine-readable data. Covers schema design for models, native structured-output modes, validation and repair, and extraction that survives contact with messy input.
$ npx -y skills add nimadorostkar/Claude-Skills-collection --skill structured-output --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/structured-outputContext preview
The summary Claude sees to decide when to auto-load this skill.
Use when an LLM must return machine-readable data. Covers schema design for models, native structured-output modes, validation and repair, and extraction that survives contact with messy input.
name: structured-output description: Use when an LLM must return machine-readable data. Covers schema design for models, native structured-output modes, validation and repair, and extraction that survives contact with messy input. metadata: category: ai version: 1.0.0 tags: [structured-output, json, extraction, schema, validation]
Get reliably parseable, correctly typed data out of a language model. The naive approach — asking for JSON in the prompt and calling `json.loads` — fails often enough to be a production incident.
1. **Use the API's native mechanism** — JSON schema mode or tool calling constrains the decoder so that malformed output is structurally impossible. Asking for JSON in prose does not. 2. **Design the schema for a model, not a database** — Descriptive field names, enums instead of free strings, and a description on every field explaining what belongs in it. The schema is documentation the model reads. 3. **Make absence representable** — A nullable field with a clear meaning. Without one, the model will invent a plausible value rather than leave it out, because that is what the schema demanded. 4. **Validate, then repair, then fail** — Parse against the schema. On a semantic failure (a valid date that is in the future when it must be past), send the error back for one repair attempt, then give up cleanly. 5. **Measure per field** — Aggregate extraction accuracy hides the one field that is wrong 40% of the time.
**A schema that guides the model:**
from pydantic import BaseModel, Field
from typing import Literal
class ExtractedInvoice(BaseModel):
invoice_number: str | None = Field(
description="The invoice number exactly as printed. Null if not present. "
"Do not construct one from the filename or the date."
)
total_cents: int | None = Field(
description="The final amount due, in minor units (cents/pence). "
"Use the TOTAL, not the subtotal and not any line item."
)
currency: Literal["USD", "EUR", "GBP"] | None = Field(
description="ISO code, inferred from the currency symbol or explicit code. "
"Null if genuinely ambiguous — do not assume USD."
)
due_date: date | None
confidence: float = Field(
ge=0, le=1,
description="Your confidence that every extracted field is correct. "
"Below 0.7 if the document is unclear, is not an invoice, "
"or if you had to guess any field."
)
source_quotes: dict[str, str] = Field(
description="For each non-null field, the exact text from the document "
"that supports it. This is used for audit."
)**Extraction with validation, one repair attempt, and a human fallback:**
async def extract(document: str) -> ExtractedInvoice | HumanReview:
result = await model.complete(
EXTRACTION_PROMPT.format(document=document),
response_format=ExtractedInvoice, # native constrained decoding
)
# Structurally valid by construction. Now check that it is semantically sane.
errors = validate_semantics(result, document)
if errors:
# One repair attempt, with the specific errors fed back.
result = await model.complete(
REPAIR_PROMPT.format(document=document, previous=result, errors=errors),
response_format=ExtractedInvoice,
)
errors = validate_semantics(result, document)
if errors or result.confidence < 0.7:
return HumanReview(document=document, draft=result, reasons=errors)
return result
def validate_semantics(inv: ExtractedInvoice, document: str) -> list[str]:
errors = []
if inv.total_cents is not None and inv.total_cents <= 0:
errors.append("total_cents must be positive")
if inv.due_date and inv.due_date.year < 2000:
errors.append(f"due_date {inv.due_date} is implausible")
# The strongest check available: every quoted span must exist in the source.
for field, quote in inv.source_quotes.items():
if quote not in document:
errors.append(f"source_quote for '{field}' does notA curated library of 137 production-grade skills for Claude and other AI coding agents. Every skill follows one structure, speaks with one voice, and earns its place by changing what the agent does.
Repo: nimadorostkar/Claude-Skills-collection
Use when writing project instructions for a coding agent (CLAUDE.md, AGENTS.md, or equivalent). Covers what belongs in them, what does not, structure, and…
Use when an agent needs state that survives a session or a context compaction. Covers what to persist, file-based memory, structuring notes for retrieval, and…
Use when automating agent behavior with lifecycle hooks. Covers hook events, deterministic enforcement of rules the model should not be trusted to remember,…
Use when packaging skills, commands, hooks, and MCP servers into a distributable plugin. Covers manifest structure, bundling, versioning, testing, and…
Use when writing a new skill for an AI agent. Covers scoping, description writing for reliable triggering, progressive disclosure, and the difference between a…
Use when reviewing or improving an existing agent skill. Covers triggering accuracy, content quality, redundancy with the base model, and measuring whether the…