Skip to content

The agent engineering platform.

fix(core): fix support of Pydantic v1 models in BaseTool.args (#32487)

Christophe Bornet5fd7962a78e7
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index 8c8857d79d..169d47bb82 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -546,6 +546,8 @@ class ChildTool(BaseTool):
"""
if isinstance(self.args_schema, dict):
json_schema = self.args_schema
+ elif self.args_schema and issubclass(self.args_schema, BaseModelV1):
+ json_schema = self.args_schema.schema()
else:
input_schema = self.get_input_schema()
json_schema = input_schema.model_json_schema()
diff --git a/libs/core/langchain_core/tools/simple.py b/libs/core/langchain_core/tools/simple.py
index 40c58e372e..b115150e1b 100644
--- a/libs/core/langchain_core/tools/simple.py
+++ b/libs/core/langchain_core/tools/simple.py
@@ -64,11 +64,7 @@ class Tool(BaseTool):
The input arguments for the tool.
"""
if self.args_schema is not None:
- if isinstance(self.args_schema, dict):
- json_schema = self.args_schema
- else:
- json_schema = self.args_schema.model_json_schema()
- return json_schema["properties"]
+ return super().args
# For backwards compatibility, if the function signature is ambiguous,
# assume it takes a single string input.
return {"tool_input": {"type": "string"}}
diff --git a/libs/core/langchain_core/tools/structured.py b/libs/core/langchain_core/tools/structured.py
index 08078e9277..e512c3742f 100644
--- a/libs/core/langchain_core/tools/structured.py
+++ b/libs/core/langchain_core/tools/structured.py
@@ -67,16 +67,6 @@ class StructuredTool(BaseTool):
# --- Tool ---
- @property
- def args(self) -> dict:
- """The tool's input arguments."""
- if isinstance(self.args_schema, dict):
- json_schema = self.args_schema
- else:
- input_schema = self.get_input_schema()
- json_schema = input_schema.model_json_schema()
- return json_schema["properties"]
-
def _run(
self,
*args: Any,
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index 63d9c30e46..ba32844201 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -1904,6 +1904,11 @@ def test_args_schema_as_pydantic(pydantic_model: Any) -> None:
name="some_tool", description="some description", args_schema=pydantic_model
)
+ assert tool.args == {
+ "a": {"title": "A", "type": "integer"},
+ "b": {"title": "B", "type": "string"},
+ }
+
input_schema = tool.get_input_schema()
if issubclass(input_schema, BaseModel):
input_json_schema = input_schema.model_json_schema()