11"""
22Functions to retrieve the correct output parser and format instructions for the LLM model.
33"""
4+ from typing import Union , Dict , Any , Type , Callable
45from pydantic import BaseModel as BaseModelV2
56from pydantic .v1 import BaseModel as BaseModelV1
6- from typing import Union , Dict , Any , Type , Callable
77from langchain_core .output_parsers import JsonOutputParser
88
9- def get_structured_output_parser (schema : Union [Dict [str , Any ], Type [BaseModelV1 | BaseModelV2 ], Type ]) -> Callable :
9+ def get_structured_output_parser (schema : Union [Dict [str , Any ],
10+ Type [BaseModelV1 | BaseModelV2 ], Type ]) -> Callable :
1011 """
1112 Get the correct output parser for the LLM model.
1213
@@ -15,7 +16,7 @@ def get_structured_output_parser(schema: Union[Dict[str, Any], Type[BaseModelV1
1516 """
1617 if issubclass (schema , BaseModelV1 ):
1718 return _base_model_v1_output_parser
18-
19+
1920 if issubclass (schema , BaseModelV2 ):
2021 return _base_model_v2_output_parser
2122
@@ -29,12 +30,14 @@ def get_pydantic_output_parser(schema: Union[Dict[str, Any], Type[BaseModelV1 |
2930 JsonOutputParser: The output parser object.
3031 """
3132 if issubclass (schema , BaseModelV1 ):
32- raise ValueError ("pydantic.v1 and langchain_core.pydantic_v1 are not supported with this LLM model. Please use pydantic v2 instead." )
33-
33+ raise ValueError ("""pydantic.v1 and langchain_core.pydantic_v1
34+ are not supported with this LLM model. Please use pydantic v2 instead.""" )
35+
3436 if issubclass (schema , BaseModelV2 ):
3537 return JsonOutputParser (pydantic_object = schema )
3638
37- raise ValueError ("The schema is not a pydantic subclass. With this LLM model you must use a pydantic schemas." )
39+ raise ValueError ("""The schema is not a pydantic subclass.
40+ With this LLM model you must use a pydantic schemas.""" )
3841
3942def _base_model_v1_output_parser (x : BaseModelV1 ) -> dict :
4043 """
@@ -47,16 +50,15 @@ def _base_model_v1_output_parser(x: BaseModelV1) -> dict:
4750 dict: The parsed output.
4851 """
4952 work_dict = x .dict ()
50-
51- # recursive dict parser
53+
5254 def recursive_dict_parser (work_dict : dict ) -> dict :
5355 dict_keys = work_dict .keys ()
5456 for key in dict_keys :
5557 if isinstance (work_dict [key ], BaseModelV1 ):
5658 work_dict [key ] = work_dict [key ].dict ()
5759 recursive_dict_parser (work_dict [key ])
5860 return work_dict
59-
61+
6062 return recursive_dict_parser (work_dict )
6163
6264
0 commit comments