Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/docs/concepts/structured_outputs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,20 @@ ResponseFormatter(answer="The powerhouse of the cell is the mitochondrion. Mitoc
For more details on usage, see our [how-to guide](/docs/how_to/structured_output/#the-with_structured_output-method).

:::

## Handling parsing errors

In production, LLMs may return malformed outputs. While `with_structured_output()` doesn't provide automatic retry on parsing errors, you can use `OutputFixingParser` for this:

```python
from langchain.output_parsers import OutputFixingParser
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI we're not including this in langchain_v1 yet. We'll likely need to figure out how to have the same functionality exposed in v1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eyurtsev what is the recommended approach in V1? with_structured_output?


# Create a robust parser with automatic retry
base_parser = PydanticOutputParser(pydantic_object=ResponseFormatter)
robust_parser = OutputFixingParser.from_llm(parser=base_parser, llm=model)

# Automatically fixes parsing errors by asking the LLM to correct malformed outputs
structured_output = robust_parser.parse(llm_output)
```

This works with both `PydanticOutputParser` and `JsonOutputParser`. For cases requiring the original prompt context, see the [RetryOutputParser guide](/docs/how_to/output_parser_retry).
Loading