Skip to content

Commit 2104cf0

Browse files
Copilotmdrxy
andauthored
fix: replace deprecated Pydantic .schema() calls with v1/v2 compatible pattern (#32162)
This PR addresses deprecation warnings users encounter when using LangChain tools with Pydantic v2: ``` PydanticDeprecatedSince20: The `schema` method is deprecated; use `model_json_schema` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. ``` ## Root Cause Several LangChain components were still using the deprecated `.schema()` method directly instead of the Pydantic v1/v2 compatible approach. While users calling `.schema()` on returned models will still see warnings (which is correct), LangChain's internal code should not generate these warnings. ## Changes Made Updated 3 files to use the standard compatibility pattern: ```python # Before (deprecated) schema = model.schema() # After (compatible with both v1 and v2) if hasattr(model, "model_json_schema"): schema = model.model_json_schema() # Pydantic v2 else: schema = model.schema() # Pydantic v1 ``` ### Files Updated: - **`evaluation/parsing/json_schema.py`**: Fixed `_parse_json()` method to handle Pydantic models correctly - **`output_parsers/yaml.py`**: Fixed `get_format_instructions()` to use compatible schema access - **`chains/openai_functions/citation_fuzzy_match.py`**: Fixed direct `.schema()` call on QuestionAnswer model ## Verification ✅ **Zero breaking changes** - all existing functionality preserved ✅ **No deprecation warnings** from LangChain internal code ✅ **Backward compatible** with Pydantic v1 ✅ **Forward compatible** with Pydantic v2 ✅ **Edge cases handled** (strings, plain objects, etc.) ## User Impact LangChain users will no longer see deprecation warnings from internal LangChain code. Users who directly call `.schema()` on schemas returned by LangChain should adopt the same compatibility pattern: ```python # User code should use this pattern input_schema = tool.get_input_schema() if hasattr(input_schema, "model_json_schema"): schema_result = input_schema.model_json_schema() else: schema_result = input_schema.schema() ``` Fixes #31458. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mdrxy <[email protected]> Co-authored-by: Mason Daugherty <[email protected]>
1 parent 18c64ae commit 2104cf0

File tree

4 files changed

+2997
-2983
lines changed

4 files changed

+2997
-2983
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ def create_citation_fuzzy_match_chain(llm: BaseLanguageModel) -> LLMChain:
123123
Chain (LLMChain) that can be used to answer questions with citations.
124124
"""
125125
output_parser = PydanticOutputFunctionsParser(pydantic_schema=QuestionAnswer)
126-
schema = QuestionAnswer.schema()
126+
if hasattr(QuestionAnswer, "model_json_schema"):
127+
schema = QuestionAnswer.model_json_schema()
128+
else:
129+
schema = QuestionAnswer.schema()
127130
function = {
128131
"name": schema["title"],
129132
"description": schema["description"],

libs/langchain/langchain/evaluation/parsing/json_schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ def evaluation_name(self) -> str:
7070
def _parse_json(self, node: Any) -> Union[dict, list, None, float, bool, int, str]:
7171
if isinstance(node, str):
7272
return parse_json_markdown(node)
73+
if hasattr(node, "model_json_schema") and callable(node.model_json_schema):
74+
# Pydantic v2 model
75+
return node.model_json_schema()
7376
if hasattr(node, "schema") and callable(node.schema):
74-
# Pydantic model
77+
# Pydantic v1 model
7578
return node.schema()
7679
return node
7780

libs/langchain/langchain/output_parsers/yaml.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ def parse(self, text: str) -> T:
4343

4444
def get_format_instructions(self) -> str:
4545
# Copy schema to avoid altering original Pydantic schema.
46-
schema = dict(self.pydantic_object.schema().items())
46+
if hasattr(self.pydantic_object, "model_json_schema"):
47+
# Pydantic v2
48+
schema = dict(self.pydantic_object.model_json_schema().items())
49+
elif hasattr(self.pydantic_object, "schema"):
50+
# Pydantic v1
51+
schema = dict(self.pydantic_object.schema().items())
52+
else:
53+
msg = "Pydantic object must have either model_json_schema or schema method"
54+
raise ValueError(msg)
4755

4856
# Remove extraneous fields.
4957
reduced_schema = schema

0 commit comments

Comments
 (0)