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

Command do not render graph if nodes are defined within a class. #2965

Closed
4 tasks done
SergioG-M opened this issue Jan 9, 2025 · 2 comments
Closed
4 tasks done

Command do not render graph if nodes are defined within a class. #2965

SergioG-M opened this issue Jan 9, 2025 · 2 comments

Comments

@SergioG-M
Copy link

SergioG-M commented Jan 9, 2025

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

import random

from langgraph.graph import START, StateGraph
from langgraph.types import Command
from typing_extensions import Literal, TypedDict


# Define graph state
class State(TypedDict):
    foo: str


# Define the nodes


class MyGraph:
    def node_a(self, state: State) -> Command[Literal["node_b", "node_c"]]:
        print("Called A")
        value = random.choice(["a", "b"])
        # this is a replacement for a conditional edge function
        if value == "a":
            goto = "node_b"
        else:
            goto = "node_c"

        # note how Command allows you to BOTH update the graph state AND route to the next node
        return Command(
            # this is the state update
            update={"foo": value},
            # this is a replacement for an edge
            goto=goto,
        )

    # Nodes B and C are unchanged

    def node_b(self, state: State):
        print("Called B")
        return {"foo": state["foo"] + "b"}

    def node_c(self, state: State):
        print("Called C")
        return {"foo": state["foo"] + "c"}

    def build_graph(self):
        builder = StateGraph(State)
        builder.add_edge(START, "node_a")
        builder.add_node("node_a", self.node_a)
        builder.add_node("node_b", self.node_b)
        builder.add_node("node_c", self.node_c)
        # NOTE: there are no edges between nodes A, B and C!

        graph = builder.compile()
        return graph


from IPython.display import display, Image

graph = MyGraph().build_graph()

display(Image(graph.get_graph().draw_mermaid_png()))

Error Message and Stack Trace (if applicable)

No response

Description

When using command inside a class the edges are not rendered. It works if nodes are outside (or probably if static methods?)
I adapted this code: https://langchain-ai.github.io/langgraph/how-tos/command/#define-graph
And the generated graph is this:

image

System Info

langgraph version 0.2.61

@vbarda
Copy link
Collaborator

vbarda commented Jan 9, 2025

I can reproduce the issue, will look into it

@vbarda
Copy link
Collaborator

vbarda commented Jan 14, 2025

Fixed in #3032

@vbarda vbarda closed this as completed Jan 14, 2025
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

2 participants