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)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')LCEL‚ÄãOutput parsers implement the Runnable interface, the basic building block of the 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
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.
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. ->: 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)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')LCEL‚ÄãOutput parsers implement the Runnable interface, the basic building block of the 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