How do you implement structured output generation with LLMs?
Structured output forces LLMs to generate responses in specific formats (JSON, XML, typed objects) for programmatic consumption. Methods: (1) JSON mode: API-level JSON enforcement (OpenAI jsonobject response format, Anthropic tool use for structured extraction). The model is constrained to output valid JSON. (2) Schema-based: provide a JSON Schema or Pydantic model definition, and the LLM generates output conforming to the schema. OpenAI Structured Outputs guarantees schema compliance. (3) Grammar-based: for local models, GBNF grammars in llama.cpp constrain token generation to match a formal grammar, guaranteeing syntactic correctness. (4) Tool/function calling: define output as tool parameters with JSON Schema, model populates the parameters, and you extract the structured data from the tool call. Implementation: define your output schema clearly (field names, types, descriptions, constraints), include the schema in the system prompt or API configuration, provide 1-2 examples of expected output format, and validate generated output against the schema. Libraries: instructor (Python, works with OpenAI, Anthropic, Ollama), zod with OpenAI SDK for TypeScript, and LangChain structured output parsers. Validation: always validate LLM output even with structured output mode (belt and suspenders approach), implement retry logic for validation failures (re-prompt with the error), and set sensible defaults for optional fields. Use cases: data extraction from text (extract entities into typed objects), classification with metadata (category + confidence + reasoning), API response generation (type-safe responses from natural language queries), and form auto-fill (extract form fields from unstructured documents). Best practices: keep schemas simple (flat structures are more reliable than deeply nested), use enums for constrained fields, provide clear field descriptions, and test with diverse inputs to verify robustness.