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()},)_input = prompt.format_prompt(query=joke_query)output = model(_input.to_string())parser.parse(output)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')# Here's another example, but with a compound typed field.class Actor(BaseModel):    name: str = Field(description="name of an actor")    film_names: List[str] = Field(description="list of names of films they starred in")actor_query = "Generate the filmography for a random actor."parser = PydanticOutputParser(pydantic_object=Actor)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)_input = prompt.format_prompt(query=actor_query)output = model(_input.to_string())parser.parse(output)    Actor(name='Tom Hanks', film_names=['Forrest Gump', 'Saving Private Ryan', 'The Green Mile', 'Cast Away', 'Toy Story'])PreviousAuto-fixing parserNextRetry parserCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.
This output parser allows users to specify an arbitrary JSON schema and query LLMs for JSON outputs that conform to that schema.
This output parser allows users to specify an arbitrary JSON schema and query LLMs for JSON outputs that conform to that schema. ->: 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()},)_input = prompt.format_prompt(query=joke_query)output = model(_input.to_string())parser.parse(output)    Joke(setup='Why did the chicken cross the road?', punchline='To get to the other side!')# Here's another example, but with a compound typed field.class Actor(BaseModel):    name: str = Field(description="name of an actor")    film_names: List[str] = Field(description="list of names of films they starred in")actor_query = "Generate the filmography for a random actor."parser = PydanticOutputParser(pydantic_object=Actor)prompt = PromptTemplate(    template="Answer the user query.\n{format_instructions}\n{query}\n",    input_variables=["query"],    partial_variables={"format_instructions": parser.get_format_instructions()},)_input = prompt.format_prompt(query=actor_query)output = model(_input.to_string())parser.parse(output)    Actor(name='Tom Hanks', film_names=['Forrest Gump', 'Saving Private Ryan', 'The Green Mile', 'Cast Away', 'Toy Story'])PreviousAuto-fixing parserNextRetry parserCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.