[PR #5424] feat(langgraph): add type checking for matching node signatures vs input_schema for add_node #4432

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

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

State: closed
Merged: Yes


Cranking on type safety / helpful hints for langgraph!

When you call add_node, if input_schema is specified, the type checker will warn if the node signature isn't compatible.

Also, ensures StateT | Command | None is accepted for invoke / stream methods, with corresponding tests.

For example:

from typing import Any, TypedDict
from langgraph.graph import StateGraph, END
from typing import Sequence

class A(TypedDict):
    a: int

class B(TypedDict):
    b: int

class State(A, B):
    ...

def a(state: A) -> Any:
    ...

def b(state: B) -> Any:
    ...

# Create and configure the graph
workflow = StateGraph(State)
workflow.add_node("a", a, input_schema=A)
workflow.add_node("b", b, input_schema=B)

workflow.add_node("a_wrong", a, input_schema=B) # warns (B doesn't match A)
workflow.add_node("b_wrong", b, input_schema=A) # warns (A doesn't match B)

Makes more progress on https://github.com/langchain-ai/langgraph/issues/5000

TODO:

  • Need to figure out if we should always be using contravariant type vars for the case where they're accepted as args to the graph / node invocation.
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/5424 **State:** closed **Merged:** Yes --- Cranking on type safety / helpful hints for langgraph! When you call `add_node`, if `input_schema` is specified, the type checker will warn if the node signature isn't compatible. Also, ensures `StateT | Command | None` is accepted for `invoke` / `stream` methods, with corresponding tests. For example: ```py from typing import Any, TypedDict from langgraph.graph import StateGraph, END from typing import Sequence class A(TypedDict): a: int class B(TypedDict): b: int class State(A, B): ... def a(state: A) -> Any: ... def b(state: B) -> Any: ... # Create and configure the graph workflow = StateGraph(State) workflow.add_node("a", a, input_schema=A) workflow.add_node("b", b, input_schema=B) workflow.add_node("a_wrong", a, input_schema=B) # warns (B doesn't match A) workflow.add_node("b_wrong", b, input_schema=A) # warns (A doesn't match B) ``` Makes more progress on https://github.com/langchain-ai/langgraph/issues/5000 TODO: * Need to figure out if we should always be using contravariant type vars for the case where they're accepted as args to the graph / node invocation.
yindo added the pull-request label 2026-02-20 17:50:11 -05:00
yindo closed this issue 2026-02-20 17:50:11 -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#4432