Skip to content

fix(prebuilt): support generic type arguments for ToolRuntime injection - #6509

Merged
Sydney Runkle (sydney-runkle) merged 5 commits into
langchain-ai:mainfrom
dumko2001:fix/tool-runtime-generic
Jan 12, 2026
Merged

Sydney Runkle (sydney-runkle) merged 5 commits into
langchain-ai:mainfrom
dumko2001:fix/tool-runtime-generic

Conversation

@dumko2001

Copy link
Copy Markdown
Contributor

Description:
This PR fixes an issue where injection types (like ToolRuntime) were not recognized by ToolNode when used with generic type arguments (e.g., ToolRuntime[MyContext]).

Previously, the _is_injection check relied solely on isinstance and issubclass, which fail for typing._GenericAlias objects. This update adds a check using typing.get_origin() to correctly identify the base class of generic types, ensuring the runtime is injected correctly even when type hints are present.

Issue: Fixes #6465

Dependencies: None

Twitter handle: @SidharthRajmoh2

@vercel

vercel Bot commented Nov 27, 2025

Copy link
Copy Markdown

Sidharth Rajmohan (@dumko2001) is attempting to deploy a commit to the LangChain Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much. This is consistent w/ the logic we have for checking the origin in langchain-core as well.

It's actually a bit odd that these functions aren't unified.

@sydney-runkle
Sydney Runkle (sydney-runkle) merged commit cb2faa7 into langchain-ai:main Jan 12, 2026
56 checks passed
@dumko2001

Sidharth Rajmohan (dumko2001) commented Jan 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Welcome Sydney! . I’m already digging into the core for some other PRs (#6511 , #6512 ), so I’m happy to take a stab at cleaning this up. Let me know if you'd like me to open a PR for it!

@Sourav-Nandy-ai

Copy link
Copy Markdown

Hi Sidharth,

I ran a real before-and-after test on this. First, I caught an error in my own notes. I had this PR's merge date wrong (I had 2025-11-27, which is actually when it was opened; it merged on 2026-01-12). I corrected this before testing.

I installed real langgraph-prebuilt==1.0.5 (pre-fix) and 1.0.6 (post-fix, the actual first release with your fix). I then built a tool with a ToolRuntime[MyContext]-annotated parameter and dispatched it through a real ToolNode.invoke() call, running it 10 times for each version.

The Results

  • Pre-fix: Every single call fails with a real rt: Field required validation error. The generic-parameterized annotation is not recognized, so ToolNode tries to treat it as an LLM-supplied argument.
  • Post-fix: Every call succeeds, and the runtime object is correctly injected.

I confirmed this at the source level as well. The _is_injection() function in 1.0.5 has no generic-origin branch, whereas 1.0.6 adds exactly the check needed for get_origin(ToolRuntime[MyContext]) to match.

I maintain agent-eval (agent_regress). You can find the full scripts and raw output here: https://github.com/RudrenduPaul/agent-eval

Given you are doing more ToolRuntime generic-type work, I am curious whether this reproduction matches the real-world case that prompted the fix, or if there is a related generic-type edge case worth testing next.

Best,
Sourav

HaoFeng Zhao (ronronner02) pushed a commit to ronronner02/langgraph that referenced this pull request Sep 25, 2026
…on (langchain-ai#6509)

**Description:**
This PR fixes an issue where injection types (like `ToolRuntime`) were
not recognized by `ToolNode` when used with generic type arguments
(e.g., `ToolRuntime[MyContext]`).

Previously, the `_is_injection` check relied solely on `isinstance` and
`issubclass`, which fail for `typing._GenericAlias` objects. This update
adds a check using `typing.get_origin()` to correctly identify the base
class of generic types, ensuring the runtime is injected correctly even
when type hints are present.

**Issue:** Fixes langchain-ai#6465

**Dependencies:** None

**Twitter handle:** @SidharthRajmoh2

---------

Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ToolRuntime[WithTypeArguments] not supported

3 participants