generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 605
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
Problem Statement
I would like Strands to list messages asynchronously from S3 to improve its performance.
Proposed Solution
We will spawn a thread and start an event_loop in that thread. This threaded event loop will call s3 asynchronously for reading the objects it needs to from s3.
- Code for spawning a thread for async activities: https://github.com/strands-agents/sdk-python/blob/main/src/strands/_async.py
- Tracking issue: Add parallel reading support to S3SessionManager.list_messages() #1186
- Similar implementation that called s3 async, but did not spawn a thread: feat: implement concurrent message reading for session managers #897
Use Case
Call aws boto s3 get object asynchronously
Alternatives Solutions
N/A
Implementation Requirements
Based on clarification discussion and repository analysis:
Technical Approach
- Target File:
src/strands/session/s3_session_manager.py - Target Method:
list_messages()(lines 259-300) - Pattern: Use threaded async event loop (similar to existing
run_async()utility) - Performance Bottleneck: Current sequential S3 reads (lines 292-295)
Dependencies
- Add:
aiobotocore(https://github.com/aio-libs/aiobotocore)- Add to
pyproject.tomldependencies - Use aiobotocore for async S3 operations instead of aioboto3
- Add to
Scope
- ONLY convert
list_messages()to async - DO NOT convert other S3 operations (read_message, write_s3_object, delete_session, etc.)
- Maintain both async and sync S3 clients in S3SessionManager
Implementation Details
1. Add aiobotocore Client
- Create an async S3 client using aiobotocore alongside existing sync boto3 client
- Initialize in
__init__()method - Ensure proper cleanup/resource management
2. Implement Async list_messages()
- Create async version of message reading logic
- Use
asyncio.gather()or similar to read multiple messages concurrently - Maintain message ordering (sort by message index)
- Use existing
run_async()utility fromsrc/strands/_async.pyto execute async code
3. Integration Pattern
# Use run_async() to spawn thread and run async event loop
from strands._async import run_async
...
# Pseudocode example
def list_messages(self, session_id, agent_id, limit=None, offset=0, **kwargs):
async def async_list_messages():
# 1. List message keys (can stay sync or convert to async)
# 2. Sort and paginate keys
# 3. Read messages concurrently using asyncio.gather()
# 4. Return ordered list
pass
return run_async(async_list_messages)Configuration
- Default Behavior: Async reading is the DEFAULT behavior
- No configuration parameters needed: All list_messages calls will use async
- Backward Compatible: External API remains synchronous (async is internal implementation)
Error Handling
- Fail Fast: If ANY S3 message read fails, raise an error
- Stop the entire operation on first failure
- Propagate errors to caller with appropriate error messages
- Use existing
SessionExceptionfor error reporting
Testing Strategy
- Unit Tests: Existing tests in
tests/strands/session/test_s3_session_manager.pyshould work- Tests use
motofor AWS mocking - Tests should be agnostic to underlying sync vs async client
- Verify tests still pass with async implementation
- Tests use
- Integration Tests: Already exist, should continue to work
- No new tests required: Existing test coverage should be sufficient
Files to Modify
-
src/strands/session/s3_session_manager.py- Add aiobotocore client initialization
- Modify
list_messages()method - Add async helper method for concurrent message reading
-
pyproject.toml- Add
aiobotocoreto dependencies
- Add
Acceptance Criteria
-
aiobotocoreadded to project dependencies - Async S3 client created and managed in S3SessionManager
-
list_messages()uses async reading with threaded event loop viarun_async() - Messages are read concurrently from S3
- Message ordering is preserved (sorted by message index)
- Error handling: fail fast on any read error
- All existing unit tests pass (
tests/strands/session/test_s3_session_manager.py) - All existing integration tests pass
- External API remains synchronous (no breaking changes)
- Code follows existing patterns and style guidelines
Related Work
- PR Add parallel reading support to S3SessionManager.list_messages() #1186: Will be closed in favor of this approach
- PR feat: implement concurrent message reading for session managers #897: Referenced as similar implementation
- Existing Utility:
src/strands/_async.py-run_async()function to be reused
Implementation Notes
- Reuse existing
run_async()utility as-is (no modifications needed) - Don't worry about performance benchmarks or metrics
- Focus on correctness and maintaining existing test coverage
- Single cohesive implementation (no sub-task breakdown)
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request