PydanticOutputParser Class — langchain Architecture
Architecture documentation for the PydanticOutputParser class in pydantic.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 570792b6_910c_5259_8419_5183ac1b1409["PydanticOutputParser"] 0d9a7db1_a024_a63e_d6da_4108a6cd2019["JsonOutputParser"] 570792b6_910c_5259_8419_5183ac1b1409 -->|extends| 0d9a7db1_a024_a63e_d6da_4108a6cd2019 00fb336c_8832_626d_1bf9_f4f07a04d663["pydantic.py"] 570792b6_910c_5259_8419_5183ac1b1409 -->|defined in| 00fb336c_8832_626d_1bf9_f4f07a04d663 fd96632b_d191_0274_eb13_17083eb19c1d["_parse_obj()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| fd96632b_d191_0274_eb13_17083eb19c1d be3b196e_4a79_f447_3f29_9d15eee5575b["_parser_exception()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| be3b196e_4a79_f447_3f29_9d15eee5575b 832ab0e8_f54d_dc1d_0342_eccf5c13bd0d["parse_result()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| 832ab0e8_f54d_dc1d_0342_eccf5c13bd0d 72590bc9_9c62_cc76_ee87_a0199ef2338a["parse()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| 72590bc9_9c62_cc76_ee87_a0199ef2338a 6bf20b4d_d595_a2e8_9a0a_c828bb0760ad["get_format_instructions()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| 6bf20b4d_d595_a2e8_9a0a_c828bb0760ad 94c3b392_5e98_e89a_9d75_c0205476adf6["_type()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| 94c3b392_5e98_e89a_9d75_c0205476adf6 d8e29480_6c2f_f821_c933_dec496874a87["OutputType()"] 570792b6_910c_5259_8419_5183ac1b1409 -->|method| d8e29480_6c2f_f821_c933_dec496874a87
Relationship Graph
Source Code
libs/core/langchain_core/output_parsers/pydantic.py lines 19–121
class PydanticOutputParser(JsonOutputParser, Generic[TBaseModel]):
"""Parse an output using a Pydantic model."""
pydantic_object: Annotated[type[TBaseModel], SkipValidation()]
"""The Pydantic model to parse."""
def _parse_obj(self, obj: dict) -> TBaseModel:
try:
if issubclass(self.pydantic_object, pydantic.BaseModel):
return self.pydantic_object.model_validate(obj)
if issubclass(self.pydantic_object, pydantic.v1.BaseModel):
return self.pydantic_object.parse_obj(obj)
msg = f"Unsupported model version for PydanticOutputParser: \
{self.pydantic_object.__class__}"
raise OutputParserException(msg)
except (pydantic.ValidationError, pydantic.v1.ValidationError) as e:
raise self._parser_exception(e, obj) from e
def _parser_exception(
self, e: Exception, json_object: dict
) -> OutputParserException:
json_string = json.dumps(json_object, ensure_ascii=False)
name = self.pydantic_object.__name__
msg = f"Failed to parse {name} from completion {json_string}. Got: {e}"
return OutputParserException(msg, llm_output=json_string)
@overload
def parse_result(
self, result: list[Generation], *, partial: Literal[False] = False
) -> TBaseModel: ...
@overload
def parse_result(
self, result: list[Generation], *, partial: bool = False
) -> TBaseModel | None: ...
def parse_result(
self, result: list[Generation], *, partial: bool = False
) -> TBaseModel | None:
"""Parse the result of an LLM call to a Pydantic object.
Args:
result: The result of the LLM call.
partial: Whether to parse partial JSON objects.
If `True`, the output will be a JSON object containing all the keys that
have been returned so far.
Raises:
OutputParserException: If the result is not valid JSON or does not conform
to the Pydantic model.
Returns:
The parsed Pydantic object.
"""
try:
json_object = super().parse_result(result)
return self._parse_obj(json_object)
except OutputParserException:
if partial:
return None
raise
def parse(self, text: str) -> TBaseModel:
"""Parse the output of an LLM call to a Pydantic object.
Args:
text: The output of the LLM call.
Returns:
The parsed Pydantic object.
"""
return self.parse_result([Generation(text=text)])
def get_format_instructions(self) -> str:
"""Return the format instructions for the JSON output.
Returns:
The format instructions for the JSON output.
"""
# Copy schema to avoid altering original Pydantic schema.
Domain
Extends
Source
Frequently Asked Questions
What is the PydanticOutputParser class?
PydanticOutputParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/pydantic.py.
Where is PydanticOutputParser defined?
PydanticOutputParser is defined in libs/core/langchain_core/output_parsers/pydantic.py at line 19.
What does PydanticOutputParser extend?
PydanticOutputParser extends JsonOutputParser.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free