output_parser
On this page
A production-grade output parsing, validation, fault-tolerance, and retry engine for LLM/RAG/agent pipelines.
output_parser turns messy, unpredictable raw text coming out of a language model into structured, validated, optionally typed Python data — without you having to hand-write regexes or brittle json.loads() calls for every prompt.
It ships two complementary layers that can be used independently or together:
- The single-format parser toolkit — small, focused parser classes (
JSONOutputParser,YAMLOutputParser,CSVOutputParser,ListOutputParser,StructuredOutputParser,PydanticOutputParser) built on a sharedBaseOutputParserinterface. You pick the parser for the format you expect. - The orchestration engine (
OutputParserinparser.py) — a full pipeline that automatically detects the format of unknown text, parses it, repairs it if broken, optionally retries against a live LLM with escalating prompts, validates the result (schema, business rules, safety), and returns everything as aParseResultwith a completeParseTraceaudit log.
Why this exists
LLMs are asked to "return JSON" and instead return JSON wrapped in prose, wrapped in a code fence, with a trailing comma, or as YAML, or as a numbered list. Traditional json.loads(response) breaks immediately. output_parser is built around the assumption that malformed output is the normal case, not the exception, and gives you a layered strategy to recover a usable answer anyway.
Module map
Two ways to use this library
A. Single-format parsers — use when you know exactly what format you're expecting from the LLM:
from output_parser import JSONOutputParser
parser = JSONOutputParser()
result = parser.parse('Here is the data: {"name": "John", "age": 30}')
# {'name': 'John', 'age': 30}
B. The orchestrator — use when the format is unpredictable, you want automatic repair/retry, or you need a full audit trail:
from output_parser import OutputParser
parser = OutputParser()
result = parser.parse('{"answer": "Paris", "confidence": 0.98}')
print(result.data) # {'answer': 'Paris', 'confidence': 0.98}
print(result.trace.summary())
Continue to Installation and Quickstart.
[!NOTE] Scope of this documentation This documentation was generated directly from the
output_parserpackage source (14 modules, ~3,700 lines). Every signature, parameter, exception, and behavior described here reflects the actual implementation. Where a behavior could not be determined from the source (e.g. exact upstreampydantic/pyyamlversion compatibility), it is explicitly noted rather than assumed.