Skip to main content
In this section, you will learn how to extract plain text and convert comma-separated string outputs from LLMs into Python list objects.

Objectives

  1. Understand why output parsers are required.
  2. Initialize and configure StrOutputParser to clean LLM response objects.
  3. Configure CommaSeparatedListOutputParser and feed format instructions to the LLM.

Invoking Parsers: .invoke() vs .parse()

When using LangChain output parsers step-by-step, you have two primary methods to run them:
  1. .invoke(response): Passes the entire model response object (like AIMessage). LangChain automatically extracts the raw string under the hood and parses it.
  2. .parse(raw_text): Passes a raw Python str (e.g., response.content). Use this when you have manually extracted the text or are parsing raw strings from external sources.
[!NOTE] In production LCEL chains, the pipe operator | automatically runs .invoke() behind the scenes, making .invoke() the standard approach for unified LangChain components.

Code Implementation

Each step of the implementation is preceded by extensive comments explaining the code logic.

Setup and Initialization

First, we load environment variables and initialize our chat model using core abstractions (Groq provider).

StrOutputParser

The simplest output parser. It extracts the raw string message content from the model’s chat response object.

CommaSeparatedListOutputParser

Instructs the model to return a list of items separated by commas, then parses that raw string into a standard Python list.

Practice & Exercises

To reinforce what you’ve learned in this section, practice with the interactive notebook:

Practice & Exercises

Practice extracting strings and comma-separated lists from LLM outputs.💻 VS Code | 🚀 Colab | 📥 Download