-
Notifications
You must be signed in to change notification settings - Fork 314
feat: expose user-defined state in MultiAgent Graph #703
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
base: main
Are you sure you want to change the base?
Conversation
- Add SharedContext class to multiagent.base for unified state management - Add shared_context property to Graph class for easy access - Update GraphState to include shared_context field - Refactor Swarm to use SharedContext from base module - Add comprehensive tests for SharedContext functionality - Support JSON serialization validation and deep copying Resolves strands-agents#665
src/strands/multiagent/base.py
Outdated
|
||
context: dict[str, dict[str, Any]] = field(default_factory=dict) | ||
|
||
def add_context(self, node_id: str, key: str, value: Any) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for raising this!
I have a couple of concerns relating to backwards compatibility.
It looks like we switched from SwarmNode to node_id. Can we instead retain the Node object. Refactor SwarmNode into base.py as some MultiAgentNode.
Then we need to maintain backwards compatibility via aliases in swarm. meaning we do not want to break imports as right now it will be broken if a user has an import like from strands.multiagent.swarm import SharedContext
so we need to avoid breaking consumers for SharedContext and Node.
context: dict[str, dict[str, Any]] = field(default_factory=dict) | ||
|
||
def add_context(self, node_id: str, key: str, value: Any) -> None: | ||
"""Add context for a specific node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It also looks like unit tests are failing. May need to rebase or address them if they are still failing
- Refactor SharedContext to use Node objects instead of node_id strings - Add MultiAgentNode base class for unified node abstraction - Update SwarmNode and GraphNode to inherit from MultiAgentNode - Maintain backward compatibility with aliases in swarm.py - Update all tests to use new API with node objects - Fix indentation issues in graph.py Resolves reviewer feedback on PR strands-agents#665
- Restored all missing Swarm implementation methods (_setup_swarm, _execute_swarm, etc.) - Fixed SharedContext usage to use node objects instead of node_id strings - All multiagent tests now pass locally - Maintains backward compatibility for existing imports Fixes CI test failures
- Fixed import sorting in graph.py and swarm.py - All linting checks now pass - Code is ready for CI pipeline
- Fixed all formatting issues with ruff format - All linting checks now pass - All functionality tests pass - Code is completely error-free and ready for CI
Could you please take a moment to review the updated implementation? I've made every effort to address your feedback while maintaining the requested functionality for issue #665. The code is now ready for CI validation and should pass all checks. |
|
||
# Backward compatibility aliases | ||
# These ensure that existing imports continue to work | ||
__all__ = ["SwarmNode", "SharedContext", "Status"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing, thanks! I will pull this down and test a little bit today but looks great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @dbschmigelski ,
please test and merge this if you find any issue to fix please let me know
🎯 Feature: Expose User Defined State in MultiAgent Graph
Summary
This PR implements the requested feature from issue #665 to expose user-defined state in MultiAgent Graph, similar to how Agent has AgentState and Swarm has SharedContext. This enables users to store and access information across graph nodes without including it in the LLM context.
Changes Made
1. Add SharedContext class to multiagent.base for unified state management
src/strands/multiagent/base.py
2. Add shared_context property to Graph class for easy access
src/strands/multiagent/graph.py
graph.shared_context.add_context("node_id", "key", "value")
3. Update GraphState to include shared_context field
src/strands/multiagent/graph.py
shared_context: SharedContext
field to GraphState class4. Refactor Swarm to use SharedContext from base module
src/strands/multiagent/swarm.py
5. Add comprehensive tests for SharedContext functionality
tests/strands/multiagent/test_base.py
- Unit tests for SharedContexttests/strands/multiagent/test_graph.py
- Integration tests for Graph6. Support JSON serialization validation and deep copying
Related Issues
Documentation PR
No documentation PR needed at this time. The feature is self-contained and follows existing patterns.
Type of Change
New feature - This adds new functionality to the Graph class without breaking existing functionality.
Testing
How have you tested the change?
Verify that the changes do not break functionality or introduce warnings in consuming repositories
Test Results
# All tests passing tests/strands/multiagent/test_base.py::test_shared_context_initialization PASSED tests/strands/multiagent/test_base.py::test_shared_context_add_context PASSED tests/strands/multiagent/test_base.py::test_shared_context_get_context PASSED tests/strands/multiagent/test_base.py::test_shared_context_validation PASSED tests/strands/multiagent/test_base.py::test_shared_context_isolation PASSED tests/strands/multiagent/test_base.py::test_shared_context_copy_semantics PASSED tests/strands/multiagent/test_graph.py::test_graph_shared_context PASSED tests/strands/multiagent/test_graph.py::test_graph_shared_context_validation PASSED
Code Quality Checks
ruff check
with no errorsruff format
Usage Examples
Basic Usage
Advanced Usage
Benefits
Migration Path
For Existing Swarm Users
No changes required - existing code continues to work. However, users are encouraged to import from the base module:
For New Graph Users
Simply use the new
shared_context
property:Checklist
Files Changed
src/strands/multiagent/base.py
- Added SharedContext classsrc/strands/multiagent/graph.py
- Added shared_context to GraphState and Graphsrc/strands/multiagent/swarm.py
- Updated to use base SharedContexttests/strands/multiagent/test_base.py
- Tests for SharedContexttests/strands/multiagent/test_graph.py
- Tests for Graph integrationImpact Assessment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.