LLM Structured Output: JSON Schema, Function Calling, and Pydantic Integration
Force reliable structured data extraction from LLMs with zero parsing failures
LLM Structured Output: JSON Schema, Function Calling, and Pydantic Integration
Force reliable structured data extraction from LLMs with zero parsing failures
Master reliable structured output extraction from LLMs using JSON Schema mode, function calling, Pydantic validators, and instructor library for zero-failure parsing in production.
Structured output is the foundation of reliable LLM applications. Three approaches ranked by reliability: 1) JSON Mode (OpenAI/Anthropic): set response_format={"type": "json_object"} - guarantees valid JSON output but not specific schema adherence. 2) Function Calling (OpenAI): define output schema as function parameters - LLM must output valid parameters matching the JSON schema. Most reliable approach. 3) Instructor library: wraps OpenAI/Anthropic APIs with Pydantic models, automatically retries on validation failure, provides clean Python API. from instructor import from_openai; client = from_openai(OpenAI()); response = client.chat.completions.create(model="gpt-4o", response_model=ProductReview, messages=[...]). Pydantic model defines the exact output schema with validators. Production patterns: 1) Always validate outputs even with JSON mode - LLMs can still produce semantically invalid data (negative prices, future birthdates). 2) Implement retry with error feedback: if validation fails, send error message back to LLM asking it to fix specific fields. 3) Partial outputs: for long extractions, use streaming with partial validation to return results as they are generated. 4) Schema versioning: when output schema changes, version your prompts and handle old format gracefully. Real-world example: entity extraction from unstructured text using instructor with nested Pydantic models for people, organizations, locations with confidence scores.
相关教程
Build complex multi-step AI workflows with state management using LangGraph
Chain-of-thought, tree-of-thoughts, self-consistency, and systematic evaluation methods
Deploy Llama 3 with 20x higher throughput than naive serving