*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to use output parsers to parse an LLM response into structured format

On this page

How to use output parsers to parse an LLM response into structured format
=========================================================================

Language models output text. But there are times where you want to get more structured information than just text back. While some model providers support [built-in ways to return structured output](/v0.2/docs/how_to/structured_output/), not all do.

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

*   "Get format instructions": A method which returns a string containing instructions for how the output of a language model should be formatted.
*   "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:

*   "Parse with prompt": 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[​](#get-started "Direct link to Get started")
---------------------------------------------------------

Below we go over the main type of output parser, the `PydanticOutputParser`.

    from langchain_core.output_parsers import PydanticOutputParserfrom langchain_core.prompts import PromptTemplatefrom langchain_core.pydantic_v1 import BaseModel, Field, validatorfrom langchain_openai import OpenAImodel = OpenAI(model_name="gpt-3.5-turbo-instruct", temperature=0.0)# Define your desired data structure.class Joke(BaseModel):    setup: str = Field(description="question to set up a joke")    punchline: str = Field(description="answer to resolve the joke")    # You can add custom validation logic easily with Pydantic.    @validator("setup")    def question_ends_with_question_mark(cls, field):        if field[-1] != "?":            raise ValueError("Badly formed question!")        return field# Set up a parser + inject instructions into the prompt template.parser = PydanticOutputParser(pydantic_object=Joke)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)# And a query intended to prompt a language model to populate the data structure.prompt_and_model = prompt | modeloutput = prompt_and_model.invoke({"query": "Tell me a joke."})parser.invoke(output)

**API Reference:**[PydanticOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.pydantic.PydanticOutputParser.html) | [PromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.prompt.PromptTemplate.html) | [OpenAI](https://api.python.langchain.com/en/latest/llms/langchain_openai.llms.base.OpenAI.html)

    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')

LCEL[​](#lcel "Direct link to LCEL")
------------------------------------

Output parsers implement the [Runnable interface](/v0.2/docs/concepts/#interface), the basic building block of the [LangChain Expression Language (LCEL)](/v0.2/docs/concepts/#langchain-expression-language-lcel). This means they support `invoke`, `ainvoke`, `stream`, `astream`, `batch`, `abatch`, `astream_log` calls.

Output parsers accept a string or `BaseMessage` as input and can return an arbitrary type.

    parser.invoke(output)

    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')

Instead of manually invoking the parser, we also could've just added it to our `Runnable` sequence:

    chain = prompt | model | parserchain.invoke({"query": "Tell me a joke."})

    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')

While all parsers support the streaming interface, only certain parsers can stream through partially parsed objects, since this is highly dependent on the output type. Parsers which cannot construct partial objects will simply yield the fully parsed output.

The `SimpleJsonOutputParser` for example can stream through partial outputs:

    from langchain.output_parsers.json import SimpleJsonOutputParserjson_prompt = PromptTemplate.from_template(    "Return a JSON object with an `answer` key that answers the following question: {question}")json_parser = SimpleJsonOutputParser()json_chain = json_prompt | model | json_parser

**API Reference:**[SimpleJsonOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.json.SimpleJsonOutputParser.html)

    list(json_chain.stream({"question": "Who invented the microscope?"}))

    [{}, {'answer': ''}, {'answer': 'Ant'}, {'answer': 'Anton'}, {'answer': 'Antonie'}, {'answer': 'Antonie van'}, {'answer': 'Antonie van Lee'}, {'answer': 'Antonie van Leeu'}, {'answer': 'Antonie van Leeuwen'}, {'answer': 'Antonie van Leeuwenho'}, {'answer': 'Antonie van Leeuwenhoek'}]

While the PydanticOutputParser cannot:

    list(chain.stream({"query": "Tell me a joke."}))

    [Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')]

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/output_parser_structured.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/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 run custom functions

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

Next

How to handle cases where no queries are generated

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

*   [Get started](#get-started)
*   [LCEL](#lcel)