Skip to main content

CSV parser

This output parser can be used when you want to return a list of comma-separated items.

from langchain.output_parsers import CommaSeparatedListOutputParser
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

output_parser = CommaSeparatedListOutputParser()

format_instructions = output_parser.get_format_instructions()
prompt = PromptTemplate(
template="List five {subject}.\n{format_instructions}",
partial_variables={"format_instructions": format_instructions},

model = ChatOpenAI(temperature=0)

chain = prompt | model | output_parser
chain.invoke({"subject": "ice cream flavors"})
'Mint Chocolate Chip',
'Cookies and Cream']
for s in{"subject": "ice cream flavors"}):
['Mint Chocolate Chip']
['Cookies and Cream']