How to use the output-fixing parser
Thisoutput parser wraps another output parser, and in the event that the first one fails it calls out to another LLM to fix any errors.
But we can do other things besides throw errors. Specifically, we can pass the misformatted output, along with the formatted instructions, to the model and ask it to fix it.
For this example, we'll use the above Pydantic output parser. Here's what happens if we pass it a result that does not comply with the schema:
from typingimport List
from langchain_core.exceptionsimport OutputParserException
from langchain_core.output_parsersimport PydanticOutputParser
from langchain_openaiimport ChatOpenAI
from pydanticimport BaseModel, Field
classActor(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)
misformatted="{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"
try:
parser.parse(misformatted)
except OutputParserExceptionas e:
print(e)
Invalid json output: {'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE
Now we can construct and use aOutputFixingParser
. This output parser takes as an argument another output parser but also an LLM with which to try to correct any formatting mistakes.
from langchain.output_parsersimport OutputFixingParser
new_parser= OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())
API Reference:OutputFixingParser
new_parser.parse(misformatted)
Actor(name='Tom Hanks', film_names=['Forrest Gump'])
Find out api documentation forOutputFixingParser.