Skip to main content

Output Parsers

Language models output text. But many times you may want to get more structured information than just text back. This is where output parsers come in.

Output parsers are classes that help structure language model responses. There are two main methods an output parser must implement:

  • getFormatInstructions(): str A method which returns a string containing instructions for how the output of a language model should be formatted.
  • parse(raw: string): any A method which takes in a string (assumed to be the response from a language model) and parses it into some structure.

And then one optional one:

  • parseWithPrompt(text: string, prompt: BasePromptValue): any: A method which takes in a string (assumed to be the response from a language model) and a formatted prompt (assumed to the prompt that generated such a response) and parses it into some structure. The prompt is largely provided in the event the OutputParser wants to retry or fix the output in some way, and needs information from the prompt to do so.

Below we go over some examples of output parsers.

Structured Output Parser

This output parser can be used when you want to return multiple fields.

import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { StructuredOutputParser } from "langchain/output_parsers";

export const run = async () => {
// With a `StructuredOutputParser` we can define a schema for the output.
const parser = StructuredOutputParser.fromNamesAndDescriptions({
answer: "answer to the user's question",
source: "source used to answer the user's question, should be a website.",
});

const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
template:
"Answer the users question as best as possible.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({
question: "What is the capital of France?",
});
const response = await model.call(input);

console.log(input);
/*
Answer the users question as best as possible.
The output should be a markdown code snippet formatted in the following schema:
```json
{
"answer": string // answer to the user's question
"source": string // source used to answer the user's question, should be a website.
}
```
*/

console.log(response);
/*
```json
{
"answer": "Paris",
"source": "https://en.wikipedia.org/wiki/France"
}
```
*/

console.log(parser.parse(response));
// { answer: 'Paris', source: 'https://en.wikipedia.org/wiki/France' }
};

Structured Output Parser with Zod Schema

This output parser can be also be used when you want to define the output schema using Zod, a TypeScript validation library. The Zod schema passed in needs be parseable from a JSON string, so eg. z.date() is not allowed, but z.coerce.date() is.

import { z } from "zod";
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { StructuredOutputParser } from "langchain/output_parsers";

export const run = async () => {
// We can use zod to define a schema for the output using the `fromZodSchema` method of `StructuredOutputParser`.
const parser = StructuredOutputParser.fromZodSchema(
z.object({
answer: z.string().describe("answer to the user's question"),
sources: z
.array(z.string())
.describe("sources used to answer the question, should be websites."),
})
);

const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
template:
"Answer the users question as best as possible.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({
question: "What is the capital of France?",
});
const response = await model.call(input);

console.log(input);
/*
Answer the users question as best as possible.
The output should be a markdown code snippet formatted in the following schema:
*/

console.log(response);
/*
```json
{
"answer": "The capital of France is Paris.",
"sources": ["https://www.worldatlas.com/articles/what-is-the-capital-of-france.html"]
}
```
*/

console.log(parser.parse(response));
/*
{
answer: 'The capital of France is Paris.',
sources: [
'https://www.worldatlas.com/articles/what-is-the-capital-of-france.html'
]
}
*/
};

Output Fixing Parser

This output parser wraps another output parser, and in the event that the first one fails it calls out to another LLM to fix any errors.

import { z } from "zod";
import { ChatOpenAI } from "langchain/chat_models/openai";
import {
StructuredOutputParser,
OutputFixingParser,
} from "langchain/output_parsers";

export const run = async () => {
const parser = StructuredOutputParser.fromZodSchema(
z.object({
answer: z.string().describe("answer to the user's question"),
sources: z
.array(z.string())
.describe("sources used to answer the question, should be websites."),
})
);
/** This is a bad output because sources is a string, not a list */
const badOutput = `\`\`\`json
{
"answer": "foo",
"sources": "foo.com"
}
\`\`\``;
try {
await parser.parse(badOutput);
} catch (e) {
console.log("Failed to parse bad output: ", e);
/*
Failed to parse bad output: OutputParserException [Error]: Failed to parse. Text: ```json
{
"answer": "foo",
"sources": "foo.com"
}
```. Error: [
{
"code": "invalid_type",
"expected": "array",
"received": "string",
"path": [
"sources"
],
"message": "Expected array, received string"
}
]
at StructuredOutputParser.parse (/Users/ankushgola/Code/langchainjs/langchain/src/output_parsers/structured.ts:71:13)
at run (/Users/ankushgola/Code/langchainjs/examples/src/prompts/fix_parser.ts:25:18)
at <anonymous> (/Users/ankushgola/Code/langchainjs/examples/src/index.ts:33:22)
*/
}
const fixParser = OutputFixingParser.fromLLM(
new ChatOpenAI({ temperature: 0 }),
parser
);
const output = await fixParser.parse(badOutput);
console.log("Fixed output: ", output);
// Fixed output: { answer: 'foo', sources: [ 'foo.com' ] }
};

Comma-separated List Parser

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

import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { CommaSeparatedListOutputParser } from "langchain/output_parsers";

export const run = async () => {
// With a `CommaSeparatedListOutputParser`, we can parse a comma separated list.
const parser = new CommaSeparatedListOutputParser();

const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
template: "List five {subject}.\n{format_instructions}",
inputVariables: ["subject"],
partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({ subject: "ice cream flavors" });
const response = await model.call(input);

console.log(input);
/*
List five ice cream flavors.
Your response should be a list of comma separated values, eg: `foo, bar, baz`
*/

console.log(response);
// Vanilla, Chocolate, Strawberry, Mint Chocolate Chip, Cookies and Cream

console.log(parser.parse(response));
/*
[
'Vanilla',
'Chocolate',
'Strawberry',
'Mint Chocolate Chip',
'Cookies and Cream'
]
*/
};