*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to use the output-fixing parser

How to use the output-fixing parser
===================================

This output 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 typing import Listfrom langchain_core.output_parsers import PydanticOutputParserfrom langchain_core.pydantic_v1 import BaseModel, Fieldfrom langchain_openai import ChatOpenAI

**API Reference:**[PydanticOutputParser](https://api.python.langchain.com/en/latest/output_parsers/langchain_core.output_parsers.pydantic.PydanticOutputParser.html) | [ChatOpenAI](https://api.python.langchain.com/en/latest/chat_models/langchain_openai.chat_models.base.ChatOpenAI.html)

    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)

    misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"

    parser.parse(misformatted)

    ---------------------------------------------------------------------------``````outputJSONDecodeError                           Traceback (most recent call last)``````outputFile ~/workplace/langchain/libs/langchain/langchain/output_parsers/pydantic.py:29, in PydanticOutputParser.parse(self, text)     28     json_str = match.group()---> 29 json_object = json.loads(json_str, strict=False)     30 return self.pydantic_object.parse_obj(json_object)``````outputFile ~/.pyenv/versions/3.10.1/lib/python3.10/json/__init__.py:359, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)    358     kw['parse_constant'] = parse_constant--> 359 return cls(**kw).decode(s)``````outputFile ~/.pyenv/versions/3.10.1/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)    333 """Return the Python representation of ``s`` (a ``str`` instance    334 containing a JSON document).    335     336 """--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())    338 end = _w(s, end).end()``````outputFile ~/.pyenv/versions/3.10.1/lib/python3.10/json/decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)    352 try:--> 353     obj, end = self.scan_once(s, idx)    354 except StopIteration as err:``````outputJSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)``````outputDuring handling of the above exception, another exception occurred:``````outputOutputParserException                     Traceback (most recent call last)``````outputCell In[4], line 1----> 1 parser.parse(misformatted)``````outputFile ~/workplace/langchain/libs/langchain/langchain/output_parsers/pydantic.py:35, in PydanticOutputParser.parse(self, text)     33 name = self.pydantic_object.__name__     34 msg = f"Failed to parse {name} from completion {text}. Got: {e}"---> 35 raise OutputParserException(msg, llm_output=text)``````outputOutputParserException: Failed to parse Actor from completion {'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}. Got: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Now we can construct and use a `OutputFixingParser`. 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_parsers import OutputFixingParsernew_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())

**API Reference:**[OutputFixingParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.fix.OutputFixingParser.html)

    new_parser.parse(misformatted)

    Actor(name='Tom Hanks', film_names=['Forrest Gump'])

Find out api documentation for [OutputFixingParser](https://api.python.langchain.com/en/latest/output_parsers/langchain.output_parsers.fix.OutputFixingParser.html#langchain.output_parsers.fix.OutputFixingParser).

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/output_parser_fixing.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 create a custom Output Parser

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

Next

How to parse JSON output

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