fix: allow standalone .invoke() on tools with ToolRuntime parameter#7227
Open
Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:mainfrom
Open
fix: allow standalone .invoke() on tools with ToolRuntime parameter#7227Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:mainfrom
Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
When a @tool-decorated function has a `runtime: ToolRuntime` parameter,
calling `.invoke({})` standalone (outside a LangGraph graph) fails because
Pydantic requires the `runtime` field and langchain-core's `_parse_input`
does not provide a default for injected arguments.
This commit fixes the issue with two changes:
1. Add `__get_pydantic_core_schema__` to `ToolRuntime` so that Pydantic
treats it as an optional field with a `None` default during schema
validation. This handles the bare `ToolRuntime` type annotation.
2. Patch `BaseTool._parse_input` at import time to handle injected
arguments (ToolRuntime, InjectedState, InjectedStore) gracefully:
- When all injected args are present (ToolNode path), delegate to
the original implementation unchanged.
- When injected args are missing (standalone path), validate using
`tool_call_schema` (which excludes injected fields) and re-add
the injected keys with `None` defaults.
This enables the following usage pattern for unit testing and standalone
tool invocation:
@tool
def my_tool(x: int, runtime: ToolRuntime) -> str:
if runtime is not None:
# running inside a graph
...
# standalone path
return str(x)
my_tool.invoke({"x": 42}) # works now
Fixes langchain-ai#7222
Signed-off-by: Tan <alvinttang@gmail.com>
Contributor
|
This PR has been automatically closed because you are not assigned to the linked issue. External contributors must be assigned to an issue before opening a PR for it. Please:
Maintainers: reopen this PR or remove the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #7222
When a
@tool-decorated function has aruntime: ToolRuntimeparameter, calling.invoke({})standalone (outside of a LangGraph graph) fails with a PydanticValidationErrorbecauseruntimeis treated as a required input field.Root cause: langchain-core's
_parse_inputvalidates tool input against the fullargs_schema(which includesToolRuntimeas a required field) and does not provide defaults for injected arguments when they are absent.Fix (two parts):
ToolRuntime.__get_pydantic_core_schema__— makes Pydantic treatToolRuntimeas optional with aNonedefault during schema validation. Handles the bareToolRuntimetype annotation.Patch
BaseTool._parse_input— attool_node.pyimport time, wraps the original method to handle injected arguments (ToolRuntime,InjectedState,InjectedStore) gracefully:tool_call_schema(which already excludes injected fields) and re-adds injected keys withNonedefaults.Before:
After:
Test plan
.invoke({})withToolRuntimeparameter succeeds (returnsruntime=None).invoke()withToolRuntime+ other args worksToolRuntime[MyContext](generic) standalone invocation works.ainvoke()workstool_call_schemastill correctly excludesruntimetest_tool_node.pytests passtest_tool_node_validation_error_filtering.pytests pass🤖 Generated with Claude Code