Quick Start
Language models output text. But you may often 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()
: A method which returns a string containing instructions for how the output of a language model should be formatted. You can inject this into your prompt if necessary.parse()
: 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()
: A method which takes in a string (assumed to be the response from a language model) and a prompt (assumed to be 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.
Get started
Below we go over one useful type of output parser, the StructuredOutputParser
.
This output parser can be used when you want to return multiple fields.
Note: If you want complex schema returned (i.e. a JSON object with arrays of strings), you can use Zod Schema as detailed here.
- npm
- Yarn
- pnpm
npm install @langchain/openai
yarn add @langchain/openai
pnpm add @langchain/openai
import { OpenAI } from "@langchain/openai";
import { RunnableSequence } from "@langchain/core/runnables";
import { StructuredOutputParser } from "langchain/output_parsers";
import { PromptTemplate } from "@langchain/core/prompts";
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 chain = RunnableSequence.from([
PromptTemplate.fromTemplate(
"Answer the users question as best as possible.\n{format_instructions}\n{question}"
),
new OpenAI({ temperature: 0 }),
parser,
]);
console.log(parser.getFormatInstructions());
/*
Answer the users question as best as possible.
You must format your output as a JSON value that adheres to a given "JSON Schema" instance.
"JSON Schema" is a declarative language that allows you to annotate and validate JSON documents.
For example, the example "JSON Schema" instance {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings.
Thus, the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of this example "JSON Schema". The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.
Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas!
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
```
{"type":"object","properties":{"answer":{"type":"string","description":"answer to the user's question"},"sources":{"type":"array","items":{"type":"string"},"description":"sources used to answer the question, should be websites."}},"required":["answer","sources"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
```
What is the capital of France?
*/
const response = await chain.invoke({
question: "What is the capital of France?",
format_instructions: parser.getFormatInstructions(),
});
console.log(response);
// { answer: 'Paris', source: 'https://en.wikipedia.org/wiki/Paris' }
API Reference:
- OpenAI from
@langchain/openai
- RunnableSequence from
@langchain/core/runnables
- StructuredOutputParser from
langchain/output_parsers
- PromptTemplate from
@langchain/core/prompts
LCEL
Output parsers implement the Runnable interface, the basic building block of the LangChain Expression Language (LCEL).
This means they support invoke
, stream
, batch
, and streamLog
calls.
Output parsers accept model outputs (a string or BaseMessage
) as input and can return an arbitrary type. This is convenient for chaining as shown above.
await parser.invoke(`
\`\`\`json
{ answer: 'Paris', sources: [ 'https://en.wikipedia.org/wiki/Paris' ] }
\`\`\`
`);
// { answer: 'Paris', sources: [ 'https://en.wikipedia.org/wiki/Paris' ] }
While all parsers support the streaming interface, only certain parsers can stream through partially parsed objects as the model generates them, since this is highly dependent on the output type. Parsers which cannot construct partial objects will simply yield the fully parsed output.