Skip to content

Commit

Permalink
Merge pull request #1894 from langchain-ai/dqbd/xray-bool
Browse files Browse the repository at this point in the history
fix(graph): xray=True should be distinct from xray=1
  • Loading branch information
dqbd authored Sep 30, 2024
2 parents 904904c + 358603b commit a69c58b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libs/langgraph/langgraph/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,11 @@ def add_edge(
subgraph = (
subgraphs[key].get_graph(
config=config,
xray=xray - 1 if isinstance(xray, int) and xray > 0 else xray,
xray=xray - 1
if isinstance(xray, int)
and not isinstance(xray, bool)
and xray > 0
else xray,
)
if key in subgraphs
else node.get_graph(config=config)
Expand Down
36 changes: 36 additions & 0 deletions libs/langgraph/tests/__snapshots__/test_pregel.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -5148,6 +5148,42 @@

'''
# ---
# name: test_xray_bool
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
graph TD;
__start__([<p>__start__</p>]):::first
gp_one(gp_one)
gp_two___start__(<p>__start__</p>)
gp_two_p_one(p_one)
gp_two_p_two___start__(<p>__start__</p>)
gp_two_p_two_c_one(c_one)
gp_two_p_two_c_two(c_two)
gp_two_p_two___end__(<p>__end__</p>)
gp_two___end__(<p>__end__</p>)
__end__([<p>__end__</p>]):::last
__start__ --> gp_one;
gp_two___end__ --> gp_one;
gp_one -. &nbsp;0&nbsp; .-> gp_two___start__;
gp_one -. &nbsp;1&nbsp; .-> __end__;
subgraph gp_two
gp_two___start__ --> gp_two_p_one;
gp_two_p_two___end__ --> gp_two_p_one;
gp_two_p_one -. &nbsp;0&nbsp; .-> gp_two_p_two___start__;
gp_two_p_one -. &nbsp;1&nbsp; .-> gp_two___end__;
subgraph p_two
gp_two_p_two___start__ --> gp_two_p_two_c_one;
gp_two_p_two_c_two --> gp_two_p_two_c_one;
gp_two_p_two_c_one -. &nbsp;0&nbsp; .-> gp_two_p_two_c_two;
gp_two_p_two_c_one -. &nbsp;1&nbsp; .-> gp_two_p_two___end__;
end
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc

'''
# ---
# name: test_xray_issue
'''
%%{init: {'flowchart': {'curve': 'linear'}}}%%
Expand Down
45 changes: 45 additions & 0 deletions libs/langgraph/tests/test_pregel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11358,6 +11358,51 @@ def _node(state: State):
assert app.get_graph(xray=True).draw_mermaid() == snapshot


def test_xray_bool(snapshot: SnapshotAssertion) -> None:
class State(TypedDict):
messages: Annotated[list, add_messages]

def node(name):
def _node(state: State):
return {"messages": [("human", f"entered {name} node")]}

return _node

grand_parent = StateGraph(State)

child = StateGraph(State)

child.add_node("c_one", node("c_one"))
child.add_node("c_two", node("c_two"))

child.add_edge("__start__", "c_one")
child.add_edge("c_two", "c_one")

child.add_conditional_edges(
"c_one", lambda x: str(randrange(0, 2)), {"0": "c_two", "1": "__end__"}
)

parent = StateGraph(State)
parent.add_node("p_one", node("p_one"))
parent.add_node("p_two", child.compile())
parent.add_edge("__start__", "p_one")
parent.add_edge("p_two", "p_one")
parent.add_conditional_edges(
"p_one", lambda x: str(randrange(0, 2)), {"0": "p_two", "1": "__end__"}
)

grand_parent.add_node("gp_one", node("gp_one"))
grand_parent.add_node("gp_two", parent.compile())
grand_parent.add_edge("__start__", "gp_one")
grand_parent.add_edge("gp_two", "gp_one")
grand_parent.add_conditional_edges(
"gp_one", lambda x: str(randrange(0, 2)), {"0": "gp_two", "1": "__end__"}
)

app = grand_parent.compile()
assert app.get_graph(xray=True).draw_mermaid() == snapshot


def test_subgraph_retries():
class State(TypedDict):
count: int
Expand Down

0 comments on commit a69c58b

Please sign in to comment.