How to use Bedrock embeddings with LangChain
Quick answer
Use the AWS Bedrock embeddings by calling the Bedrock runtime via
boto3 to generate embeddings, then wrap this call in a custom LangChain embedding class implementing embed_documents and embed_query. This enables LangChain to use Bedrock embeddings for vector search and retrieval workflows.PREREQUISITES
Python 3.8+AWS credentials configured with access to Bedrockpip install boto3 langchain
Setup
Install the required packages and configure AWS credentials to access Bedrock. You need boto3 for AWS API calls and langchain for vector store integration.
- Install packages:
pip install boto3 langchain - Configure AWS credentials via
~/.aws/credentialsor environment variablesAWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY.
pip install boto3 langchain Step by step
Create a custom LangChain embedding class that calls AWS Bedrock's invoke_model API to generate embeddings. Use the amazon.titan-text-embedding model or similar. Then use this embedding class with LangChain vector stores.
import os
import boto3
from langchain.embeddings.base import Embeddings
class BedrockEmbeddings(Embeddings):
def __init__(self, region_name="us-east-1"):
self.client = boto3.client("bedrock-runtime", region_name=region_name)
self.model_id = "amazon.titan-text-embedding" # Confirm current embedding model ID
def embed_documents(self, texts):
embeddings = []
for text in texts:
response = self.client.invoke_model(
modelId=self.model_id,
body=text.encode("utf-8")
)
# The response body is a streaming HTTP response, read and decode
embedding_bytes = response['Body'].read()
# The embedding is returned as JSON bytes, decode and parse
import json
embedding_json = json.loads(embedding_bytes)
embeddings.append(embedding_json["embedding"])
return embeddings
def embed_query(self, text):
return self.embed_documents([text])[0]
# Usage example
if __name__ == "__main__":
embeddings = BedrockEmbeddings(region_name="us-east-1")
texts = ["Hello world", "LangChain with Bedrock embeddings"]
vectors = embeddings.embed_documents(texts)
print("Embeddings:", vectors) output
Embeddings: [[0.01, 0.02, ...], [0.03, 0.04, ...]] # Example float vectors
Common variations
You can customize the region_name for Bedrock, use async calls with aioboto3, or integrate with different LangChain vector stores like FAISS or Chroma. Also, update modelId if AWS releases new embedding models.
import aioboto3
import asyncio
from langchain.embeddings.base import Embeddings
class AsyncBedrockEmbeddings(Embeddings):
def __init__(self, region_name="us-east-1"):
self.region_name = region_name
self.model_id = "amazon.titan-text-embedding"
async def embed_documents(self, texts):
embeddings = []
async with aioboto3.client("bedrock-runtime", region_name=self.region_name) as client:
for text in texts:
response = await client.invoke_model(
modelId=self.model_id,
body=text.encode("utf-8")
)
embedding_bytes = await response['Body'].read()
import json
embedding_json = json.loads(embedding_bytes)
embeddings.append(embedding_json["embedding"])
return embeddings
async def embed_query(self, text):
return (await self.embed_documents([text]))[0]
# Async usage example
async def main():
embeddings = AsyncBedrockEmbeddings()
vectors = await embeddings.embed_documents(["Async example"])
print("Async embeddings:", vectors)
if __name__ == "__main__":
asyncio.run(main()) output
Async embeddings: [[0.01, 0.02, ...]] # Example float vectors
Troubleshooting
- If you get
AccessDeniedException, verify your AWS credentials and Bedrock permissions. - If the response body is empty or malformed, check the
modelIdand ensure the Bedrock endpoint is correct. - For timeout errors, increase client timeout or check network connectivity.
Key Takeaways
- Use
boto3to call AWS Bedrockinvoke_modelfor embeddings. - Wrap Bedrock calls in a LangChain
Embeddingssubclass for seamless integration. - Confirm the current Bedrock embedding model ID, e.g.,
amazon.titan-text-embedding. - Async embedding calls are possible with
aioboto3for better performance. - Check AWS permissions and network settings if you encounter errors.