Skip to content

The agent engineering platform.

fix(core): populate default args from tool's `args_schema` (#34399)

Nhan Nguyen398c067f3096
diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py
index c4fd234e48..d9ca7b0afa 100644
--- a/libs/core/langchain_core/tools/base.py
+++ b/libs/core/langchain_core/tools/base.py
@@ -654,6 +654,7 @@ class ChildTool(BaseTool):
TypeError: If `args_schema` is not a Pydantic `BaseModel` or dict.
"""
input_args = self.args_schema
+
if isinstance(tool_input, str):
if input_args is not None:
if isinstance(input_args, dict):
@@ -671,6 +672,7 @@ class ChildTool(BaseTool):
msg = f"args_schema must be a Pydantic BaseModel, got {input_args}"
raise TypeError(msg)
return tool_input
+
if input_args is not None:
if isinstance(input_args, dict):
return tool_input
@@ -711,9 +713,30 @@ class ChildTool(BaseTool):
f"args_schema must be a Pydantic BaseModel, got {self.args_schema}"
)
raise NotImplementedError(msg)
- validated_input = {
- k: getattr(result, k) for k in result_dict if k in tool_input
- }
+
+ # Include fields from tool_input, plus fields with explicit defaults.
+ # This applies Pydantic defaults (like Field(default=1)) while excluding
+ # synthetic "args"/"kwargs" fields that Pydantic creates for *args/**kwargs.
+ field_info = get_fields(input_args)
+ validated_input = {}
+ for k in result_dict:
+ if k in tool_input:
+ # Field was provided in input - include it (validated)
+ validated_input[k] = getattr(result, k)
+ elif k in field_info and k not in ("args", "kwargs"):
+ # Check if field has an explicit default defined in the schema.
+ # Exclude "args"/"kwargs" as these are synthetic fields for variadic
+ # parameters that should not be passed as keyword arguments.
+ fi = field_info[k]
+ # Pydantic v2 uses is_required() method, v1 uses required attribute
+ has_default = (
+ not fi.is_required()
+ if hasattr(fi, "is_required")
+ else not getattr(fi, "required", True)
+ )
+ if has_default:
+ validated_input[k] = getattr(result, k)
+
for k in self._injected_args_keys:
if k in tool_input:
validated_input[k] = tool_input[k]
@@ -728,7 +751,9 @@ class ChildTool(BaseTool):
)
raise ValueError(msg)
validated_input[k] = tool_call_id
+
return validated_input
+
return tool_input
@abstractmethod
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index f880555114..ec6b18869f 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -3345,3 +3345,131 @@ async def test_tool_call_id_passed_via_run_method(method: str) -> None:
assert handler.tool_starts == 1
assert len(handler.captured_tool_call_ids) == 1
assert handler.captured_tool_call_ids[0] == "run_method_tool_call_id"
+
+
+def test_tool_args_schema_default_values() -> None:
+ """Test that Pydantic default values from `args_schema` are applied.
+
+ When a tool has an `args_schema` with default values, those defaults
+ should be passed to the tool function when the caller omits them.
+ """
+
+ class SearchArgs(BaseModel):
+ """Schema for search tool arguments."""
+
+ query: str = Field(..., description="The search query")
+ page: int = Field(default=1, description="Page number")
+ size: int = Field(default=10, description="Results per page")
+
+ @tool("search", args_schema=SearchArgs)
+ def search_tool(query: str, page: int, size: int) -> str:
+ """Perform a search with pagination.
+
+ Args:
+ query: The search query.
+ page: Page number.
+ size: Results per page.
+ """
+ return f"query={query}, page={page}, size={size}"
+
+ # Invoke with only required argument - defaults should be applied
+ result = search_tool.invoke({"query": "test"})
+ assert result == "query=test, page=1, size=10"
+
+ # Invoke with partial defaults - mix of provided and default values
+ result = search_tool.invoke({"query": "test", "page": 5})
+ assert result == "query=test, page=5, size=10"
+
+ # Invoke with all arguments explicitly provided
+ result = search_tool.invoke({"query": "test", "page": 3, "size": 20})
+ assert result == "query=test, page=3, size=20"
+
+
+async def test_tool_args_schema_default_values_async() -> None:
+ """Test that Pydantic defaults work with async tool invocation."""
+
+ class SearchArgs(BaseModel):
+ """Schema for search tool arguments."""
+
+ query: str = Field(..., description="The search query")
+ limit: int = Field(default=5, description="Max results")
+
+ @tool("async_search", args_schema=SearchArgs)
+ async def async_search_tool(query: str, limit: int) -> str:
+ """Async search tool.
+
+ Args:
+ query: The search query.
+ limit: Max results.
+ """
+ return f"query={query}, limit={limit}"
+
+ # Invoke with only required argument - default should be applied
+ result = await async_search_tool.ainvoke({"query": "hello"})
+ assert result == "query=hello, limit=5"
+
+
+def test_tool_args_schema_none_default() -> None:
+ """Test that explicit `None` defaults are handled correctly.
+
+ When a field has `Field(default=None)`, that `None` value should be passed
+ to the tool function, not omitted from the arguments.
+ """
+
+ class FilterArgs(BaseModel):
+ """Schema for filter tool arguments."""
+
+ query: str = Field(..., description="The search query")
+ category: str | None = Field(default=None, description="Optional category")
+ tag: str | None = Field(default=None, description="Optional tag filter")
+
+ @tool("filter_search", args_schema=FilterArgs)
+ def filter_tool(query: str, category: str | None, tag: str | None) -> str:
+ """Search with optional filters.
+
+ Args:
+ query: The search query.
+ category: Optional category filter.
+ tag: Optional tag filter.
+ """
+ return f"query={query}, category={category}, tag={tag}"
+
+ # Invoke with only required argument - None defaults should be applied
+ result = filter_tool.invoke({"query": "test"})
+ assert result == "query=test, category=None, tag=None"
+
+ # Invoke with one optional provided
+ result = filter_tool.invoke({"query": "test", "category": "books"})
+ assert result == "query=test, category=books, tag=None"
+
+ # Invoke with all arguments
+ result = filter_tool.invoke({"query": "test", "category": "books", "tag": "new"})
+ assert result == "query=test, category=books, tag=new"
+
+
+def test_tool_args_schema_falsy_defaults() -> None:
+ """Test falsy default values (`0`, `False`, empty string) are handled correctly."""
+
+ class ConfigArgs(BaseModel):
+ """Schema for config tool arguments."""
+
+ name: str = Field(..., description="Config name")
+ enabled: bool = Field(default=False, description="Whether enabled")
+ count: int = Field(default=0, description="Initial count")
+ prefix: str = Field(default="", description="Optional prefix")
+
+ @tool("config_tool", args_schema=ConfigArgs)
+ def config_tool(name: str, *, enabled: bool, count: int, prefix: str) -> str:
+ """Configure settings.
+
+ Args:
+ name: Config name.
+ enabled: Whether enabled.
+ count: Initial count.
+ prefix: Optional prefix.
+ """
+ return f"name={name}, enabled={enabled}, count={count}, prefix={prefix!r}"
+
+ # Invoke with only required argument - falsy defaults should be applied
+ result = config_tool.invoke({"name": "test"})
+ assert result == "name=test, enabled=False, count=0, prefix=''"