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.
相关推荐
虫师c2 小时前
List vs Set:深入剖析Java两大集合的核心区别与实战应用
java·数据结构·list·set·java集合
ayaya_mana5 小时前
Linux告别搜索卡顿:解决“Argument list too long”与实现文件内容秒搜
linux·运维·list
百***58146 小时前
Windows操作系统部署Tomcat详细讲解
java·windows·tomcat
IDOlaoluo8 小时前
Windows系统调校_20250408_36367c06.exe安装教程(附详细步骤)
windows
视觉震撼11 小时前
RDP登录事件详细溯源分析脚本(兼容Windows PowerShell版本)
运维·网络·windows·网络安全·网络攻击模型·安全威胁分析·安全架构
井上泷奈11 小时前
Win键失效解决方法
windows·经验分享·其他
网硕互联的小客服12 小时前
Windows2008 如何禁用FSO?
运维·服务器·网络·windows·安全
不惑_12 小时前
[特殊字符] 在 Windows 上设置 SQLite
数据库·windows·sqlite
IT古董20 小时前
Windows 11 专业版 安装与配置 Docker Desktop 保姆级手册(包成功永久免关注免VIP)
windows·docker·容器