Output parsing (LLM)
Parsing and extracting structured data from an LLM's text answer — and handling when the model doesn't follow the requested format exactly. The messy reality behind getting reliable, machine-readable output from a model that generates free text.
Problem: ask a model for JSON and you often get near-JSON — extra chatter around it ("Here's the JSON:"), broken quotes, truncated output, or markdown fences. A naive JSON.parse crashes. Output parsing handles this: robust parsers that pull JSON out of surrounding text, repair common errors, validate against a schema, and on failure ask the model to try again ("retry with error"). Libraries: LangChain's OutputParsers, Pydantic validation. The better solution when available: constrained decoding / structured output (force the model to only generate valid JSON at the token level), which eliminates the problem at the source. Output parsing is the defense when you don't have that guarantee. An everyday but critical part of building reliable LLM apps. Related to structured output and function calling.