# 08. OutputFixingParser

## OutputFixingParser <a href="#outputfixingparser" id="outputfixingparser"></a>

`OutputFixingParser` Provides the ability to automatically fix errors that may occur during the output parsing process. This parser is basically another parser, for example `PydanticOutputParser` It is designed to fix the error through additional LLM calls when wrapping and returning an output or error in a format that this parser cannot handle.

At the heart of this approach, if the first attempt results in non-compliance with the schema, `OutputFixingParser` Is that it automatically recognizes the wrong output, and resubmits it to the model with a new command to fix it. In this process, the command for correction should pinpoint the error and include specific instructions to reconstruct the data in the correct format.

For example, `PydanticOutputParser` I tried using to generate output that complies with a specific data schema, but it may happen that some fields are missing or the data type is wrong. At this time `OutputFixingParser` As a next step, we submit a new request to LLM, including instructions to correct the error. LLM will generate a new output that fixes the error based on these instructions.

```
# API KEY를 환경변수로 관리하기 위한 설정 파일
from dotenv import load_dotenv

# API KEY 정보로드
load_dotenv()
```

```
True
```

```
from langchain_openai import ChatOpenAI
from langchain.output_parsers import PydanticOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
from typing import List


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)
```

```
# Deliberately enter incorrect format
misformatted = "{'name': 'Tom Hanks', 'film_names': ['Forrest Gump']}"

# Attempting to parse data entered in an incorrect format
parser.parse(misformatted)

# Error Output
```

`OutputFixingParser` I will use to correct the wrong format.

```
from langchain.output_parsers import OutputFixingParser

new_parser = OutputFixingParser.from_llm(parser=parser, llm=ChatOpenAI())
```

```
# Output in incorrect format
misformatted
```

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

```
# OutputFixingParser Parsing malformed output using
actor = new_parser.parse(misformatted)
```

```
# Parsed results
actor
```

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