- Configure the AWS client and import required packages.
- Load a policy document and chunk it into paragraphs.
- Convert chunks into embeddings with SentenceTransformer.
- Validate the vector index dimension in the S3 vector bucket.
- Upload vectors to the S3 vector index.
- Query the index using a text query to retrieve nearest neighbors.
- Python 3.8+
- A working AWS account and credentials with access to the S3 Vectors API
- The
boto3andsentence-transformerspackages
Setup — imports and AWS configuration
- For this demo we perform a simple paragraph split (split on double newlines). Depending on your data, you might prefer sentence splitting or a sliding window approach.
- Use the same SentenceTransformer model that you will use for queries to ensure dimensional consistency.
- The vector index must be created with the same vector dimension as the embeddings produced by your model. Query the index metadata and read the dimension field robustly to handle variations in the API response structure.
Always ensure the vector index dimension matches the embedding dimension produced by your model. A mismatch will cause
put_vectors or query_vectors operations to fail.If the index was created with an incorrect dimension (for example, 3 instead of 384), you must delete and recreate the index with the correct
dimension value before uploading vectors.- Embedding vectors are fixed-length numeric arrays. If the index expects a different length, the underlying vector store cannot store or compare vectors correctly, and operations will error out.


- Each entry needs a unique
key, the vector under the supported numeric format (herefloat32), and optionalmetadata(we store the original paragraph text).
- Use the same model to encode the query text. Provide the query vector as
float32and request metadata and distance if you want to inspect results.
topKcontrols how many nearest neighbors are returned.- The
queryVectormust match the index dimension. - The response typically includes an array of
vectors(hits); each hit containskey,distance, and whatevermetadatayou stored.
- Convert text to embeddings using a single consistent model (e.g.
sentence-transformers/all-MiniLM-L6-v2). - Always verify that the S3 vector index
dimensionmatches the embedding vector length. - Upload vectors with
put_vectors, including a stablekey, thevectorvalues (e.g.float32), and helpfulmetadata. - Query with
query_vectorsusing the same model, correcttopK, and requestreturnMetadata/returnDistanceas needed. - If you encounter dimension mismatches, delete and recreate the index with the correct dimension.
