Skip to content

feat: add conditional edges, fan-out/fan-in, and max iterations support#13

Open
NirAlon wants to merge 3 commits intotmc:mainfrom
NirAlon:feature/add-conditional-edges
Open

feat: add conditional edges, fan-out/fan-in, and max iterations support#13
NirAlon wants to merge 3 commits intotmc:mainfrom
NirAlon:feature/add-conditional-edges

Conversation

@NirAlon
Copy link

@NirAlon NirAlon commented Mar 11, 2026

Summary

This PR adds three major features to langgraphgo:

  1. Conditional Edges - Route to different nodes based on state evaluation
  2. Fan-out/Fan-in - Parallel execution of multiple nodes with result merging
  3. Max Iterations - Cycle protection with configurable iteration limits

Changes

Conditional Edges

  • Add ConditionalEdge struct with condition function and routing mapping
  • Add AddConditionalEdges method to define state-based routing
  • Add ErrInvalidConditionResult error for unmapped routing results

Fan-out/Fan-in (Parallel Execution)

  • When multiple edges originate from a node, target nodes execute in parallel using goroutines
  • Results are merged using a reducer function (default: concatenation)
  • Add SetReducer method for custom merge logic
  • Automatic fan-in detection finds common target node after parallel branches

Max Iterations

  • Add SetMaxIterations method to configure cycle limits (default: 25)
  • Add ErrMaxIterationsExceeded error when limit is reached
  • Prevents infinite loops in cyclic graphs

New API

// Conditional routing                                                                                                                                             
g.AddConditionalEdges("classifier", routeFunc, map[string]string{                                                                                                  
    "route_a": "handler_a",                                                                                                                                        
    "route_b": "handler_b",                                                                                                                                        
})                                                                                                                                                                 

// Max iterations (default 25)                                                                                                                                     
g.SetMaxIterations(100)                                                                                                                                            

// Custom reducer for parallel results                                                                                                                             
g.SetReducer(func(results [][]llms.MessageContent) []llms.MessageContent {                                                                                         
    // custom merge logic                                                                                                                                          
})                                                                                                                                                                 

Testing                                                                                                                                                            

- Added tests for conditional edge routing                                                                                                                         
- Added tests for max iterations exceeded                                                                                                                          
- Added tests for fan-out parallel execution                                                                                                                       
- Added tests for custom reducer                                                                                                                                   

All existing tests continue to pass

Add AddConditionalEdges method to enable state-based routing in graphs.
This allows nodes to route to different targets based on evaluating
the current state through a condition function.

Changes:
- Add ConditionalEdge struct with From, Condition, and Mapping fields
- Add conditionalEdges map to MessageGraph
- Add AddConditionalEdges method
- Update Invoke to check conditional edges before regular edges
- Add ErrInvalidConditionResult error for unmapped routing results
- Add tests for conditional edge routing
- Add SetMaxIterations to prevent infinite loops in cyclic graphs
- Add SetReducer for custom parallel result merging
- Add fan-out support for parallel node execution
- Add fan-in support for merging parallel branches
- Add tests for max iterations, fan-out, and custom reducer
- Update README with new features documentation
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

Successfully merging this pull request may close these issues.

2 participants