[PR #4932] Improve type checking on graph init and invoke/stream #4157

Closed
opened 2026-02-20 17:49:45 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/4932

State: closed
Merged: Yes


  • Type checking on input for invoke, stream, etc matches that of either InputT or StateT, depending on if InputT is specified
  • Type checking on node signatures, requiring state to be typed as StateT
  • Ensuring the type used for state_schema is a typed dict spec, dataclass, or base model type
from typing import Annotated, Any

from langgraph.checkpoint.memory import InMemorySaver
from typing_extensions import TypedDict

from langgraph.graph import StateGraph
from operator import add


class State(TypedDict):
    info: Annotated[list[str], add]


graph_builder = StateGraph(State)


def start(state: State) -> Any:
    return {"info": state["info"] + ["!"]}


def invalid_node() -> Any: ...


graph_builder.add_node("start", start)
graph_builder.add_node("invalid", invalid_node, input=State)
"""
Argument of type "() -> Any" cannot be assigned to parameter "action" of type "CallableStateNode[State]" in function "add_node"
  Type "() -> Any" is not assignable to type "CallableStateNode[State]"
    Type "() -> Any" is not assignable to type "(state: State) -> Any"
      Function accepts too many positional parameters; expected 0 but received 1
    Type "() -> Any" is not assignable to type "(state: State, config: RunnableConfig) -> Any"
      Function accepts too many positional parameters; expected 0 but received 
"""
graph_builder.set_entry_point("start")
graph = graph_builder.compile(checkpointer=InMemorySaver())

result = graph.invoke(
    {"info": ["hello", "world"]}, config={"configurable": {"thread_id": 1}}
)
graph.invoke({"wrong": 1}, config={"configurable": {"thread_id": 1}})
"""
Argument of type "dict[str, int]" cannot be assigned to parameter "input" of type "State" in function "invoke"
"""


class InputState(TypedDict):
    something: int


class State(InputState):
    info: Annotated[list[str], add]


new_builder = StateGraph(State, input=InputState)
new_builder.add_node("start", start)
new_builder.set_entry_point("start")
new_graph = new_builder.compile(checkpointer=InMemorySaver())

result = new_graph.invoke({"something": 1})
result = new_graph.invoke({"something": 2, "info": ["hello", "world"]})
"""
Argument of type "dict[str, int | list[str]]" cannot be assigned to parameter "input" of type "InputState" in function "invoke"
"""

TODO, largely in future PRs:

  • Add OutputT as a generic on StateGraph, related to typing output for invoke and stream
  • Investigate Runnable use internally - migrating away from this where possible
  • Investigate accepting callables with arbitrary args via ParamSpec, then can remove typing for store/writer
  • Add new RunContext
  • Rename args to StateGraph (input -> input_type etc)
  • Figure out how to handle sync and async functions
  • Make better use of defined Unset types
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/4932 **State:** closed **Merged:** Yes --- * Type checking on input for `invoke`, `stream`, etc matches that of either `InputT` or `StateT`, depending on if `InputT` is specified * Type checking on node signatures, requiring `state` to be typed as `StateT` * Ensuring the type used for `state_schema` is a typed dict spec, dataclass, or base model type ```py from typing import Annotated, Any from langgraph.checkpoint.memory import InMemorySaver from typing_extensions import TypedDict from langgraph.graph import StateGraph from operator import add class State(TypedDict): info: Annotated[list[str], add] graph_builder = StateGraph(State) def start(state: State) -> Any: return {"info": state["info"] + ["!"]} def invalid_node() -> Any: ... graph_builder.add_node("start", start) graph_builder.add_node("invalid", invalid_node, input=State) """ Argument of type "() -> Any" cannot be assigned to parameter "action" of type "CallableStateNode[State]" in function "add_node" Type "() -> Any" is not assignable to type "CallableStateNode[State]" Type "() -> Any" is not assignable to type "(state: State) -> Any" Function accepts too many positional parameters; expected 0 but received 1 Type "() -> Any" is not assignable to type "(state: State, config: RunnableConfig) -> Any" Function accepts too many positional parameters; expected 0 but received """ graph_builder.set_entry_point("start") graph = graph_builder.compile(checkpointer=InMemorySaver()) result = graph.invoke( {"info": ["hello", "world"]}, config={"configurable": {"thread_id": 1}} ) graph.invoke({"wrong": 1}, config={"configurable": {"thread_id": 1}}) """ Argument of type "dict[str, int]" cannot be assigned to parameter "input" of type "State" in function "invoke" """ class InputState(TypedDict): something: int class State(InputState): info: Annotated[list[str], add] new_builder = StateGraph(State, input=InputState) new_builder.add_node("start", start) new_builder.set_entry_point("start") new_graph = new_builder.compile(checkpointer=InMemorySaver()) result = new_graph.invoke({"something": 1}) result = new_graph.invoke({"something": 2, "info": ["hello", "world"]}) """ Argument of type "dict[str, int | list[str]]" cannot be assigned to parameter "input" of type "InputState" in function "invoke" """ ``` TODO, largely in future PRs: * Add `OutputT` as a generic on `StateGraph`, related to typing output for `invoke` and `stream` * Investigate `Runnable` use internally - migrating away from this where possible * Investigate accepting callables with arbitrary args via `ParamSpec`, then can remove typing for store/writer * Add new `RunContext` * Rename args to `StateGraph` (`input` -> `input_type` etc) * Figure out how to handle sync and async functions * Make better use of defined `Unset` types
yindo added the pull-request label 2026-02-20 17:49:45 -05:00
yindo closed this issue 2026-02-20 17:49:45 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#4157