Skip to content

Commit a5137b0

Browse files
authored
refactor(langchain): resolve pydantic deprecation warnings (#33125)
1 parent 5bea283 commit a5137b0

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

libs/langchain/langchain/chains/openai_functions/citation_fuzzy_match.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ def create_citation_fuzzy_match_chain(llm: BaseLanguageModel) -> LLMChain:
135135
Chain (LLMChain) that can be used to answer questions with citations.
136136
"""
137137
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()
142139
function = {
143140
"name": schema["title"],
144141
"description": schema["description"],

libs/langchain/langchain/chains/openai_functions/qa_with_structure.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ def create_qa_with_structure_chain(
7979
)
8080
raise ValueError(msg)
8181
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())
8683
else:
8784
schema_dict = cast("dict", schema)
8885
function = {

libs/langchain/langchain/output_parsers/yaml.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ def parse(self, text: str) -> T:
3434
yaml_str = match.group("yaml") if match else text
3535

3636
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)
4038

4139
except (yaml.YAMLError, ValidationError) as e:
4240
name = self.pydantic_object.__name__
@@ -46,15 +44,7 @@ def parse(self, text: str) -> T:
4644
@override
4745
def get_format_instructions(self) -> str:
4846
# 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())
5848

5949
# Remove extraneous fields.
6050
reduced_schema = schema
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
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+
212

313
__all__ = ["get_pydantic_major_version"]

0 commit comments

Comments
 (0)