*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to parse JSON output

On this page

How to parse JSON output
========================

While some model providers support [built-in ways to return structured output](/v0.2/docs/how_to/structured_output), not all do. We can use an output parser to help users to specify an arbitrary JSON schema via the prompt, query a model for outputs that conform to that schema, and finally parse that schema as JSON.

note

Keep in mind that large language models are leaky abstractions! You’ll have to use an LLM with sufficient capacity to generate well-formed JSON.

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Chat models](/v0.2/docs/concepts/#chat-models)
*   [Output parsers](/v0.2/docs/concepts/#output-parsers)
*   [Prompt templates](/v0.2/docs/concepts/#prompt-templates)
*   [Structured output](/v0.2/docs/how_to/structured_output)
*   [Chaining runnables together](/v0.2/docs/how_to/sequence/)

The [`JsonOutputParser`](https://v02.api.js.langchain.com/classes/langchain_core_output_parsers.JsonOutputParser.html) is one built-in option for prompting for and then parsing JSON output.

### Pick your chat model:

*   OpenAI
*   Anthropic
*   FireworksAI
*   MistralAI
*   Groq
*   VertexAI

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/openai 

    yarn add @langchain/openai 

    pnpm add @langchain/openai 

#### Add environment variables

    OPENAI_API_KEY=your-api-key

#### Instantiate the model

    import { ChatOpenAI } from "@langchain/openai";const model = new ChatOpenAI({  model: "gpt-3.5-turbo",  temperature: 0});

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/anthropic 

    yarn add @langchain/anthropic 

    pnpm add @langchain/anthropic 

#### Add environment variables

    ANTHROPIC_API_KEY=your-api-key

#### Instantiate the model

    import { ChatAnthropic } from "@langchain/anthropic";const model = new ChatAnthropic({  model: "claude-3-sonnet-20240229",  temperature: 0});

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/community 

    yarn add @langchain/community 

    pnpm add @langchain/community 

#### Add environment variables

    FIREWORKS_API_KEY=your-api-key

#### Instantiate the model

    import { ChatFireworks } from "@langchain/community/chat_models/fireworks";const model = new ChatFireworks({  model: "accounts/fireworks/models/firefunction-v1",  temperature: 0});

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/mistralai 

    yarn add @langchain/mistralai 

    pnpm add @langchain/mistralai 

#### Add environment variables

    MISTRAL_API_KEY=your-api-key

#### Instantiate the model

    import { ChatMistralAI } from "@langchain/mistralai";const model = new ChatMistralAI({  model: "mistral-large-latest",  temperature: 0});

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/groq 

    yarn add @langchain/groq 

    pnpm add @langchain/groq 

#### Add environment variables

    GROQ_API_KEY=your-api-key

#### Instantiate the model

    import { ChatGroq } from "@langchain/groq";const model = new ChatGroq({  model: "mixtral-8x7b-32768",  temperature: 0});

#### Install dependencies

tip

See [this section for general instructions on installing integration packages](/docs/get_started/installation#installing-integration-packages).

*   npm
*   yarn
*   pnpm

    npm i @langchain/google-vertexai 

    yarn add @langchain/google-vertexai 

    pnpm add @langchain/google-vertexai 

#### Add environment variables

    GOOGLE_APPLICATION_CREDENTIALS=credentials.json

#### Instantiate the model

    import { ChatVertexAI } from "@langchain/google-vertexai";const model = new ChatVertexAI({  model: "gemini-1.5-pro",  temperature: 0});

    import { ChatOpenAI } from "@langchain/openai";const model = new ChatOpenAI({  model: "gpt-4o",  temperature: 0,});import { JsonOutputParser } from "@langchain/core/output_parsers";import { ChatPromptTemplate } from "@langchain/core/prompts";// Define your desired data structure. Only used for typing the parser output.interface Joke {  setup: string;  punchline: string;}// A query and format instructions used to prompt a language model.const jokeQuery = "Tell me a joke.";const formatInstructions =  "Respond with a valid JSON object, containing two fields: 'setup' and 'punchline'.";// Set up a parser + inject instructions into the prompt template.const parser = new JsonOutputParser<Joke>();const prompt = ChatPromptTemplate.fromTemplate(  "Answer the user query.\n{format_instructions}\n{query}\n");const partialedPrompt = await prompt.partial({  format_instructions: formatInstructions,});const chain = partialedPrompt.pipe(model).pipe(parser);await chain.invoke({ query: jokeQuery });

    {  setup: "Why don't scientists trust atoms?",  punchline: "Because they make up everything!"}

Streaming[​](#streaming "Direct link to Streaming")
---------------------------------------------------

The `JsonOutputParser` also supports streaming partial chunks. This is useful when the model returns partial JSON output in multiple chunks. The parser will keep track of the partial chunks and return the final JSON output when the model finishes generating the output.

    for await (const s of await chain.stream({ query: jokeQuery })) {  console.log(s);}

    {}{ setup: "" }{ setup: "Why" }{ setup: "Why don't" }{ setup: "Why don't scientists" }{ setup: "Why don't scientists trust" }{ setup: "Why don't scientists trust atoms" }{ setup: "Why don't scientists trust atoms?", punchline: "" }{ setup: "Why don't scientists trust atoms?", punchline: "Because" }{  setup: "Why don't scientists trust atoms?",  punchline: "Because they"}{  setup: "Why don't scientists trust atoms?",  punchline: "Because they make"}{  setup: "Why don't scientists trust atoms?",  punchline: "Because they make up"}{  setup: "Why don't scientists trust atoms?",  punchline: "Because they make up everything"}{  setup: "Why don't scientists trust atoms?",  punchline: "Because they make up everything!"}

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You’ve now learned one way to prompt a model to return structured JSON. Next, check out the [broader guide on obtaining structured output](/v0.2/docs/how_to/structured_output) for other techniques.

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to try to fix errors in output parsing

](/v0.2/docs/how_to/output_parser_fixing)[

Next

How to parse XML output

](/v0.2/docs/how_to/output_parser_xml)

*   [Streaming](#streaming)
*   [Next steps](#next-steps)