Repository navigation
Conversation
Python port of strands-agents#3967. Adds an Embedder base class, a Titan-backed BedrockEmbedder, and S3VectorSearchStrategy, which embeds content on write, removes the vector on delete and queries S3 Vectors on search. S3Storage gains region_name and prefix properties, removes the vector on delete when using S3VectorSearchStrategy, and wraps search strategy failures in StorageError. Includes unit tests and an integration test against real S3, S3 Vectors and Bedrock.
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This branch has not been deployed
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.
Human Overview
Description
Python port of #3967: vector search for
S3Storage, backed by Amazon S3 Vectors, plus a provider-agnostic embedder. Same concepts and names as the TypeScript SDK, in the Python SDK's idioms.Embedder(strands.storage.embeddings, also re-exported fromstrands.storage.search): an ABC with one abstract method,async embed(text) -> list[float].BedrockEmbedder(strands.storage.embeddings.bedrock): wraps Bedrock Runtimeinvoke_modelfor Amazon Titan Text Embeddings (V2 by default). Takesmodel_id,region_name,boto_sessionandboto_client_config, likeBedrockModelandS3Storage;region_nameandboto_sessiontogether raiseStorageError. The model ID is passed through unchecked; a response without anembeddinglist raisesStorageError.S3VectorSearchStrategy(S3VectorSearchStrategyConfig(...))(strands.storage.search): a config dataclass, likeBm25SearchStrategy.vector_bucket_nameandindex_nameare required;embedder,max_results(10),boto_sessionandboto_client_configare optional.index()embeds the value and callsPutVectors, keyed bystorage.prefix + key, so storages andnamespace()views sharing one index do not collide. Empty and non-UTF-8 values are not embedded; any earlier vector for the key is removed instead.remove()callsDeleteVectors;search()embeds the query, callsQueryVectors, keeps keys under the storage's prefix (stripped) and scores them1 / (1 + distance).BedrockEmbedderand the S3 Vectors client are created once on first use, in the storage's region unless aboto_sessionis given.SearchStrategy[S3Storage], so it only attaches toS3Storage.S3Storage: new read-onlyregion_name(the configured region, else theboto_session's) andprefixproperties.delete()removes the vector when the strategy is anS3VectorSearchStrategy, and a strategy failure insearch()or vector removal is wrapped inStorageError(write()already wrapped indexing failures).Differences from TypeScript: the strategy takes a
boto_sessioninstead of a pre-builts3VectorsClient, matchingS3Storage; andBedrockEmbedderis imported only when the default embedder is first needed, but boto3 is a core dependency so nothing optional is avoided.The vector bucket and index must already exist (dimension 1024 for Titan V2). At runtime the strategy needs
s3vectors:PutVectors,DeleteVectorsandQueryVectorson the index, plusbedrock:InvokeModelon the embedding model with the default embedder.Related Issues
Python port of #3967. Related: #5551 (Python
S3Storagetrailing-slash prefix fix), #5547 (TS integration test and the CI role permissions this integ test also needs).Documentation PR
Site docs for S3 Vectors search will follow in a separate PR, covering both SDKs.
Type of Change
New feature
Testing
Unit tests for the strategy (indexing, non-text values, removal, search ranking and filtering, prefixes and
namespace(), default embedder and client creation, error propagation),BedrockEmbedder(request shape, model pass-through, non-Titan responses, region/session handling, user agent) andS3Storage(vector removal on delete, error wrapping,region_nameandprefix). Moto has noquery_vectors, so the strategy tests mock the boto client, as the TS tests do.hatch teston storage, memory stores and context offloader: 582 passed.hatch fmt --formatterandhatch fmt --linter(ruff + mypy) clean.New integration test
tests_integ/test_s3_vector_search_strategy.py(the three TS cases plus anamespace()check) passed 3/3 against real S3, S3 Vectors and Bedrock inus-east-1, with a personal account's credentials, and teardown left no resources. It createstest-strands-s3v-*buckets, the same prefix as the TS test, so in CI it needs the integ role permissions from test(storage): add S3VectorSearchStrategy integration test #5547.I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.