langgraph icon indicating copy to clipboard operation
langgraph copied to clipboard

`ToolRuntime[WithTypeArguments]` not supported

Open e792a8 opened this issue 1 month ago • 1 comments

Checked other resources

  • [x] This is a bug, not a usage question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • [x] I added a clear and detailed title that summarizes the issue.
  • [x] I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • [x] I included a self-contained, minimal example that demonstrates the issue INCLUDING all the relevant imports. The code run AS IS to reproduce the issue.

Example Code

from langgraph.prebuilt.tool_node import ToolNode, ToolRuntime
from langgraph.graph import StateGraph, MessagesState, START, END
from langchain_core.messages import AIMessage
from langchain_core.messages.tool import tool_call
from langchain.tools import tool
from dataclasses import dataclass


@dataclass
class MyContext:
    some_info: str


@tool
def get_info_error(rt: ToolRuntime[MyContext]):
    """This tool returns error."""
    return rt.context.some_info


@tool
def get_info_ok_but_no_typechecking(rt: ToolRuntime):
    """This tool is ok, but lacks type checking."""
    return rt.context.some_info  # type: ignore


builder = StateGraph(MessagesState, context_schema=MyContext)
builder.add_node(
    "tool_node", ToolNode([get_info_ok_but_no_typechecking, get_info_error])
)
builder.add_edge(START, "tool_node")
builder.add_edge("tool_node", END)
graph = builder.compile()
result = graph.invoke(
    {
        "messages": [
            AIMessage(
                "_",
                tool_calls=[
                    tool_call(name="get_info_ok_but_no_typechecking", args={}, id="1"),
                    tool_call(name="get_info_error", args={}, id="2"),
                ],
            )
        ]
    },
    context=MyContext(some_info="this is info"),
)
print(result["messages"][-2:])

Error Message and Stack Trace (if applicable)

[ToolMessage(content='this is info', name='get_info_ok_but_no_typechecking', id='cafc05ec-1e87-401a-8150-f7a356046ae1', tool_call_id='1'), 
ToolMessage(content="Error invoking tool 'get_info_error' with kwargs {} with error:\n rt: Field required\n Please fix the error and try again.", name='get_info_error', id='9ae338e5-7672-48f2-9a52-e9ab1d4675f0', tool_call_id='2', status='error')]

Description

The ToolRuntime argument of a @tool function cannot attach type arguments, like ToolRuntime[MyContext, MyStatus], or the runtime is not injected. This makes typechecking some difficult.

System Info

System Information

OS: Linux OS Version: #1 SMP PREEMPT_DYNAMIC Wed, 15 Oct 2025 11:05:48 +0000 Python Version: 3.13.7 (main, Aug 15 2025, 12:34:02) [GCC 15.2.1 20250813]

Package Information

langchain_core: 1.0.6 langchain: 1.0.8 langchain_community: 0.4.1 langsmith: 0.4.38 langchain_anthropic: 1.0.1 langchain_chroma: 1.0.0 langchain_classic: 1.0.0 langchain_ollama: 1.0.0 langchain_openai: 1.0.2 langchain_text_splitters: 1.0.0 langgraph_api: 0.4.28 langgraph_cli: 0.4.5 langgraph_runtime_inmem: 0.14.1 langgraph_sdk: 0.2.9

Optional packages not installed

langserve

Other Dependencies

aiohttp: 3.13.2 anthropic: 0.72.0 blockbuster: 1.5.25 chromadb: 1.3.4 click: 8.3.0 cloudpickle: 3.1.1 cryptography: 44.0.3 dataclasses-json: 0.6.7 httpx: 0.28.1 httpx-sse: 0.4.3 jsonpatch: 1.33 jsonschema-rs: 0.29.1 langgraph: 1.0.3 langgraph-checkpoint: 3.0.0 numpy: 2.3.4 ollama: 0.6.0 openai: 2.6.1 opentelemetry-api: 1.38.0 opentelemetry-sdk: 1.38.0 orjson: 3.11.4 packaging: 25.0 pydantic: 2.12.3 pydantic-settings: 2.11.0 pyjwt: 2.10.1 python-dotenv: 1.2.1 pyyaml: 6.0.3 PyYAML: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.2.0 sqlalchemy: 2.0.44 SQLAlchemy: 2.0.44 sse-starlette: 2.1.3 starlette: 0.49.1 structlog: 25.5.0 tenacity: 9.1.2 tiktoken: 0.12.0 truststore: 0.10.4 typing-extensions: 4.15.0 uvicorn: 0.38.0 watchfiles: 1.1.1 zstandard: 0.25.0

e792a8 avatar Nov 19 '25 19:11 e792a8

Facing the same issue, and its been a while...

pydantic_core._pydantic_core.ValidationError: 1 validation error for my_tool_name
runtime
  Field required [type=missing, input_value={xxxxx': 'xxxx..xxxxxxx']}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.11/v/missing

asksac avatar Nov 22 '25 04:11 asksac

Hi @e792a8 and @asksac,

I've submitted a fix in PR #6509.

The issue was that ToolNode wasn't unwrapping the generic type alias correctly when checking for injection candidates. The PR updates the logic to use typing.get_origin() so that ToolRuntime[T] is correctly identified as a valid injection.

I also added a regression test based on the example provided here. Feel free to take a look!

dumko2001 avatar Nov 27 '25 08:11 dumko2001