Skip to content

feat(adk): add native Bedrock embedding support for agent memory#1641

Merged
EItanya merged 4 commits intokagent-dev:mainfrom
go-faustino:feat/bedrock-embedding-support
Apr 10, 2026
Merged

feat(adk): add native Bedrock embedding support for agent memory#1641
EItanya merged 4 commits intokagent-dev:mainfrom
go-faustino:feat/bedrock-embedding-support

Conversation

@go-faustino
Copy link
Copy Markdown
Contributor

@go-faustino go-faustino commented Apr 8, 2026

Summary

Adds native AWS Bedrock embedding support to KagentMemoryService, so agent memory works out of the box with Bedrock embedding models (e.g. amazon.titan-embed-text-v1, amazon.titan-embed-text-v2:0).

Currently, when provider="bedrock" is set on an embedding ModelConfig, the memory service falls back to an OpenAI-compatible client which fails because no OPENAI_API_KEY is available:

WARNING - Unknown embedding provider 'bedrock'; attempting OpenAI-compatible call.
ERROR   - The api_key client option must be set either by passing api_key to the client
          or by setting the OPENAI_API_KEY environment variable

Changes

  • _memory_service.py: Add _embed_bedrock() method and register "bedrock" in _call_embedding_provider dispatch
  • test_embedding.py: Add 3 unit tests (basic embedding, region from env, error handling)

Implementation details

  • Uses boto3.client("bedrock-runtime").invoke_model(), consistent with how KAgentBedrockLlm handles LLM calls in models/_bedrock.py
  • Region resolved from AWS_DEFAULT_REGION / AWS_REGION env vars (same credential chain as the Bedrock LLM provider — IRSA, instance profile, etc.)
  • Each text is embedded individually since the Titan Embedding API accepts a single inputText per invocation; asyncio.gather parallelises the calls
  • The existing truncation-to-768 + L2-normalization logic in _generate_embedding_async handles dimension differences across Titan v1 (1536d) and v2 (1024d) models, so no model-specific dimensions parameter is needed
  • boto3 is already a declared dependency of kagent-adk

Testing

cd python
uv run pytest packages/kagent-adk/tests/unittests/test_embedding.py -v
# 13 passed (10 existing + 3 new)

New tests:

  • test_bedrock_embed — verifies embedding generation and truncation to 768 dimensions
  • test_bedrock_embed_uses_region_from_env — verifies AWS_REGION env var is passed to boto3 client
  • test_bedrock_embed_error_returns_empty — verifies graceful error handling returns []

@go-faustino go-faustino marked this pull request as ready for review April 8, 2026 14:50
Copilot AI review requested due to automatic review settings April 8, 2026 14:50
@go-faustino go-faustino requested a review from yuval-k as a code owner April 8, 2026 14:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class AWS Bedrock (Titan) embedding support to KagentMemoryService, allowing agent memory embeddings to work when EmbeddingConfig.provider="bedrock" without falling back to an OpenAI-compatible client.

Changes:

  • Add a Bedrock embedding implementation (_embed_bedrock) and wire it into the embedding-provider dispatcher.
  • Use boto3.client("bedrock-runtime").invoke_model() per text (parallelized via asyncio.gather + to_thread).
  • Add unit tests covering Bedrock embedding, region selection via env vars, and error handling.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/kagent-adk/src/kagent/adk/_memory_service.py Adds Bedrock embedding provider dispatch + _embed_bedrock() implementation using boto3 Bedrock Runtime.
python/packages/kagent-adk/tests/unittests/test_embedding.py Adds unit tests validating Bedrock embedding behavior, env-region handling, and graceful failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add _embed_bedrock() to KagentMemoryService so that memory works with
AWS Bedrock embedding models (e.g. amazon.titan-embed-text-v1/v2)
without falling back to the OpenAI-compatible client.

The implementation uses boto3's invoke_model API, consistent with how
KAgentBedrockLlm already handles LLM calls. Region is resolved from
AWS_DEFAULT_REGION / AWS_REGION env vars (same credential chain as
the Bedrock LLM provider). Each text is embedded individually since
the Titan Embedding API accepts a single inputText per invocation;
asyncio.gather parallelises the calls.

The upstream truncation-to-768 + L2-normalization logic handles
dimension differences across Titan v1 (1536d) and v2 (1024d) models,
so no model-specific dimension parameter is needed.

Signed-off-by: Gonçalo Faustino <goncalo.santos@wellhub.com>
Made-with: Cursor
- Remove unused mock_response in test_bedrock_embed
- Override AWS_DEFAULT_REGION in region env test to prevent flakiness
  when the runner environment has it set

Signed-off-by: Gonçalo Faustino <goncalo.santos@wellhub.com>
Made-with: Cursor
Wrap long mock_client.invoke_model lines to comply with ruff
line-length formatting rules.

Signed-off-by: Gonçalo Faustino <goncalo.santos@wellhub.com>
Made-with: Cursor
@go-faustino go-faustino force-pushed the feat/bedrock-embedding-support branch from da00b95 to 6bd89de Compare April 9, 2026 16:49
Copy link
Copy Markdown
Contributor

@supreme-gg-gg supreme-gg-gg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@EItanya EItanya merged commit 8d6340c into kagent-dev:main Apr 10, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants