File tree Expand file tree Collapse file tree 4 files changed +15
-21
lines changed Expand file tree Collapse file tree 4 files changed +15
-21
lines changed Original file line number Diff line number Diff line change @@ -135,10 +135,7 @@ def create_citation_fuzzy_match_chain(llm: BaseLanguageModel) -> LLMChain:
135
135
Chain (LLMChain) that can be used to answer questions with citations.
136
136
"""
137
137
output_parser = PydanticOutputFunctionsParser (pydantic_schema = QuestionAnswer )
138
- if hasattr (QuestionAnswer , "model_json_schema" ):
139
- schema = QuestionAnswer .model_json_schema ()
140
- else :
141
- schema = QuestionAnswer .schema ()
138
+ schema = QuestionAnswer .model_json_schema ()
142
139
function = {
143
140
"name" : schema ["title" ],
144
141
"description" : schema ["description" ],
Original file line number Diff line number Diff line change @@ -79,10 +79,7 @@ def create_qa_with_structure_chain(
79
79
)
80
80
raise ValueError (msg )
81
81
if isinstance (schema , type ) and is_basemodel_subclass (schema ):
82
- if hasattr (schema , "model_json_schema" ):
83
- schema_dict = cast ("dict" , schema .model_json_schema ())
84
- else :
85
- schema_dict = cast ("dict" , schema .schema ())
82
+ schema_dict = cast ("dict" , schema .model_json_schema ())
86
83
else :
87
84
schema_dict = cast ("dict" , schema )
88
85
function = {
Original file line number Diff line number Diff line change @@ -34,9 +34,7 @@ def parse(self, text: str) -> T:
34
34
yaml_str = match .group ("yaml" ) if match else text
35
35
36
36
json_object = yaml .safe_load (yaml_str )
37
- if hasattr (self .pydantic_object , "model_validate" ):
38
- return self .pydantic_object .model_validate (json_object )
39
- return self .pydantic_object .parse_obj (json_object )
37
+ return self .pydantic_object .model_validate (json_object )
40
38
41
39
except (yaml .YAMLError , ValidationError ) as e :
42
40
name = self .pydantic_object .__name__
@@ -46,15 +44,7 @@ def parse(self, text: str) -> T:
46
44
@override
47
45
def get_format_instructions (self ) -> str :
48
46
# Copy schema to avoid altering original Pydantic schema.
49
- if hasattr (self .pydantic_object , "model_json_schema" ):
50
- # Pydantic v2
51
- schema = dict (self .pydantic_object .model_json_schema ().items ())
52
- elif hasattr (self .pydantic_object , "schema" ):
53
- # Pydantic v1
54
- schema = dict (self .pydantic_object .schema ().items ())
55
- else :
56
- msg = "Pydantic object must have either model_json_schema or schema method"
57
- raise ValueError (msg )
47
+ schema = dict (self .pydantic_object .model_json_schema ().items ())
58
48
59
49
# Remove extraneous fields.
60
50
reduced_schema = schema
Original file line number Diff line number Diff line change 1
- from langchain_core .utils .pydantic import get_pydantic_major_version
1
+ from langchain_core .utils .pydantic import PYDANTIC_VERSION
2
+
3
+
4
+ def get_pydantic_major_version () -> int :
5
+ """Get the major version of Pydantic.
6
+
7
+ Returns:
8
+ The major version of Pydantic.
9
+ """
10
+ return PYDANTIC_VERSION .major
11
+
2
12
3
13
__all__ = ["get_pydantic_major_version" ]
You can’t perform that action at this time.
0 commit comments