Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

[Bug Fix] Bedrock KB - Using LiteLLM Managed Credentials for Query (#13787)

Ishaan Jaff56ac77831605
diff --git a/litellm/vector_stores/main.py b/litellm/vector_stores/main.py
index 80d6341146a..01e06e1306d 100644
--- a/litellm/vector_stores/main.py
+++ b/litellm/vector_stores/main.py
@@ -358,6 +358,22 @@ def search(
litellm_call_id: Optional[str] = kwargs.get("litellm_call_id", None)
_is_async = kwargs.pop("asearch", False) is True
+ # pull credentials from registry if available
+ vector_store_id_for_credentials = kwargs.get("vector_store_id", vector_store_id)
+ if (
+ litellm.vector_store_registry is not None
+ and vector_store_id_for_credentials is not None
+ ):
+ try:
+ registry_credentials = (
+ litellm.vector_store_registry.get_credentials_for_vector_store(
+ vector_store_id_for_credentials
+ )
+ )
+ kwargs.update(registry_credentials)
+ except Exception:
+ pass
+
# get llm provider logic
litellm_params = GenericLiteLLMParams(**kwargs)
diff --git a/tests/test_litellm/vector_stores/test_vector_store_registry.py b/tests/test_litellm/vector_stores/test_vector_store_registry.py
index 785b55d7e03..fb585e11220 100644
--- a/tests/test_litellm/vector_stores/test_vector_store_registry.py
+++ b/tests/test_litellm/vector_stores/test_vector_store_registry.py
@@ -13,8 +13,11 @@ sys.path.insert(
) # Adds the parent directory to the system path
from datetime import datetime, timezone
+from unittest.mock import MagicMock, patch
+import litellm
from litellm.types.vector_stores import LiteLLM_ManagedVectorStore
+from litellm.vector_stores.main import search
from litellm.vector_stores.vector_store_registry import VectorStoreRegistry
@@ -113,3 +116,39 @@ def test_add_vector_store_to_registry():
assert len(registry.vector_stores) == 3
# Original store should still be there unchanged
assert registry.vector_stores[0]["vector_store_name"] == "existing_store_1"
+
+
+
+def test_search_uses_registry_credentials():
+ """search() should pull credentials from vector_store_registry when available"""
+ vector_store = LiteLLM_ManagedVectorStore(
+ vector_store_id="vs1",
+ custom_llm_provider="bedrock",
+ created_at=datetime.now(timezone.utc),
+ updated_at=datetime.now(timezone.utc),
+ )
+ registry = VectorStoreRegistry([vector_store])
+ original_registry = getattr(litellm, "vector_store_registry", None)
+ litellm.vector_store_registry = registry
+ try:
+ logger = MagicMock()
+ logger._response_cost_calculator.return_value = 0
+ with patch.object(
+ registry,
+ "get_credentials_for_vector_store",
+ return_value={"aws_access_key_id": "ABC", "aws_secret_access_key": "DEF", "aws_region_name": "us-east-1"},
+ ) as mock_get_creds, patch(
+ "litellm.vector_stores.main.ProviderConfigManager.get_provider_vector_stores_config",
+ return_value=MagicMock(),
+ ), patch(
+ "litellm.vector_stores.main.base_llm_http_handler.vector_store_search_handler",
+ return_value={},
+ ) as mock_handler:
+ search(vector_store_id="vs1", query="test", litellm_logging_obj=logger)
+ mock_get_creds.assert_called_once_with("vs1")
+ called_params = mock_handler.call_args.kwargs["litellm_params"]
+ assert getattr(called_params, "aws_access_key_id") == "ABC"
+ assert getattr(called_params, "aws_secret_access_key") == "DEF"
+ assert getattr(called_params, "aws_region_name") == "us-east-1"
+ finally:
+ litellm.vector_store_registry = original_registry
diff --git a/tests/vector_store_tests/test_bedrock_vector_store.py b/tests/vector_store_tests/test_bedrock_vector_store.py
index 5f473fac007..be4f5bd80e0 100644
--- a/tests/vector_store_tests/test_bedrock_vector_store.py
+++ b/tests/vector_store_tests/test_bedrock_vector_store.py
@@ -111,4 +111,115 @@ async def test_bedrock_search_with_router():
vector_store_id="T37J8R4WTM",
custom_llm_provider="bedrock",
)
- print(search_response)
\ No newline at end of file
+ print(search_response)
+
+
+
+async def test_bedrock_search_with_credentials_managed_registry():
+ """
+ Test that the vector store search uses the credential accessor from the registry
+ when AWS environment variables are not set, ensuring credentials are managed properly.
+ """
+ from unittest.mock import patch, MagicMock
+ from litellm.router import Router
+ from litellm.types.vector_stores import LiteLLM_ManagedVectorStore
+ from litellm.types.utils import CredentialItem
+ from litellm.vector_stores.vector_store_registry import VectorStoreRegistry
+ from datetime import datetime, timezone
+ import litellm
+
+ # Store original registry and credential list
+ original_registry = getattr(litellm, "vector_store_registry", None)
+ original_credential_list = getattr(litellm, "credential_list", [])
+
+ try:
+ # Set up test AWS credentials in the credential system
+ test_credentials = CredentialItem(
+ credential_name="bedrock-litellm-website-knowledgebase",
+ credential_info={
+ "provider": "aws",
+ "description": "Test AWS credentials for bedrock"
+ },
+ credential_values={
+ "aws_access_key_id": "test_access_key",
+ "aws_secret_access_key": "test_secret_key",
+ "aws_region_name": "us-east-1",
+ }
+ )
+
+ # Set up the credential list
+ litellm.credential_list = [test_credentials]
+
+ # Create vector store with credential reference
+ vector_store = LiteLLM_ManagedVectorStore(
+ vector_store_id="T37J8R4WTM",
+ custom_llm_provider="bedrock",
+ created_at=datetime.now(timezone.utc),
+ updated_at=datetime.now(timezone.utc),
+ litellm_credential_name="bedrock-litellm-website-knowledgebase",
+ )
+
+ # Set up registry
+ registry = VectorStoreRegistry([vector_store])
+ litellm.vector_store_registry = registry
+
+ # Verify credentials can be retrieved from registry
+ retrieved_credentials = registry.get_credentials_for_vector_store("T37J8R4WTM")
+ assert retrieved_credentials, "Should retrieve credentials from registry"
+ assert retrieved_credentials.get("aws_access_key_id") == "test_access_key"
+ assert retrieved_credentials.get("aws_secret_access_key") == "test_secret_key"
+ assert retrieved_credentials.get("aws_region_name") == "us-east-1"
+
+ # Create router and perform search
+ _router = Router(model_list=[])
+
+ # Mock the credential injection process to verify it's called
+ with patch.object(registry, 'get_credentials_for_vector_store', wraps=registry.get_credentials_for_vector_store) as mock_get_creds:
+ # Mock the actual search call to avoid making real API calls
+ with patch('litellm.vector_stores.main.base_llm_http_handler.vector_store_search_handler') as mock_handler:
+ mock_handler.return_value = {
+ "data": [
+ {
+ "id": "test_result",
+ "text": "Mock search result",
+ "score": 0.9,
+ "metadata": {}
+ }
+ ]
+ }
+
+ search_response = await _router.avector_store_search(
+ query="what happens after we add a model",
+ vector_store_id="T37J8R4WTM",
+ custom_llm_provider="bedrock",
+ )
+
+ # Verify the search was called
+ mock_handler.assert_called_once()
+ call_kwargs = mock_handler.call_args[1]
+
+ # Verify that the credential accessor was called with the correct vector store ID
+ mock_get_creds.assert_called_with("T37J8R4WTM")
+
+ # Verify the credentials were injected into the search call
+ litellm_params = call_kwargs.get("litellm_params", {})
+
+ # The key test: verify that credentials from the registry were used
+ # Since we have a registry with credentials, they should be present in the params
+ assert hasattr(litellm_params, 'aws_access_key_id'), "aws_access_key_id should be in litellm_params"
+ assert hasattr(litellm_params, 'aws_secret_access_key'), "aws_secret_access_key should be in litellm_params"
+ assert hasattr(litellm_params, 'aws_region_name'), "aws_region_name should be in litellm_params"
+
+ # Verify we got the expected response
+ assert search_response["data"][0]["id"] == "test_result"
+
+ print(f"✅ Test passed: Credential accessor was called with vector store ID: T37J8R4WTM")
+ print(f"✅ Retrieved credentials: {retrieved_credentials}")
+ print(f"✅ Credentials were injected into search call")
+ print(f"✅ Search completed successfully using registry credentials")
+
+ finally:
+ # Restore original state
+ litellm.vector_store_registry = original_registry
+ litellm.credential_list = original_credential_list
\ No newline at end of file