Skip to main content
In this lesson we explore output parsers and how they convert free-text responses from large language models into structured Python data. Output parsers make it simple to enforce predictable formats (CSV, JSON, lists) so downstream code can consume model outputs reliably.
The image shows the interface of a Jupyter Notebook, displaying an empty code cell ready for input. The toolbar and menu options are visible at the top.
This lesson assumes your OpenAI API key is already configured in your environment.

Quick overview

  • Goal: Instruct an LLM to return a comma-separated list and parse it into a native Python list.
  • Approach: Append parser-provided format_instructions to the prompt so the model emits a predictable format, then use the parser to convert the text to Python objects.

1. Imports and basic prompt setup

Import the required classes, create an LLM client, and define a simple prompt template.

2. Invoke the model with a simple prompt (raw output)

Call the LLM without any parser instructions to see the default textual output.
Example raw output (string)
This raw response is a plain string with numbered lines. To use it programmatically you’d typically write manual parsing logic (strip numbering, split lines). Output parsers automate this process.

3. Add a CommaSeparatedListOutputParser and get format instructions

The CommaSeparatedListOutputParser provides human-readable instructions that you append to your prompt. These instructions encourage the LLM to produce CSV-style output that can be parsed reliably.
Format instructions printed:

4. Inject the format instructions into the prompt

Use partial_variables in PromptTemplate to include the parser’s format_instructions in the prompt. This keeps your prompt template flexible and reusable.
The constructed final_prompt will look like:
Tip: Using partial_variables lets you add dynamic instructions (like format hints) without changing the main prompt template every time.

5. Invoke the model with the parser-aware prompt and parse the output

Now the model is instructed to return CSV-style text. After receiving the text, feed it to output_parser.parse() to get a Python list.
Example console outputs:

Summary

  • Append parser format_instructions to your prompt to guide the model toward a predictable output format (CSV in this example).
  • Use CommaSeparatedListOutputParser to transform the returned string into a native Python list.
  • This pattern reduces brittle string processing and improves data reliability when integrating LLM responses into applications.

Quick reference table

Next steps

  • Try other output parsers (for example, JSON-specific parsers) if you need objects/dictionaries directly from the model.
  • Combine parser format instructions with few-shot examples when you need stronger conditioning.
  • Validate parsed outputs before using them in production systems to handle cases where the model doesn’t follow instructions exactly.
LLMs may sometimes ignore formatting instructions. Always validate parser output and add fallback handling for unexpected formats.

Watch Video