Comma-Separated List Output Parser in LangChain

https://python.langchain.com.cn/docs/modules/model_io/output_parsers/comma_separated

Comma-Separated List Output Parser in LangChain

This content is based on LangChain's official documentation (langchain.com.cn) and explains the CommaSeparatedListOutputParser---a tool to convert LLM outputs into comma-separated lists---in simplified terms. It strictly preserves original source codes, retains all knowledge points, and avoids arbitrary additions or modifications.

1. What is CommaSeparatedListOutputParser?

This output parser converts unstructured LLM responses into clean, comma-separated lists (Python lists).

  • Use case: When you need the LLM to return a list of items (e.g., ice cream flavors, book titles) and want to directly use the result as a Python list (no manual string splitting).
  • Key feature: It provides built-in format_instructions to guide the LLM to output comma-separated items, ensuring the parser can correctly parse the result.

2. Step 1: Import Required Modules

The code below imports all necessary classes---exactly as in the original documentation:

python 复制代码
from langchain.output_parsers import CommaSeparatedListOutputParser
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAI  # Included as in original import (even if not used in the example)

3. Step 2: Initialize the Output Parser

Create an instance of CommaSeparatedListOutputParser and get its format instructions (guidelines for the LLM to follow):

python 复制代码
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()  # Tells LLM to output comma-separated items

Note: The format_instructions automatically generated by the parser typically says: "Your response should be a list of comma-separated values. Do not include any additional text."

4. Step 3: Create a Prompt Template

Define a prompt template that includes the LLM task and the format instructions. This ensures the LLM outputs items in a comma-separated format:

python 复制代码
prompt = PromptTemplate(
    template="List five {subject}.\n{format_instructions}",
    input_variables=["subject"],  # Dynamic input (e.g., "ice cream flavors")
    partial_variables={"format_instructions": format_instructions}  # Fixed format guidelines
)

5. Step 4: Initialize the LLM and Generate Output

Use OpenAI (with temperature=0 for consistent results) to generate a response based on the formatted prompt:

python 复制代码
model = OpenAI(temperature=0)
_input = prompt.format(subject="ice cream flavors")  # Fill in the dynamic "subject"
output = model(_input)  # LLM generates comma-separated items

6. Step 5: Parse the LLM Output into a Python List

Use the output parser to convert the LLM's string output into a structured Python list. The original code and output are preserved exactly:

Code:

python 复制代码
output_parser.parse(output)

Output (exact as original):

python 复制代码
['Vanilla',
 'Chocolate',
 'Strawberry',
 'Mint Chocolate Chip',
 'Cookies and Cream']

Key Takeaways

  • CommaSeparatedListOutputParser simplifies converting LLM text outputs into usable Python lists.
  • get_format_instructions() ensures the LLM follows the correct output format (comma-separated items).
  • The prompt template combines the task (e.g., "List five ice cream flavors") and format guidelines for reliability.
  • Works with both LLMs (e.g., OpenAI) and chat models (e.g., ChatOpenAI)---the core logic remains the same.
相关推荐
Shi_haoliu19 分钟前
inno setup6.6.1实例,制作安装包,创建共享文件夹,写入注册表(提供给excel加载项,此文章解释iss文件)
前端·vue.js·windows·excel
nnsix33 分钟前
文件系统、分配单元大小 什么意思
windows
Boxsc_midnight1 小时前
【数字人学习之语音合成】Fun-CosyVoice3-0.5B-2512的windows系统中本地部署的方法
windows·学习·cosyvoice3
Coder_Boy_1 小时前
SpringAI与LangChain4j的智能应用-(实践篇4)
java·人工智能·spring boot·langchain
zhuzihuaile2 小时前
Langchain-Chatchat + Ollama + QWen3 + 搭建知识库 + AI-Win
人工智能·python·ai·langchain
Zfox_2 小时前
无缝穿越系统边界:节点小宝4.0如何让我的Mac/iOS像访问本地盘一样操控Windows
windows·macos·ios·节点小宝
嵌入式学习和实践2 小时前
Linux/Windows 系统架构查看、安装包选择指南(嵌入式开发场景适配)
linux·windows·系统架构
私人珍藏库2 小时前
[Windows] PDF 专业电子签章工具 v4.8
windows·pdf
一只蚊子03 小时前
C# WinForms配置Halcon
windows·c#·halcon
linksinke3 小时前
在windows系统上搭建Golang多版本管理器(g)的配置环境
开发语言·windows·golang