Skip to content

Conversation

@bhaskargurram-ai
Copy link

Description

Fixes #33646

When using the @tool decorator with args_schema set to a Pydantic BaseModel, injected parameters like ToolRuntime were being filtered out during input parsing, causing a TypeError: missing 1 required positional argument: 'runtime'.

Problem

The _parse_input() method in base.py was only returning fields that were both:

  1. Defined in the BaseModel schema
  2. Present in the original input

This caused injected arguments like ToolRuntime to be discarded, even though they were needed by the tool function.

Solution

Modified _parse_input() to preserve all fields from the original tool_input, not just those defined in the schema. After validating schema-defined fields, the method now adds back any additional fields that were in the original input. This ensures injected arguments are passed through to the tool function.

Before (Broken):

from pydantic import BaseModel
from langchain.tools import tool, ToolRuntime

class InputModel(BaseModel):
    query: str

@tool(args_schema=InputModel)
def my_tool(query: str, runtime: ToolRuntime):
    return f"{query}-{runtime.tool_call_id}"

# Raised: TypeError: my_tool() missing 1 required positional argument: 'runtime'
result = my_tool.invoke({"query": "hello", "runtime": runtime})

After (Fixed):

from pydantic import BaseModel
from langchain.tools import tool, ToolRuntime

class InputModel(BaseModel):
    query: str

@tool(args_schema=InputModel)
def my_tool(query: str, runtime: ToolRuntime):
    return f"{query}-{runtime.tool_call_id}"

# Works correctly! ✅
result = my_tool.invoke({"query": "hello", "runtime": runtime})
# Returns: "hello-test-123"

Changes

  • Modified: langchain_core/tools/base.py
    • _parse_input() method (lines 97-106)
    • After validating fields against the schema, preserve additional fields from original input
    • Maintains backward compatibility with existing tools

Testing

Manual testing performed with the following scenarios:

  1. ✅ Tool with BaseModel + ToolRuntime (previously failed, now works)

    • Verified that ToolRuntime is correctly passed to the function
  2. ✅ Tool without BaseModel + ToolRuntime (regression test - still works)

    • Confirmed no impact on tools using Annotated types
  3. ✅ Tool with BaseModel but no injected arguments*(regression test - still works)

    • Verified standard BaseModel validation still functions correctly

All tests pass successfully with the fix applied.

Issue

Fixes #33646

Dependencies

None - this is a bug fix using existing functionality.

When args_schema is defined as a Pydantic BaseModel, injected parameters
like ToolRuntime were being filtered out. This fix preserves all fields
from the original input, even those not defined in the schema.

Fixes langchain-ai#33646
@github-actions github-actions bot added core Related to the package `langchain-core` fix labels Oct 23, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 23, 2025

CodSpeed Performance Report

Merging #33649 will improve performances by 17.53%

Comparing bhaskargurram-ai:fix/tool-runtime-with-basemodel-33646 (f0d4343) with master (ef85161)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

⚡ 6 improvements
✅ 7 untouched
⏩ 21 skipped1

Benchmarks breakdown

Mode Benchmark BASE HEAD Change
WallTime test_import_time[BaseChatModel] 605.6 ms 515.3 ms +17.53%
WallTime test_import_time[CallbackManager] 523.4 ms 463.5 ms +12.93%
WallTime test_import_time[InMemoryVectorStore] 692.1 ms 621.5 ms +11.36%
WallTime test_import_time[LangChainTracer] 493.1 ms 439.2 ms +12.29%
WallTime test_import_time[PydanticOutputParser] 604.2 ms 521.6 ms +15.83%
WallTime test_import_time[Runnable] 558.5 ms 495.9 ms +12.62%

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to the package `langchain-core` fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runtime is not passed to tool function if the args_schema is defined with Pydantic BaseModel

1 participant