Structured Output
Force the LLM to return JSON, not free text
🔑 Key Concepts
- Why structured output? — Free text is hard to parse. JSON can be validated, stored, and used programmatically.
- Function calling — Define a JSON schema, and the model outputs arguments matching that schema. Most reliable method.
- JSON mode — response_format={'type': 'json_object'} guarantees valid JSON but doesn't guarantee the schema.
- Pydantic + LLM — Use instructor or marvin libraries to map LLM outputs directly to Pydantic models with validation.
💡 Practice: Try implementing each concept yourself before moving on. Reading about RAG and building RAG are very different things.