[docs]classPydanticOutputParser(JsonOutputParser,Generic[TBaseModel]):"""Parse an output using a pydantic model."""pydantic_object:Type[TBaseModel]# type: ignore"""The pydantic model to parse."""def_parse_obj(self,obj:dict)->TBaseModel:ifPYDANTIC_MAJOR_VERSION==2:try:ifissubclass(self.pydantic_object,pydantic.BaseModel):returnself.pydantic_object.model_validate(obj)elifissubclass(self.pydantic_object,pydantic.v1.BaseModel):returnself.pydantic_object.parse_obj(obj)else:raiseOutputParserException(f"Unsupported model version for PydanticOutputParser: \{self.pydantic_object.__class__}")except(pydantic.ValidationError,pydantic.v1.ValidationError)ase:raiseself._parser_exception(e,obj)fromeelse:# pydantic v1try:returnself.pydantic_object.parse_obj(obj)exceptpydantic.ValidationErrorase:raiseself._parser_exception(e,obj)fromedef_parser_exception(self,e:Exception,json_object:dict)->OutputParserException:json_string=json.dumps(json_object)name=self.pydantic_object.__name__msg=f"Failed to parse {name} from completion {json_string}. Got: {e}"returnOutputParserException(msg,llm_output=json_string)
[docs]defparse_result(self,result:List[Generation],*,partial:bool=False)->Optional[TBaseModel]:"""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. Defaults to False. Returns: The parsed pydantic object. """try:json_object=super().parse_result(result)returnself._parse_obj(json_object)exceptOutputParserExceptionase:ifpartial:returnNoneraisee
[docs]defparse(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. """returnsuper().parse(text)
[docs]defget_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.schema={k:vfork,vinself.pydantic_object.schema().items()}# Remove extraneous fields.reduced_schema=schemaif"title"inreduced_schema:delreduced_schema["title"]if"type"inreduced_schema:delreduced_schema["type"]# Ensure json in context is well-formed with double quotes.schema_str=json.dumps(reduced_schema,ensure_ascii=False)return_PYDANTIC_FORMAT_INSTRUCTIONS.format(schema=schema_str)
@propertydef_type(self)->str:return"pydantic"@propertydefOutputType(self)->Type[TBaseModel]:"""Return the pydantic model."""returnself.pydantic_object
_PYDANTIC_FORMAT_INSTRUCTIONS="""The output should be formatted as a JSON instance that conforms to the JSON schema below.As an example, for the schema {{"properties": {{"foo": {{"title": "Foo", "description": "a list of strings", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}the object {{"foo": ["bar", "baz"]}} is a well-formatted instance of the schema. The object {{"properties": {{"foo": ["bar", "baz"]}}}} is not well-formatted.Here is the output schema:```{schema}```"""# noqa: E501# Re-exporting types for backwards compatibility__all__=["PydanticBaseModel","PydanticOutputParser","TBaseModel",]