N
NexusDigitalLabs
← Back to Academy

Phase 2 — Working with LLMs · Lesson 22 · 20 XP

Structured output and validation

Asking a model to "return JSON" is not the same as guaranteeing valid JSON matching your schema — models occasionally miss a field, use the wrong type, or wrap the JSON in prose. Getting a response back is only step one; you still have to validate it, which is exactly what Pydantic (Lesson 8) is for.

A robust pattern: define the expected shape as a Pydantic model, ask the model for output matching it (via a JSON schema in the prompt or provider-specific structured output/tool-use features), validate what comes back, and if validation fails, resend the request with the validation error included so the model can self-correct — rather than giving up or blindly retrying the identical request.

Exercise

Define a Pydantic model for extracting structured data from unstructured text (e.g. name, date, and amount from an invoice-like paragraph). Get matching JSON from a model, validate it, and retry once with the validation error included if it's malformed.

Check yourself

1. Why is asking a model for JSON not enough on its own — what still has to happen after you get a response back?

2. When validation fails, why is retrying with the error message included usually better than retrying the identical request?

← Previous lesson

Streaming

Answer the check-yourself questions to unlock this