0
LLM Engineering & APIs
Phase 2Module 7 of 12

Output Parsers

LLMs generate text, but applications need JSON, Pydantic models, lists, or enums. Parsers extract structured data from LLM output, with retry logic when the model doesn't format correctly.

A translator converting a chef's verbal recipe (free text) into a printed recipe card with labeled sections (structured format). The parser is the translator that extracts ingredients, steps, and timing.

Visual Workflows

Start here — study each diagram, then use + / to zoom if needed.

100%
Loading diagram...

Scroll inside the frame · use + / − to zoom

Key Takeaways

  • 1.Output parsers convert raw LLM text responses into structured Python objects — handling the gap between free-form generation and typed application code.
  • 2.Parser types: PydanticOutputParser (parse into Pydantic models), JSONOutputParser, StructuredOutputParser (with schema), CommaSeparatedListOutputParser.
  • 3.Flow: parser.get_format_instructions() → inject into prompt → LLM generates → parser.parse(response).
  • 4.Retry: if parsing fails, re-prompt with error message.
  • 5.Modern alternative: native structured outputs (OpenAI response_format, tool calling) are more reliable than parse-from-text.

Real Example

Scenario

Extract meeting info: prompt asks for title, date, attendees. LLM returns JSON string. PydanticOutputParser validates into Meeting(title=str, date=datetime, attendees=list[str]). If date format wrong, parser raises error → retry with 'date must be YYYY-MM-DD'.

What you would do

In LLM Engineering & APIs, apply Output Parsers to this scenario: Extract meeting info: prompt asks for title, date, attendees. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Output Parsers (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Output Parsers happens in the code.

Output Parsers
1from langchain_core.output_parsers import PydanticOutputParser  # import dependencies2from pydantic import BaseModel, Field  # import dependencies3
4class MovieReview(BaseModel):  # define a data structure or component5    title: str = Field(description="Movie title")6    rating: int = Field(description="Rating 1-10")7    summary: str = Field(description="One sentence summary")8
9parser = PydanticOutputParser(pydantic_object=MovieReview)  # key line for Output Parsers10
11format_instructions = parser.get_format_instructions()12# Inject into prompt: f"\n{format_instructions}"13
14# Simulate LLM response15llm_output = '{"title": "Inception", "rating": 9, "summary": "Mind-bending thriller about dreams."}'  # key line for Output Parsers16review = parser.parse(llm_output)  # key line for Output Parsers17print(review.title, review.rating)  # show output for debugging

Cheat Sheet

Quick recap

quick ref
  • PydanticOutputParser(pydantic_object=Model)
  • parser.get_format_instructions()
  • parser.parse(llm_output)
  • Retry on ValidationError
  • Prefer structured outputs API when available
  • Field(description=) guides LLM

Common Mistakes

  • Not including format instructions in prompt — low parse success
  • No retry logic — single parse failure breaks pipeline
  • Using text parsing when structured outputs API available
  • Overly complex schemas — LLM struggles with deep nesting