Skip to main content

Custom list parser

This output parser can be used when you want to return a list of items with a specific length and separator.

npm install @langchain/openai
import { OpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { CustomListOutputParser } from "@langchain/core/output_parsers";
import { RunnableSequence } from "@langchain/core/runnables";

// With a `CustomListOutputParser`, we can parse a list with a specific length and separator.
const parser = new CustomListOutputParser({ length: 3, separator: "\n" });

const chain = RunnableSequence.from([
"Provide a list of {subject}.\n{format_instructions}"
new OpenAI({ temperature: 0 }),

Provide a list of great fiction books (book, author).
Your response should be a list of 3 items separated by "\n" (eg: `foo\n bar\n baz`)
const response = await chain.invoke({
subject: "great fiction books (book, author)",
format_instructions: parser.getFormatInstructions(),

'The Catcher in the Rye, J.D. Salinger',
'To Kill a Mockingbird, Harper Lee',
'The Great Gatsby, F. Scott Fitzgerald'

API Reference:

Help us out by providing feedback on this documentation page: