Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When the update argument for Command is provided as a TypedDict, mypy raises a complaint #2758

Open
4 tasks done
gbaian10 opened this issue Dec 13, 2024 · 0 comments
Open
4 tasks done

Comments

@gbaian10
Copy link
Contributor

gbaian10 commented Dec 13, 2024

Checked other resources

  • This is a bug, not a usage question. For questions, please use GitHub Discussions.
  • I added a clear and detailed title that summarizes the issue.
  • I read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • 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 typing import Literal, TypedDict

from langgraph.graph import StateGraph
from langgraph.types import Command


class State(TypedDict):
    foo: str


def node_a(state: State) -> Command[Literal["node_b"]]:
    return Command(update=State(foo=state["foo"] + "a"), goto="node_b")


def node_b(state: State) -> State:
    return State(foo=state["foo"] + "b")


builder = StateGraph(State)
builder.set_entry_point(node_a.__name__)
builder.add_node(node_a)
builder.add_node(node_b)
graph = builder.compile()
print(graph.invoke(State(foo="")))

Error Message and Stack Trace (if applicable)

error: Argument "update" to "Command" has incompatible type "State"; expected "dict[str, Any] | Sequence[tuple[str, Any]]"  [arg-type]
        return Command(update=State(foo=state["foo"] + "a"), goto="node_b")
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)

Description

The program runs fine.
It's strange that we define the state format using TypedDict, yet receive warnings when using TypedDict as input, right?

Because the internal type hints uses dict, mypy complains when receiving a TypedDict.
I think changing the type hint to Mapping could resolve this.

image

# before
update: Union[dict[str, Any], Sequence[tuple[str, Any]]] = ()

# after
update: Union[Mapping[str, Any], Sequence[tuple[str, Any]]] = ()

If you agree with this change, I can quickly submit a simple PR to address the issue.

System Info

System Information

OS: Linux
Python Version: 3.13.1 (main, Dec 4 2024, 08:54:15) [GCC 13.2.0]

Package Information

langchain_core: 0.3.24
langgraph: 0.2.59

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

No branches or pull requests

1 participant