All capabilities · LLM application development

Get reliable structured outputs from LLMs

JSON schemas, function schemas, validation with Pydantic/Zod, handling malformed outputs.

~6 focused hoursbeginner
Explore 4 tools for this project
Market relevance

Which roles ask for this — and how often

Share of job postings in India, per role, that name this capability.

What employers mean

You should be able to…

  1. Define a JSON schema/Pydantic model and force the LLM to return data matching it
  2. Use function/tool calling to extract structured fields from unstructured text (invoices, resumes, tickets)
  3. Validate and repair malformed LLM JSON output before it hits downstream code
  4. Handle nested/optional fields and enums in extraction schemas
  5. Fall back gracefully when the model returns invalid structure (retry, default, or flag for review)
  6. Chain structured extraction with a database write or API call

Needs first: Integrate LLM APIs into an application

Learn — free, link-checked

The few resources that matter

Tools for practice

Choose a tool for the job

Start with one tool for each part of your project. You don’t need to learn them all.

Go to the practice brief

4 tools to explore

Pydantic

Build · Test

Define typed schemas and validate the structured data entering your application.

OpenAI API

Build · Test

Connect model calls, tool use and structured responses to your own application.

Claude API

Build · Test

Build model-backed features with messages, tool use and responses you can evaluate.

Practices & references

  • JSON Schema
  • Validation and retries
Practice

Schema-validated field extractor for GST invoices

Write a generator that emits 20 synthetic GST invoices, varying vendor layout, GSTIN format, HSN codes and CGST/SGST splits, and keeping the ground truth for each. Extract every invoice into a strict Pydantic schema (vendor, GSTIN, line items, tax breakup, total) using the provider's structured-output mode. Add a validation layer that recomputes the tax arithmetic and flags any invoice where the splits and line totals do not reconcile with the stated total, and repair malformed model output with one corrective retry before failing loudly.

Start from

20 synthetic GST invoices you generate - a script varies vendor layout, GSTIN, HSN codes and CGST/SGST splits, and stores the ground truth for scoring

Milestones
  1. Write the generator that emits 20 varied invoices plus their ground truth · ~1.5h
  2. Define the Pydantic schema and get structured extraction running end to end · ~1.5h
  3. Add the tax-reconciliation validator and the malformed-output repair retry · ~1.5h
Done when
  • Pydantic schema enforces types (GSTIN pattern, decimal amounts) and rejects invalid extractions
  • At least 15 varied sample invoices (different formats/vendors) processed with over 90% field accuracy
  • A validation check flags cases where CGST+SGST+line totals don't reconcile with the invoice total
  • Malformed model output triggers one retry with an error-correction prompt before failing loudly
Prove it

Evidence a recruiter can check

  • Field-level accuracy scored against the generator's ground truth across all 20 invoices
  • The validator log catching an invoice whose CGST and SGST do not reconcile with its stated total
  • The Pydantic schema itself, with the GSTIN pattern and decimal constraints that reject a bad extraction
  • A malformed model response pasted next to the corrective retry that repaired it
Signal it

Built a schema-validated invoice extractor using structured outputs - Pydantic-enforced GSTIN and decimal types, a tax-reconciliation check that flags invoices whose splits do not add up, and a repair retry for malformed model output.

Interview

Questions you'll get asked

  1. How do you guarantee an LLM response is valid JSON your backend can parse without crashing?
  2. What's the difference between JSON mode, function calling, and Pydantic-based structured outputs?
  3. How do you handle a field the model hallucinates that isn't in your schema?
  4. Walk me through extracting structured data (amount, date, vendor) from a scanned invoice using an LLM.
  5. How would you validate and auto-correct a malformed LLM output in a production pipeline?
  6. How do you design a schema so the model doesn't have to guess formats (dates, currency) inconsistently?