What are output parsers and why are they needed?
LLM & AILevel: IntermediateAdvanced Prompting
What are output parsers and why are they needed?
Hints
- Identify the stage: pretraining, adaptation, generation or evaluation.
- Name the inputs, constraints and a case where the approach fails.
Answer
LLMs return unstructured text. Production code needs structured data. Output parsers:
- Define a schema — Pydantic model or JSON schema
- Inject format instructions into the prompt
- Parse the output — JSON parsing, regex, structured extraction
- Handle failures — retry with error feedback if parsing fails
Example with Pydantic:
from pydantic import BaseModel
class MovieReview(BaseModel):
title: str
sentiment: Literal["positive", "negative", "neutral"]
confidence: float
key_points: list[str]
Modern approach: use response_format={"type": "json_schema", "schema": ...} with OpenAI or function calling to get guaranteed valid JSON from the model.
Source and licence: JustInCache & contributors — Awesome AI Interviews · Apache-2.0