- Home
- Skills
- Workflow Automation
- Instructor: Structured LLM Output with Pydantic
More screenshots
Works with the AI Agents you already use
Instructor: Structured LLM Output with Pydantic
Stop parsing LLM output on a prayer: define the shape as a Pydantic schema, let validation errors teach the model through retries, and stream when latency matters.
$5
Instructor: Structured LLM Output with Pydantic
Example session with this skill installed
We process supplier invoices (PDFs converted to text). I need vendor, invoice number, date, line items, and the grand total as structured records for our ledger. Problem: on maybe 1 in 15 invoices the printed total doesn't match the sum of the line items, and those must be flagged for a human, not booked.
- Read your context and instructions
- Compiled the instructor
INVOICE EXTRACTION — schema, validator, and failure policy
Schema (descriptions are the instructions; note the self-report field):
class LineItem(BaseModel):
description: str = Field(description="Item text as printed on the invoice")
amount: float = Field(description="Line total for this item")
class Invoice(BaseModel):
vendor: str = Field(description="Vendor name as printed at the top")
invoice_number: str = Field(description="The invoice's own number, not an order or PO number")
date: str = Field(description="Invoice date as printed, ISO format")
items: list[LineItem]
total: float = Field(description="The grand total the invoice asks to be paid")
totals_match: bool = Field(description="True if items sum to total within 0.02")
The catch is a model validator, not hope: it sums the items independently and, if the arithmetic disagrees while totals_match is true, raises an error naming both numbers. That error goes back to the model on retry — it re-reads the amounts and either fixes a misread digit or returns totals_match: false, which is the outcome you actually want for your 1-in-15.
Configuration: native structured mode (tool calling) preferred; max_retries=3 per the budget table for a nested schema on a frontier model. Exhausted retries go to a dead-letter queue with the raw model output logged — those logs are your schema's bug reports.
Failure policy wired to your ledger: totals_match: false → human review queue, never booked. Clean extractions → booked directly. Before this touches volume, run it against your five ugliest historical invoices (scans with skewed tables are where extraction schemas go to die) and fix the schema against what breaks.
Connects securely to your tools. The creator never sees your data.
About this skill
Every LLM pipeline eventually hits the same wall: the model returns almost-JSON, a number where a string belongs, an invented category — and downstream code breaks in ways that are nobody's job to fix. This skill is the reliable alternative. You define the output as a Pydantic schema, hand it to the model as a contract, and let automatic validation-plus-retry close the gap between "usually right" and "contractually right."
The core discipline: the schema is a prompt, not just a type. Field descriptions are written as instructions — where to look, what counts, what to do when data is absent. Types do the constraining (enums for categories, bounds for numbers, formats for strings), so whole classes of wrongness become impossible rather than unlikely. When validation still fails, the specific error goes back to the model ("items sum to 412.10 but total is 421.10 — re-read the amounts"), and that specificity is why retries land instead of looping.
The skill covers the full working surface: a step protocol from schema draft to shipped failure policy, a schema design checklist, a retry-budget decision table keyed to model size and schema complexity, the four patterns that earn their keep (extraction, classification with evidence, multi-entity, structured analysis, batch), streaming modes for latency-sensitive UI, provider notes for native structured output vs. JSON-mode fallback, and a failure-mode table mapping symptoms to fixes. A complete worked example extracts invoices with a cross-field validator that catches totals which don't match their line items — the class of error that validates cleanly and is still wrong. Edge cases include self-contradicting documents, oversized inputs, multilingual sources with evidence quotes, and prompt-injection text hiding inside documents you're extracting from.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Workflow Automation.
- Validation guarantees shape and stated constraints, not truth: a confidently wrong value that satisfies the schema still passes unless you add evidence fields and validators, and the skill pushes you to add them.
- Smaller and local models need simplified schemas and more retries; deeply nested schemas that work on a frontier model may never stabilize on a small one.
- It covers the structured-output layer only — document conversion (PDF to text), storage, and your ledger logic stay yours.
- Retry loops cost tokens in proportion to failure rate; a chronically failing schema is a design bug to fix, not a budget to raise indefinitely.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 24 days ago
Permissions
No special permissions declared or detected