@@ -259,6 +259,19 @@ def test_validate_graph_success() -> None:
259
259
graph .graph_validate ()
260
260
assert not graph .get_has_cycles ()
261
261
262
+ # Use a lambda condition
263
+ graph_with_lambda = DiGraph (
264
+ nodes = {
265
+ "A" : DiGraphNode (
266
+ name = "A" , edges = [DiGraphEdge (target = "B" , condition = lambda msg : "test" in msg .to_model_text ())]
267
+ ),
268
+ "B" : DiGraphNode (name = "B" , edges = []),
269
+ }
270
+ )
271
+ # No error should be raised
272
+ graph_with_lambda .graph_validate ()
273
+ assert not graph_with_lambda .get_has_cycles ()
274
+
262
275
263
276
def test_validate_graph_missing_start_node () -> None :
264
277
"""Test validation failure when no start node exists."""
@@ -298,6 +311,23 @@ def test_validate_graph_mixed_conditions() -> None:
298
311
with pytest .raises (ValueError , match = "Node 'A' has a mix of conditional and unconditional edges" ):
299
312
graph .graph_validate ()
300
313
314
+ # Use lambda for condition
315
+ graph_with_lambda = DiGraph (
316
+ nodes = {
317
+ "A" : DiGraphNode (
318
+ name = "A" ,
319
+ edges = [
320
+ DiGraphEdge (target = "B" , condition = lambda msg : "test" in msg .to_model_text ()),
321
+ DiGraphEdge (target = "C" ),
322
+ ],
323
+ ),
324
+ "B" : DiGraphNode (name = "B" , edges = []),
325
+ "C" : DiGraphNode (name = "C" , edges = []),
326
+ }
327
+ )
328
+ with pytest .raises (ValueError , match = "Node 'A' has a mix of conditional and unconditional edges" ):
329
+ graph_with_lambda .graph_validate ()
330
+
301
331
302
332
@pytest .mark .asyncio
303
333
async def test_invalid_digraph_manager_cycle_without_termination () -> None :
@@ -603,6 +633,29 @@ async def test_digraph_group_chat_conditional_branch(runtime: AgentRuntime | Non
603
633
result = await team .run (task = "Trigger yes" )
604
634
assert result .messages [2 ].source == "B"
605
635
636
+ # Use lambda conditions
637
+ graph_with_lambda = DiGraph (
638
+ nodes = {
639
+ "A" : DiGraphNode (
640
+ name = "A" ,
641
+ edges = [
642
+ DiGraphEdge (target = "B" , condition = lambda msg : "yes" in msg .to_model_text ()),
643
+ DiGraphEdge (target = "C" , condition = lambda msg : "no" in msg .to_model_text ()),
644
+ ],
645
+ ),
646
+ "B" : DiGraphNode (name = "B" , edges = [], activation = "any" ),
647
+ "C" : DiGraphNode (name = "C" , edges = [], activation = "any" ),
648
+ }
649
+ )
650
+ team_with_lambda = GraphFlow (
651
+ participants = [agent_a , agent_b , agent_c ],
652
+ graph = graph_with_lambda ,
653
+ runtime = runtime ,
654
+ termination_condition = MaxMessageTermination (5 ),
655
+ )
656
+ result_with_lambda = await team_with_lambda .run (task = "Trigger no" )
657
+ assert result_with_lambda .messages [2 ].source == "C"
658
+
606
659
607
660
@pytest .mark .asyncio
608
661
async def test_digraph_group_chat_loop_with_exit_condition (runtime : AgentRuntime | None ) -> None :
@@ -785,6 +838,31 @@ async def test_digraph_group_chat_multiple_conditional(runtime: AgentRuntime | N
785
838
result = await team .run (task = "banana" )
786
839
assert result .messages [2 ].source == "C"
787
840
841
+ # Use lambda conditions
842
+ graph_with_lambda = DiGraph (
843
+ nodes = {
844
+ "A" : DiGraphNode (
845
+ name = "A" ,
846
+ edges = [
847
+ DiGraphEdge (target = "B" , condition = lambda msg : "apple" in msg .to_model_text ()),
848
+ DiGraphEdge (target = "C" , condition = lambda msg : "banana" in msg .to_model_text ()),
849
+ DiGraphEdge (target = "D" , condition = lambda msg : "cherry" in msg .to_model_text ()),
850
+ ],
851
+ ),
852
+ "B" : DiGraphNode (name = "B" , edges = []),
853
+ "C" : DiGraphNode (name = "C" , edges = []),
854
+ "D" : DiGraphNode (name = "D" , edges = []),
855
+ }
856
+ )
857
+ team_with_lambda = GraphFlow (
858
+ participants = [agent_a , agent_b , agent_c , agent_d ],
859
+ graph = graph_with_lambda ,
860
+ runtime = runtime ,
861
+ termination_condition = MaxMessageTermination (5 ),
862
+ )
863
+ result_with_lambda = await team_with_lambda .run (task = "cherry" )
864
+ assert result_with_lambda .messages [2 ].source == "D"
865
+
788
866
789
867
class _TestMessageFilterAgentConfig (BaseModel ):
790
868
name : str
0 commit comments