feat(session): allow S3SessionManager to reuse a pre-built S3 client#2301
Open
MukundaKatta wants to merge 1 commit into
Open
Conversation
Adds a new optional `s3_client` keyword argument to `S3SessionManager`. When provided, the manager reuses the caller's boto3 S3 client directly instead of constructing a new `boto3.Session` and S3 client, avoiding the per-instance HTTP connection pool + endpoint discovery overhead. Callers also retain full control of the client (credentials, retry config, custom endpoints). When `s3_client` is supplied, the existing `boto_session`, `boto_client_config`, and `region_name` parameters are ignored (documented in the docstring). Backward-compatible: callers that do not pass `s3_client` get the existing behavior unchanged, including the `strands-agents` user-agent tag on the auto-built client. Tests added (moto-backed): - test_s3_client_kwarg_reuses_supplied_client - test_s3_client_kwarg_ignores_session_and_config - test_s3_client_kwarg_supports_session_round_trip - test_default_path_still_works Full session/s3 suite: 50 passed. Refs strands-agents#1163
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
s3_clientkeyword argument toS3SessionManager.__init__so callers can pass a pre-builtboto3S3 client and avoid the per-instance overhead of constructing a freshboto3.Sessionand S3 client on every manager creation.When
s3_clientis provided,boto_session,boto_client_config, andregion_nameare ignored (documented in the docstring) and the caller's client is used as-is.When
s3_clientis omitted, behavior is unchanged: the manager builds aboto3.Session(usingregion_nameandboto_session) and a client with thestrands-agentsuser-agent tag.This is a backward-compatible additive change.
Why
Refs #1163. Users instantiating many
S3SessionManagerinstances in the same process (per-agent, per-session-id, etc.) currently pay the cost ofboto3.Session(...)andsession.client("s3", ...)on every manager. Eachsession.client(...)call creates a fresh urllib3 connection pool and runs endpoint discovery against AWS.Passing a single shared S3 client across managers eliminates that overhead and lets callers control credentials, retry config, and custom endpoints (for example, MinIO or LocalStack) without subclassing.
A Strands maintainer (@pgrayy) acknowledged the issue on #1163: "I agree that we should allow for reuse of the session manager instance so that user do not have to reinitialize the underlying client."
Example
Before:
After:
Tests
Added four
moto-backed tests totests/strands/session/test_s3_session_manager.py:test_s3_client_kwarg_reuses_supplied_client— supplied client becomesmanager.client.test_s3_client_kwarg_ignores_session_and_config—boto_session.clientis never called whens3_clientis set.test_s3_client_kwarg_supports_session_round_trip— end-to-end create + read of a session through a manager built with the new kwarg.test_default_path_still_works— the existing path with nos3_clientis unchanged.Full session/S3 test suite: 50 passed locally.
Test plan
pytest tests/strands/session/test_s3_session_manager.pypassess3_client=argument) get identical behaviors3_client=is supplied, noboto3.Sessionis constructeds3_clientis suppliedRefs #1163.