mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-03 01:38:46 +00:00
* test: migrate local_testing SDK and provider tests out of build_and_test * test: cover sagemaker region, hf template and nova paths end to end Region rows now go through litellm.completion on the sagemaker/ handler that the legacy tests exercised, the prompt template test mocks the Hugging Face tokenizer config instead of switching to custom_prompt_dict, streaming tests assert role, finish reason and body, and Nova single turn and streaming run through litellm.completion with a recorded request body * test: tighten migrated integration tests for local OpenAI routes, timeouts and air-gapped boot Split the catch-all providers migration file by behavior. The fake endpoint route tests now hit the owned upstream directly, as the legacy harness tests did. LM Studio asserts the outbound key, path and stripped model. The embedding timeout test asserts the proxy maps a deployment timeout to a 408 and forwards the read timeout to the provider client, instead of timing out the test client The Azure streaming tool-call test also asserts the second identical stream is served from the Redis cache with the same reassembled tool call The Docker network test always skipped in CI because the security job has no litellm/litellm image and no egress to pull one, and it tested a published image rather than the checkout. It now boots the checkout proxy with every outbound HTTP connection refused and asserts readiness, the bundled cost map fallback and that the cost map host is the only egress attempted * test: tighten the migrated e2e rows for local_testing SDK and provider tests Drop the duplicated Anthropic covers marker and the Anthropic cell wrongly claimed by the Bedrock and Vertex cache tests. Keep the file-wide Anthropic model as it is on main. Send the 2023-06-01 version header to Anthropic through the deployment's extra_headers so the header actually reaches the provider, and mark the creation-token case 1h then 5m like the legacy test did Reuse the Bedrock file's own registration for the pydantic response test, swap the hand-built PNGs for the shared cat.jpg fixture, assert the SageMaker Nova image answer names the cat and that top_logprobs=3 is honored, and register the SageMaker Nova opt-in markers the same way as the other opt-in markers * test: restore lost assertions in migrated test_main tests Restore the role alternation check in test_completion_anthropic_hanging, the embedding and streaming response-header checks, the logit_bias drop check and the gemini system and safety settings checks. Add an azure 429 retry-after test for test_exception_with_headers, trigger the router fallback with mock_timeout like the legacy test, and stop the mock timeout tests sleeping 3s * test: restore lost legacy assertions in migrated vertex ai unit tests * test: tighten migrated cost, streaming and embedding tests and scope the hosted mock guard * test: strengthen migrated anthropic, azure, bedrock and cohere provider tests Assert full request bodies and usage instead of presence checks, restore the dummy-tool and custom_llm_provider assertions the legacy tests made, add the async and per-model variants the legacy parametrization covered, split streamed tool arguments across deltas, and move the Azure chat streaming tests to the azure.py mirror file * test: tighten migrated llms provider tests and cover legacy intents lost in the move Strengthen the migrated fireworks, gemini, mistral, together, openai text completion, openrouter, perplexity and openai error tests so they assert what the legacy tests proved (cost, provider tag, usage-only-on-last-chunk, function_call rebuild, reasoning param, sync/async/stream error fields). Replace the huggingface and jina tests that doubled litellm code with respx end-to-end tests. Add the tests the pre-existing mapped tests never covered: cohere 400 mapping, token-array text completion, hosted_vllm/llamafile/lm_studio env api base embeddings, watsonx embedding auth, ollama ssl_verify client and ollama vision prompt * test: cover residual legacy paths for anthropic tool streams, watsonx async stream, ollama http images and caller async embedding clients * test: restore single request assertions in the deep_infra and azure_ai embedding tests * test: drop a no-op streaming cost flag from the openai streaming cost test * test: cover residual legacy-test lines in caching, main, utils, replicate, vertex and bedrock invoke * test: cover streamed audio assembly, choice membership, untranslated responses events and the stream log guard * test: pin cost rates in unit tests, use sdk for cohere e2e, drop vertex llama e2e * test: send the vertex maps e2e request through the openai sdk * test: drop the local_testing jobs that select no tests after the migrations * test: wait for the streamed cache entry and keep custom Mistral rates test-local * test: keep untouched Mistral test lines as they were --------- Co-authored-by: yuneng <yuneng@berri.ai>
1725 lines
67 KiB
Python
1725 lines
67 KiB
Python
import json
|
|
import asyncio
|
|
import os
|
|
from unittest.mock import Mock, patch
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
import httpx
|
|
import respx
|
|
|
|
import litellm
|
|
from litellm.caching.caching import Cache
|
|
from litellm.llms.bedrock.embed.twelvelabs_marengo_transformation import TwelveLabsMarengoEmbeddingConfig
|
|
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler, HTTPHandler
|
|
from litellm.llms.bedrock.embed.embedding import BedrockEmbedding
|
|
from tests.unit.llms.bedrock.event_loop_probe import EventLoopProbe
|
|
|
|
# Mock responses for different embedding models
|
|
titan_embedding_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
cohere_embedding_response = {"embeddings": [[0.1, 0.2, 0.3]], "inputTextTokenCount": 10}
|
|
|
|
twelvelabs_embedding_response = {
|
|
"embedding": [0.1, 0.2, 0.3],
|
|
"embeddingOption": "visual-text",
|
|
"startSec": 0.0,
|
|
"endSec": 1.0,
|
|
}
|
|
|
|
# Test data
|
|
test_input = "Hello world from litellm"
|
|
test_image_base64 = "data:image/png,test_image_base64_data"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,input_type,embed_response",
|
|
[
|
|
("bedrock/amazon.titan-embed-text-v1", "text", titan_embedding_response),
|
|
("bedrock/amazon.titan-embed-text-v2:0", "text", titan_embedding_response),
|
|
("bedrock/amazon.titan-embed-image-v1", "image", titan_embedding_response),
|
|
("bedrock/cohere.embed-english-v3", "text", cohere_embedding_response),
|
|
("bedrock/cohere.embed-multilingual-v3", "text", cohere_embedding_response),
|
|
(
|
|
"bedrock/twelvelabs.marengo-embed-2-7-v1:0",
|
|
"text",
|
|
twelvelabs_embedding_response,
|
|
),
|
|
(
|
|
"bedrock/twelvelabs.marengo-embed-2-7-v1:0",
|
|
"image",
|
|
twelvelabs_embedding_response,
|
|
),
|
|
],
|
|
)
|
|
def test_bedrock_embedding_with_api_key_bearer_token(model, input_type, embed_response):
|
|
"""Test embedding functionality with bearer token authentication"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
input_data = test_image_base64 if input_type == "image" else test_input
|
|
|
|
# Add inputType parameter for TwelveLabs Marengo models
|
|
kwargs = {
|
|
"model": model,
|
|
"input": input_data,
|
|
"client": client,
|
|
"aws_region_name": "us-east-1",
|
|
"aws_bedrock_runtime_endpoint": "https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
"api_key": test_api_key,
|
|
}
|
|
|
|
# Add input_type parameter for TwelveLabs Marengo models (maps to inputType)
|
|
if "twelvelabs.marengo-embed" in model:
|
|
kwargs["input_type"] = input_type
|
|
|
|
response = litellm.embedding(**kwargs)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
assert isinstance(response.data[0]["embedding"], list)
|
|
assert len(response.data[0]["embedding"]) == 3 # Based on mock response
|
|
|
|
headers = mock_post.call_args.kwargs.get("headers", {})
|
|
assert "Authorization" in headers
|
|
assert headers["Authorization"] == f"Bearer {test_api_key}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,input_type,embed_response",
|
|
[
|
|
("bedrock/amazon.titan-embed-text-v1", "text", titan_embedding_response),
|
|
],
|
|
)
|
|
def test_bedrock_embedding_with_env_variable_bearer_token(
|
|
model, input_type, embed_response
|
|
):
|
|
"""Test embedding functionality with bearer token from environment variable"""
|
|
client = HTTPHandler()
|
|
test_api_key = "env-bearer-token-12345"
|
|
|
|
with (
|
|
patch.dict(os.environ, {"AWS_BEARER_TOKEN_BEDROCK": test_api_key}),
|
|
patch.object(client, "post") as mock_post,
|
|
):
|
|
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-west-2",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-west-2.amazonaws.com",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
headers = mock_post.call_args.kwargs.get("headers", {})
|
|
assert "Authorization" in headers
|
|
assert headers["Authorization"] == f"Bearer {test_api_key}"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model",
|
|
["amazon.titan-embed-text-v1", "amazon.titan-embed-image-v1", "amazon.titan-embed-text-v2:0"],
|
|
)
|
|
@pytest.mark.parametrize("sync_mode", [True, False])
|
|
async def test_bedrock_titan_embedding_cache_hit_avoids_a_second_request(
|
|
respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch, model: str, sync_mode: bool
|
|
) -> None:
|
|
monkeypatch.setattr(litellm, "cache", Cache())
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
route = respx_mock.post(f"https://bedrock-runtime.us-east-1.amazonaws.com/model/{model}/invoke").mock(
|
|
return_value=httpx.Response(200, json=titan_embedding_response)
|
|
)
|
|
request = {
|
|
"model": f"bedrock/{model}",
|
|
"input": test_input,
|
|
"api_key": "test-bearer-token",
|
|
"aws_region_name": "us-east-1",
|
|
"aws_bedrock_runtime_endpoint": "https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
"caching": True,
|
|
}
|
|
|
|
if sync_mode:
|
|
response_1 = litellm.embedding(**request)
|
|
response_2 = litellm.embedding(**request)
|
|
else:
|
|
response_1 = await litellm.aembedding(**request)
|
|
response_2 = await litellm.aembedding(**request)
|
|
|
|
assert json.loads(route.calls.last.request.read()) == {"inputText": test_input}
|
|
assert response_1.data[0]["embedding"] == [0.1, 0.2, 0.3]
|
|
assert response_1.usage.prompt_tokens == 10
|
|
assert response_2._hidden_params["cache_hit"] is True
|
|
assert response_2.data[0]["embedding"] == [0.1, 0.2, 0.3]
|
|
assert route.call_count == 1
|
|
|
|
|
|
def test_bedrock_cohere_embedding_returns_the_scripted_vector(respx_mock: respx.MockRouter) -> None:
|
|
route = respx_mock.post(
|
|
"https://bedrock-runtime.us-east-1.amazonaws.com/model/cohere.embed-multilingual-v3/invoke"
|
|
).mock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json={"embeddings": [[0.3, 0.2, 0.1]], "inputTextTokenCount": 4},
|
|
)
|
|
)
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/cohere.embed-multilingual-v3",
|
|
input=["hello"],
|
|
api_key="test-bearer-token",
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
)
|
|
|
|
assert json.loads(route.calls.last.request.read()) == {"input_type": "search_document", "texts": ["hello"]}
|
|
assert response.data == [{"object": "embedding", "index": 0, "embedding": [0.3, 0.2, 0.1]}]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_bedrock_embedding_with_bearer_token():
|
|
"""Test async embedding functionality with bearer token authentication"""
|
|
client = AsyncHTTPHandler()
|
|
test_api_key = "async-bearer-token-12345"
|
|
model = "bedrock/amazon.titan-embed-text-v1"
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_embedding_response)
|
|
mock_response.json = Mock(return_value=titan_embedding_response)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = await litellm.aembedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-west-2",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-west-2.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
headers = mock_post.call_args.kwargs.get("headers", {})
|
|
assert "Authorization" in headers
|
|
assert headers["Authorization"] == f"Bearer {test_api_key}"
|
|
|
|
|
|
def test_bedrock_embedding_with_sigv4():
|
|
"""Test embedding falls back to SigV4 auth when no bearer token is provided"""
|
|
model = "bedrock/amazon.titan-embed-text-v1"
|
|
|
|
with patch(
|
|
"litellm.llms.bedrock.embed.embedding.BedrockEmbedding.embeddings"
|
|
) as mock_bedrock_embed:
|
|
mock_embedding_response = litellm.EmbeddingResponse()
|
|
mock_embedding_response.data = [{"embedding": [0.1, 0.2, 0.3]}]
|
|
mock_bedrock_embed.return_value = mock_embedding_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
aws_region_name="us-west-2",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
mock_bedrock_embed.assert_called_once()
|
|
|
|
|
|
def test_bedrock_titan_v2_encoding_format_float():
|
|
"""Test amazon.titan-embed-text-v2:0 with encoding_format=float parameter"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/amazon.titan-embed-text-v2:0"
|
|
|
|
# Mock response with embeddingsByType for binary format (addressing issue #14680)
|
|
titan_v2_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_v2_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
encoding_format="float", # This should work but currently throws UnsupportedParamsError
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
assert isinstance(response.data[0]["embedding"], list)
|
|
assert len(response.data[0]["embedding"]) == 3
|
|
|
|
# Verify that the request contains embeddingTypes: ["float"] instead of encoding_format
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "embeddingTypes" in request_body
|
|
assert request_body["embeddingTypes"] == ["float"]
|
|
assert "encoding_format" not in request_body
|
|
|
|
|
|
def test_bedrock_titan_v2_encoding_format_base64():
|
|
"""Test amazon.titan-embed-text-v2:0 with encoding_format=base64 parameter (maps to binary)"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/amazon.titan-embed-text-v2:0"
|
|
|
|
# Mock response with embeddingsByType for binary format
|
|
titan_v2_binary_response = {
|
|
"embeddingsByType": {
|
|
"binary": "YmluYXJ5X2VtYmVkZGluZ19kYXRh" # base64 encoded binary data
|
|
},
|
|
"inputTextTokenCount": 10,
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_v2_binary_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
encoding_format="base64", # This should map to embeddingTypes: ["binary"]
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify that the request contains embeddingTypes: ["binary"] for base64 encoding
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "embeddingTypes" in request_body
|
|
assert request_body["embeddingTypes"] == ["binary"]
|
|
|
|
|
|
def test_twelvelabs_input_type_parameter_mapping():
|
|
"""Test that input_type parameter is correctly mapped to inputType for TwelveLabs models"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/twelvelabs.marengo-embed-2-7-v1:0"
|
|
|
|
twelvelabs_response = {
|
|
"data": [{"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}]
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(twelvelabs_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Test with input_type parameter (new LiteLLM parameter)
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
input_type="text", # New parameter that should map to inputType
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
assert isinstance(response.data[0]["embedding"], list)
|
|
assert len(response.data[0]["embedding"]) == 3
|
|
|
|
# Verify that the request contains inputType (mapped from input_type)
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "inputType" in request_body
|
|
assert request_body["inputType"] == "text"
|
|
assert "input_type" not in request_body # Should be mapped, not passed through
|
|
|
|
|
|
def test_twelvelabs_input_type_parameter_mapping_async_invoke():
|
|
"""Test that input_type parameter is correctly mapped to inputType for TwelveLabs async invoke models"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/async_invoke/twelvelabs.marengo-embed-2-7-v1:0"
|
|
|
|
async_invoke_response = {
|
|
"invocationArn": "arn:aws:bedrock:us-east-1:123456789012:async-invoke/abc123def456"
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(async_invoke_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Test with input_type parameter for async invoke
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
output_s3_uri="s3://test-bucket/async-invoke-output/",
|
|
input_type="text", # New parameter that should map to inputType
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
assert hasattr(response, "_hidden_params")
|
|
assert response._hidden_params is not None
|
|
assert hasattr(response._hidden_params, "_invocation_arn")
|
|
|
|
# Verify that the request contains inputType in modelInput (mapped from input_type)
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "modelInput" in request_body
|
|
assert "inputType" in request_body["modelInput"]
|
|
assert request_body["modelInput"]["inputType"] == "text"
|
|
assert "input_type" not in request_body # Should be mapped, not passed through
|
|
|
|
|
|
def test_twelvelabs_missing_input_type_error():
|
|
"""Test that missing input_type parameter defaults to 'text' for TwelveLabs models"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
|
|
# Test TwelveLabs model - should default to 'text' when input_type is missing
|
|
twelvelabs_model = "bedrock/twelvelabs.marengo-embed-2-7-v1:0"
|
|
twelvelabs_response = {
|
|
"data": [{"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}]
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(twelvelabs_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Test that missing input_type defaults to "text" for TwelveLabs
|
|
response = litellm.embedding(
|
|
model=twelvelabs_model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
# No input_type parameter - should default to "text"
|
|
)
|
|
|
|
# Verify the response is successful
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify that the request contains inputType: "text" by default
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "inputType" in request_body
|
|
assert request_body["inputType"] == "text"
|
|
|
|
# Test Amazon Titan model - should NOT throw error (input_type not required)
|
|
titan_model = "bedrock/amazon.titan-embed-text-v1"
|
|
titan_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Test that missing input_type does NOT throw an error for Amazon Titan
|
|
response = litellm.embedding(
|
|
model=titan_model,
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
# No input_type parameter - should work fine
|
|
)
|
|
|
|
# Should succeed without input_type
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,embed_response",
|
|
[
|
|
("bedrock/amazon.titan-embed-text-v1", titan_embedding_response),
|
|
("bedrock/amazon.titan-embed-text-v2:0", titan_embedding_response),
|
|
("bedrock/cohere.embed-english-v3", cohere_embedding_response),
|
|
],
|
|
)
|
|
def test_bedrock_embedding_header_forwarding(model, embed_response):
|
|
"""
|
|
Test that custom headers are correctly forwarded to Bedrock embedding API calls.
|
|
|
|
This test verifies the fix for the issue where headers configured via
|
|
forward_client_headers_to_llm_api were not being passed to Bedrock embedding provider.
|
|
|
|
Relevant Issue: https://github.com/BerriAI/litellm/pull/16042
|
|
"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
|
|
# Headers that would be set by the proxy when forwarding client headers
|
|
custom_headers = {
|
|
"X-Custom-Header": "CustomValue",
|
|
"X-BYOK-Token": "secret-token",
|
|
"Extra-Header": "foobar",
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
try:
|
|
# Call embedding with custom headers via kwargs
|
|
# This simulates what the proxy does when forward_client_headers_to_llm_api is set
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
headers=custom_headers, # This is how proxy passes forwarded headers
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify that the request was made
|
|
assert mock_post.called, "HTTP client post should be called"
|
|
|
|
# Get the actual call arguments
|
|
call_kwargs = mock_post.call_args.kwargs
|
|
headers = call_kwargs.get("headers", {})
|
|
|
|
# Verify our custom headers are present in the request headers
|
|
# Note: AWS SigV4 signing may modify header names to lowercase
|
|
for header_key, header_value in custom_headers.items():
|
|
header_found = (
|
|
header_key in headers
|
|
or header_key.lower() in headers
|
|
or any(k.lower() == header_key.lower() for k in headers.keys())
|
|
)
|
|
assert header_found, (
|
|
f"Header {header_key} should be in request headers. "
|
|
f"Found headers: {list(headers.keys())}"
|
|
)
|
|
|
|
print(f"✓ Test passed for {model}")
|
|
print(f" Headers correctly forwarded: {list(headers.keys())}")
|
|
|
|
except Exception as e:
|
|
pytest.fail(f"Failed to forward headers to {model}: {str(e)}")
|
|
|
|
|
|
def test_bedrock_embedding_extra_headers_and_headers_merge():
|
|
"""
|
|
Test that both extra_headers and headers parameters are correctly merged for Bedrock embeddings.
|
|
|
|
This ensures that headers from kwargs (forwarded by proxy) and extra_headers
|
|
(passed explicitly) are both included in the final headers sent to the provider.
|
|
"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/amazon.titan-embed-text-v1"
|
|
|
|
# Headers from proxy (via kwargs["headers"])
|
|
proxy_headers = {"X-Forwarded-Header": "ProxyValue"}
|
|
|
|
# Explicit extra_headers
|
|
explicit_headers = {"X-Explicit-Header": "ExplicitValue"}
|
|
|
|
# Mock response
|
|
embed_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
try:
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
client=client,
|
|
headers=proxy_headers, # From proxy forwarding
|
|
extra_headers=explicit_headers, # Explicitly passed
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
call_kwargs = mock_post.call_args.kwargs
|
|
headers = call_kwargs.get("headers", {})
|
|
|
|
# Both sets of headers should be present
|
|
# Note: AWS SigV4 signing may modify header names to lowercase
|
|
proxy_header_found = any(
|
|
k.lower() == "x-forwarded-header" for k in headers.keys()
|
|
)
|
|
assert proxy_header_found, (
|
|
"Proxy forwarded header should be present. "
|
|
f"Found headers: {list(headers.keys())}"
|
|
)
|
|
|
|
explicit_header_found = any(
|
|
k.lower() == "x-explicit-header" for k in headers.keys()
|
|
)
|
|
assert explicit_header_found, (
|
|
"Explicitly passed header should be present. "
|
|
f"Found headers: {list(headers.keys())}"
|
|
)
|
|
|
|
print("✓ Both header sources correctly merged and forwarded")
|
|
print(f" Final headers: {list(headers.keys())}")
|
|
|
|
except Exception as e:
|
|
pytest.fail(f"Failed to merge and forward headers: {str(e)}")
|
|
|
|
|
|
def test_bedrock_cohere_v4_embedding_response_parsing():
|
|
"""
|
|
Test parsing of Bedrock Cohere v4 embedding response which returns a dictionary of embeddings
|
|
keyed by type (e.g. 'float', 'int8') instead of a direct list.
|
|
"""
|
|
client = HTTPHandler()
|
|
test_api_key = "test-bearer-token-12345"
|
|
model = "bedrock/cohere.embed-v4:0"
|
|
|
|
# Mock response for Cohere v4 with multiple embedding types
|
|
cohere_v4_response = {
|
|
"embeddings": {"float": [[0.1, 0.2, 0.3]], "int8": [[1, 2, 3]]},
|
|
"response_type": "embeddings_by_type",
|
|
"id": "test-id",
|
|
"texts": ["test input"],
|
|
}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(cohere_v4_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=["test input"],
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key=test_api_key,
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify we get two embedding objects back (one for float, one for int8)
|
|
assert len(response.data) == 2
|
|
|
|
# Check first embedding (float)
|
|
assert response.data[0]["object"] == "embedding"
|
|
assert response.data[0]["embedding"] == [0.1, 0.2, 0.3]
|
|
assert response.data[0]["type"] == "float"
|
|
|
|
# Check second embedding (int8)
|
|
assert response.data[1]["object"] == "embedding"
|
|
assert response.data[1]["embedding"] == [1, 2, 3]
|
|
assert response.data[1]["type"] == "int8"
|
|
|
|
|
|
def test_bedrock_embedding_custom_headers_with_iam_role_and_custom_api_base():
|
|
"""
|
|
Test that custom headers are correctly forwarded when using IAM role credentials
|
|
(with session token) and a custom api_base.
|
|
|
|
This test verifies the fix for the issue where custom headers were not being
|
|
forwarded to Bedrock embeddings endpoint when using:
|
|
- IAM role authentication (session tokens)
|
|
- Custom api_base (proxy endpoint)
|
|
|
|
The fix converts HeadersDict to regular dict before passing to httpx, ensuring
|
|
headers are properly forwarded even with IAM roles and custom endpoints.
|
|
|
|
Relevant Issue: Custom headers not forwarded with IAM roles + custom api_base
|
|
"""
|
|
client = HTTPHandler()
|
|
|
|
# Simulate IAM role credentials with session token
|
|
aws_access_key_id = "AKIAIOSFODNN7EXAMPLE"
|
|
aws_secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
aws_session_token = "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpV3ZXrzoB348V+jZfXvYhEXAMPLEEXAMPLE"
|
|
|
|
# Custom api_base (simulating a proxy endpoint)
|
|
custom_api_base = "https://gateway.example.com/v1/bedrock-runtime/us-east-1"
|
|
|
|
# Custom headers that need to be forwarded
|
|
custom_headers = {
|
|
"X-Custom-Header-1": "test-value-1",
|
|
"X-Custom-Header-2": "test-value-2",
|
|
"X-Forwarded-For": "192.168.1.1",
|
|
"X-BYOK-Token": "secret-token-12345",
|
|
}
|
|
|
|
# Mock response
|
|
embed_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
try:
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-text-v1",
|
|
input=test_input,
|
|
client=client,
|
|
extra_headers=custom_headers,
|
|
api_base=custom_api_base,
|
|
aws_access_key_id=aws_access_key_id,
|
|
aws_secret_access_key=aws_secret_access_key,
|
|
aws_session_token=aws_session_token, # IAM role session token
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify that the request was made
|
|
assert mock_post.called, "HTTP client post should be called"
|
|
|
|
# Get the actual call arguments
|
|
call_kwargs = mock_post.call_args.kwargs
|
|
headers = call_kwargs.get("headers", {})
|
|
|
|
# Verify custom headers are present in the request
|
|
# Note: HeadersDict should be converted to regular dict, so headers should be accessible
|
|
for header_key, header_value in custom_headers.items():
|
|
# Check if header exists (case-insensitive for HTTP headers)
|
|
header_found = any(
|
|
k.lower() == header_key.lower() for k in headers.keys()
|
|
)
|
|
assert header_found, (
|
|
f"Custom header {header_key} should be in request headers. "
|
|
f"Found headers: {list(headers.keys())}"
|
|
)
|
|
|
|
# Verify the value matches
|
|
header_value_found = None
|
|
for k, v in headers.items():
|
|
if k.lower() == header_key.lower():
|
|
header_value_found = v
|
|
break
|
|
|
|
assert header_value_found == header_value, (
|
|
f"Header {header_key} should have value {header_value}, "
|
|
f"but found {header_value_found}"
|
|
)
|
|
|
|
# Verify AWS signature headers are also present
|
|
assert "Authorization" in headers, "AWS signature should be present"
|
|
assert "X-Amz-Date" in headers, "AWS date header should be present"
|
|
assert (
|
|
"X-Amz-Security-Token" in headers
|
|
), "Session token header should be present"
|
|
assert (
|
|
headers["X-Amz-Security-Token"] == aws_session_token
|
|
), "Session token should match the provided token"
|
|
|
|
# Verify the custom api_base was used
|
|
called_url = call_kwargs.get("url", "")
|
|
assert custom_api_base in str(called_url), (
|
|
f"Custom api_base {custom_api_base} should be used. "
|
|
f"Got URL: {called_url}"
|
|
)
|
|
|
|
print(
|
|
"✓ Test passed: Custom headers forwarded with IAM role + custom api_base"
|
|
)
|
|
print(
|
|
f" Custom headers found: {[k for k in headers.keys() if k.lower().startswith('x-custom') or k.lower().startswith('x-forwarded')]}"
|
|
)
|
|
print(
|
|
f" AWS headers found: {[k for k in headers.keys() if k.lower().startswith('x-amz') or k.lower() == 'authorization']}"
|
|
)
|
|
|
|
except Exception as e:
|
|
pytest.fail(
|
|
f"Failed to forward headers with IAM role + custom api_base: {str(e)}"
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_bedrock_embedding_custom_headers_with_iam_role_and_custom_api_base_async():
|
|
"""
|
|
Test that custom headers are correctly forwarded in async mode when using IAM role
|
|
credentials (with session token) and a custom api_base.
|
|
|
|
This is the async version of the test above, verifying the fix works for both
|
|
sync and async embedding calls.
|
|
"""
|
|
client = AsyncHTTPHandler()
|
|
|
|
# Simulate IAM role credentials with session token
|
|
aws_access_key_id = "AKIAIOSFODNN7EXAMPLE"
|
|
aws_secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
|
|
aws_session_token = "AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpV3ZXrzoB348V+jZfXvYhEXAMPLEEXAMPLE"
|
|
|
|
# Custom api_base (simulating a proxy endpoint)
|
|
custom_api_base = "https://gateway.example.com/v1/bedrock-runtime/us-west-2"
|
|
|
|
# Custom headers that need to be forwarded
|
|
custom_headers = {
|
|
"X-Custom-Header-1": "test-value-1",
|
|
"X-Custom-Header-2": "test-value-2",
|
|
"X-Forwarded-For": "192.168.1.1",
|
|
"X-BYOK-Token": "secret-token-12345",
|
|
}
|
|
|
|
# Mock response
|
|
embed_response = {"embedding": [0.1, 0.2, 0.3], "inputTextTokenCount": 10}
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = Mock(return_value=embed_response)
|
|
mock_post.return_value = mock_response
|
|
|
|
try:
|
|
response = await litellm.aembedding(
|
|
model="bedrock/amazon.titan-embed-text-v1",
|
|
input=test_input,
|
|
client=client,
|
|
extra_headers=custom_headers,
|
|
api_base=custom_api_base,
|
|
aws_access_key_id=aws_access_key_id,
|
|
aws_secret_access_key=aws_secret_access_key,
|
|
aws_session_token=aws_session_token, # IAM role session token
|
|
aws_region_name="us-west-2",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
# Verify that the request was made
|
|
assert mock_post.called, "HTTP client post should be called"
|
|
|
|
# Get the actual call arguments
|
|
call_kwargs = mock_post.call_args.kwargs
|
|
headers = call_kwargs.get("headers", {})
|
|
|
|
# Verify custom headers are present in the request
|
|
for header_key, header_value in custom_headers.items():
|
|
# Check if header exists (case-insensitive for HTTP headers)
|
|
header_found = any(
|
|
k.lower() == header_key.lower() for k in headers.keys()
|
|
)
|
|
assert header_found, (
|
|
f"Custom header {header_key} should be in request headers. "
|
|
f"Found headers: {list(headers.keys())}"
|
|
)
|
|
|
|
# Verify the value matches
|
|
header_value_found = None
|
|
for k, v in headers.items():
|
|
if k.lower() == header_key.lower():
|
|
header_value_found = v
|
|
break
|
|
|
|
assert header_value_found == header_value, (
|
|
f"Header {header_key} should have value {header_value}, "
|
|
f"but found {header_value_found}"
|
|
)
|
|
|
|
# Verify AWS signature headers are also present
|
|
assert "Authorization" in headers, "AWS signature should be present"
|
|
assert "X-Amz-Date" in headers, "AWS date header should be present"
|
|
assert (
|
|
"X-Amz-Security-Token" in headers
|
|
), "Session token header should be present"
|
|
assert (
|
|
headers["X-Amz-Security-Token"] == aws_session_token
|
|
), "Session token should match the provided token"
|
|
|
|
# Verify the custom api_base was used
|
|
called_url = call_kwargs.get("url", "")
|
|
assert custom_api_base in str(called_url), (
|
|
f"Custom api_base {custom_api_base} should be used. "
|
|
f"Got URL: {called_url}"
|
|
)
|
|
|
|
print(
|
|
"✓ Test passed (async): Custom headers forwarded with IAM role + custom api_base"
|
|
)
|
|
print(
|
|
f" Custom headers found: {[k for k in headers.keys() if k.lower().startswith('x-custom') or k.lower().startswith('x-forwarded')]}"
|
|
)
|
|
print(
|
|
f" AWS headers found: {[k for k in headers.keys() if k.lower().startswith('x-amz') or k.lower() == 'authorization']}"
|
|
)
|
|
|
|
except Exception as e:
|
|
pytest.fail(
|
|
f"Failed to forward headers with IAM role + custom api_base (async): {str(e)}"
|
|
)
|
|
|
|
|
|
def test_titan_multimodal_embedding_image_cost_tracking():
|
|
"""Test that Titan multimodal embedding with image input populates image_count in Usage."""
|
|
from litellm.llms.bedrock.embed.amazon_titan_multimodal_transformation import (
|
|
AmazonTitanMultimodalEmbeddingG1Config,
|
|
)
|
|
|
|
config = AmazonTitanMultimodalEmbeddingG1Config()
|
|
|
|
# Simulate response from AWS Bedrock
|
|
response_list = [
|
|
{
|
|
"embedding": [0.1, 0.2, 0.3],
|
|
"inputTextTokenCount": 0,
|
|
}
|
|
]
|
|
|
|
# Simulate batch_data with an image request (inputImage key set by _transform_request)
|
|
batch_data = [{"inputImage": "/9j/4AAQSkZJRg=="}]
|
|
|
|
result = config.transform_response(
|
|
response_list=response_list,
|
|
model="amazon.titan-embed-image-v1",
|
|
batch_data=batch_data,
|
|
)
|
|
|
|
assert result.usage is not None
|
|
assert result.usage.prompt_tokens_details is not None
|
|
assert result.usage.prompt_tokens_details.image_count == 1
|
|
|
|
|
|
def test_titan_multimodal_embedding_text_no_image_count():
|
|
"""Test that Titan multimodal embedding with text-only input does not set image_count."""
|
|
from litellm.llms.bedrock.embed.amazon_titan_multimodal_transformation import (
|
|
AmazonTitanMultimodalEmbeddingG1Config,
|
|
)
|
|
|
|
config = AmazonTitanMultimodalEmbeddingG1Config()
|
|
|
|
response_list = [
|
|
{
|
|
"embedding": [0.1, 0.2, 0.3],
|
|
"inputTextTokenCount": 5,
|
|
}
|
|
]
|
|
|
|
# Text-only request — no inputImage key
|
|
batch_data = [{"inputText": "hello world"}]
|
|
|
|
result = config.transform_response(
|
|
response_list=response_list,
|
|
model="amazon.titan-embed-image-v1",
|
|
batch_data=batch_data,
|
|
)
|
|
|
|
assert result.usage is not None
|
|
# prompt_tokens_details should be None for text-only (no image_count to report)
|
|
assert result.usage.prompt_tokens_details is None
|
|
|
|
|
|
def test_titan_multimodal_embedding_backward_compat_no_batch_data():
|
|
"""Test that Titan transformer works without batch_data (backward compatibility)."""
|
|
from litellm.llms.bedrock.embed.amazon_titan_multimodal_transformation import (
|
|
AmazonTitanMultimodalEmbeddingG1Config,
|
|
)
|
|
|
|
config = AmazonTitanMultimodalEmbeddingG1Config()
|
|
|
|
response_list = [
|
|
{
|
|
"embedding": [0.1, 0.2, 0.3],
|
|
"inputTextTokenCount": 5,
|
|
}
|
|
]
|
|
|
|
# Call without batch_data — should not break
|
|
result = config.transform_response(
|
|
response_list=response_list,
|
|
model="amazon.titan-embed-image-v1",
|
|
)
|
|
|
|
assert result.usage is not None
|
|
assert result.usage.prompt_tokens == 5
|
|
assert result.usage.prompt_tokens_details is None
|
|
|
|
|
|
def test_titan_image_embedding_cost_uses_per_image_rate():
|
|
"""
|
|
End-to-end test: Titan image embedding with mocked AWS response
|
|
should populate image_count for correct per-image cost calculation.
|
|
"""
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
embed_response = {
|
|
"embedding": [0.1] * 1024,
|
|
"inputTextTokenCount": 0,
|
|
}
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-image-v1",
|
|
input=["data:image/png;base64,iVBORw0KGgoAAAANSUhEUg=="],
|
|
client=client,
|
|
aws_access_key_id="fake",
|
|
aws_secret_access_key="fake",
|
|
aws_region_name="us-east-1",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
assert response.usage is not None
|
|
assert response.usage.prompt_tokens_details is not None
|
|
assert response.usage.prompt_tokens_details.image_count == 1
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"encoding_format,expected_embedding_types",
|
|
[
|
|
("float", ["float"]),
|
|
("base64", ["float"]),
|
|
(["float", "int8"], ["float", "int8"]),
|
|
],
|
|
)
|
|
def test_bedrock_cohere_embedding_types_wrapped_as_list(
|
|
encoding_format, expected_embedding_types
|
|
):
|
|
"""
|
|
Bedrock Cohere expects `embedding_types` as a JSON array, not a raw string.
|
|
|
|
Regression test for: Bedrock returns
|
|
Malformed input request: #/embedding_types: expected type: JSONArray, found: String
|
|
when `encoding_format` is passed as a string.
|
|
"""
|
|
client = HTTPHandler()
|
|
model = "bedrock/cohere.embed-multilingual-v3"
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(cohere_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=test_input,
|
|
encoding_format=encoding_format,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-east-1.amazonaws.com",
|
|
api_key="test-bearer-token-12345",
|
|
)
|
|
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
|
|
request_body = json.loads(mock_post.call_args.kwargs.get("data", "{}"))
|
|
assert "embedding_types" in request_body
|
|
assert request_body["embedding_types"] == expected_embedding_types
|
|
assert isinstance(request_body["embedding_types"], list)
|
|
|
|
|
|
def test_load_credentials_assumes_role_with_external_id(monkeypatch):
|
|
"""A trust policy requiring sts:ExternalId must be satisfied by the deployment's aws_external_id."""
|
|
import datetime
|
|
|
|
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
from litellm.llms.bedrock.embed.embedding import BedrockEmbedding
|
|
|
|
monkeypatch.delenv("AWS_EXTERNAL_ID", raising=False)
|
|
|
|
class FakeSTSClient:
|
|
def get_caller_identity(self):
|
|
return {"Arn": "arn:aws:iam::111111111111:user/litellm-proxy-pod"}
|
|
|
|
def assume_role(self, **params):
|
|
if params.get("ExternalId") != "external-id-embed":
|
|
raise ClientError(
|
|
{"Error": {"Code": "AccessDenied", "Message": "is not authorized to perform: sts:AssumeRole"}},
|
|
"AssumeRole",
|
|
)
|
|
return {
|
|
"Credentials": {
|
|
"AccessKeyId": "ASIAEMBEDROLEKEY",
|
|
"SecretAccessKey": "assumed-secret",
|
|
"SessionToken": "assumed-session-token",
|
|
"Expiration": datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=30),
|
|
}
|
|
}
|
|
|
|
optional_params = {
|
|
"aws_access_key_id": "AKIAEMBEDCALLERKEY",
|
|
"aws_secret_access_key": "pod-caller-secret",
|
|
"aws_region_name": "us-east-1",
|
|
"aws_role_name": "arn:aws:iam::999999999999:role/litellm-embed-role",
|
|
"aws_session_name": "litellm-embed-session",
|
|
"aws_external_id": "external-id-embed",
|
|
}
|
|
|
|
with patch.object(boto3, "client", return_value=FakeSTSClient()):
|
|
credentials, aws_region_name = BedrockEmbedding()._load_credentials(optional_params)
|
|
|
|
assert credentials.access_key == "ASIAEMBEDROLEKEY"
|
|
assert credentials.token == "assumed-session-token"
|
|
assert aws_region_name == "us-east-1"
|
|
assert "aws_external_id" not in optional_params
|
|
|
|
|
|
def test_embedding_session_tags_sign_the_request_and_stay_out_of_the_body(monkeypatch):
|
|
"""The tagged STS session signs the InvokeModel call and the tags never reach the body (#34069)."""
|
|
import datetime
|
|
|
|
import boto3
|
|
from botocore.exceptions import ClientError
|
|
|
|
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
|
|
monkeypatch.delenv("AWS_WEB_IDENTITY_TOKEN_FILE", raising=False)
|
|
monkeypatch.delenv("AWS_ROLE_ARN", raising=False)
|
|
tags = [{"Key": "team", "Value": "genai"}]
|
|
|
|
class FakeSTSClient:
|
|
def get_caller_identity(self):
|
|
return {"Arn": "arn:aws:iam::111111111111:user/litellm-proxy-pod"}
|
|
|
|
def assume_role(self, **params):
|
|
if list(params.get("Tags", ())) != tags:
|
|
raise ClientError(
|
|
{"Error": {"Code": "AccessDenied", "Message": "is not authorized to perform: sts:TagSession"}},
|
|
"AssumeRole",
|
|
)
|
|
return {
|
|
"Credentials": {
|
|
"AccessKeyId": "ASIAEMBEDTAGGED",
|
|
"SecretAccessKey": "assumed-secret",
|
|
"SessionToken": "assumed-session-token",
|
|
"Expiration": datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=30),
|
|
}
|
|
}
|
|
|
|
client = HTTPHandler()
|
|
with patch.object(boto3, "client", return_value=FakeSTSClient()), patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-text-v1",
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
aws_access_key_id="AKIAEMBEDCALLERKEY",
|
|
aws_secret_access_key="pod-caller-secret",
|
|
aws_role_name="arn:aws:iam::999999999999:role/litellm-embed-role",
|
|
aws_session_name="litellm-embed-session",
|
|
aws_session_tags=tags,
|
|
)
|
|
|
|
assert response.data[0]["embedding"] == titan_embedding_response["embedding"]
|
|
sent = mock_post.call_args.kwargs
|
|
assert "Credential=ASIAEMBEDTAGGED/" in sent["headers"]["Authorization"]
|
|
assert "aws_session_tags" not in sent["data"]
|
|
|
|
|
|
def test_bedrock_embedding_bearer_token_never_runs_the_sigv4_credential_chain(monkeypatch):
|
|
"""The deployment's AWS profile does not exist, so resolving SigV4 credentials
|
|
raises; a bearer-token deployment must still serve the request, since the
|
|
bearer token alone signs it."""
|
|
monkeypatch.setenv("AWS_BEARER_TOKEN_BEDROCK", "env-bearer-token-12345")
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-text-v1",
|
|
input=test_input,
|
|
client=client,
|
|
aws_region_name="us-west-2",
|
|
aws_profile_name="litellm-no-such-aws-profile",
|
|
)
|
|
|
|
assert response.data[0]["embedding"] == titan_embedding_response["embedding"]
|
|
assert mock_post.call_args.kwargs["headers"]["Authorization"] == "Bearer env-bearer-token-12345"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_single_func_embeddings_signs_off_the_event_loop(monkeypatch):
|
|
"""Regression for issue #40165: Titan, Nova, and TwelveLabs embeddings sign one SigV4 request per
|
|
input, and botocore refreshes expiring credentials inside that signing with a blocking HTTP call,
|
|
so each signing must run on a worker thread to keep the loop serving other requests."""
|
|
monkeypatch.delenv("AWS_BEARER_TOKEN_BEDROCK", raising=False)
|
|
probe = EventLoopProbe()
|
|
client = MagicMock()
|
|
client.__class__ = AsyncHTTPHandler
|
|
client.post = AsyncMock(
|
|
return_value=httpx.Response(
|
|
200,
|
|
json=titan_embedding_response,
|
|
request=httpx.Request("POST", "https://bedrock-runtime.us-west-2.amazonaws.com"),
|
|
)
|
|
)
|
|
|
|
release = asyncio.create_task(probe.release_refresh_from_the_loop())
|
|
response = await BedrockEmbedding()._async_single_func_embeddings(
|
|
client=client,
|
|
timeout=None,
|
|
batch_data=[{"inputText": test_input}],
|
|
credentials=probe.credentials(),
|
|
extra_headers=None,
|
|
endpoint_url="https://bedrock-runtime.us-west-2.amazonaws.com/model/amazon.titan-embed-text-v1/invoke",
|
|
aws_region_name="us-west-2",
|
|
model="amazon.titan-embed-text-v1",
|
|
logging_obj=MagicMock(),
|
|
provider="amazon",
|
|
)
|
|
await release
|
|
|
|
assert response.data[0]["embedding"] == titan_embedding_response["embedding"]
|
|
assert "Authorization" in client.post.call_args.kwargs["headers"]
|
|
assert probe.served_during_refresh is True
|
|
marengo_3_embedding_response = {"data": [{"embedding": [0.01 * i for i in range(512)]}]}
|
|
MARENGO_3_DUCK = "data:image/png;base64,ZHVjaw=="
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,kwargs,expected_body,expected_usage_details",
|
|
[
|
|
(
|
|
"bedrock/us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
{"input_type": "text"},
|
|
{"inputType": "text", "text": {"inputText": "a duck on water"}},
|
|
{"query_count": 1},
|
|
),
|
|
(
|
|
"bedrock/twelvelabs.marengo-embed-3-0-v1:0",
|
|
{"input_type": "text"},
|
|
{"inputType": "text", "text": {"inputText": "a duck on water"}},
|
|
{"query_count": 1},
|
|
),
|
|
(
|
|
"bedrock/us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
{"input_type": "text_image", "media_source": MARENGO_3_DUCK},
|
|
{
|
|
"inputType": "text_image",
|
|
"text_image": {"inputText": "a duck on water", "mediaSource": {"base64String": "ZHVjaw=="}},
|
|
},
|
|
{"query_count": 1, "image_count": 1},
|
|
),
|
|
(
|
|
"bedrock/us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
{"input_type": "multi_input", "media_sources": {"bird": MARENGO_3_DUCK}},
|
|
{
|
|
"inputType": "multi_input",
|
|
"multi_input": {
|
|
"inputText": "a duck on water",
|
|
"mediaSources": [{"name": "bird", "mediaType": "image", "base64String": "ZHVjaw=="}],
|
|
},
|
|
},
|
|
{"query_count": 1, "image_count": 1},
|
|
),
|
|
],
|
|
)
|
|
def test_marengo_3_embedding_sends_the_nested_payload_and_parses_512_dims(
|
|
model, kwargs, expected_body, expected_usage_details
|
|
):
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(marengo_3_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input="a duck on water",
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
api_key="test-bearer-token-12345",
|
|
**kwargs,
|
|
)
|
|
|
|
assert json.loads(mock_post.call_args.kwargs["data"]) == expected_body
|
|
assert mock_post.call_args.kwargs["url"].endswith(f"/model/{model.removeprefix('bedrock/').replace(':', '%3A')}/invoke")
|
|
assert len(response.data[0]["embedding"]) == 512
|
|
assert response.data[0]["embedding"][:2] == [0.0, 0.01]
|
|
assert response.usage.prompt_tokens == 0
|
|
assert response.usage.total_tokens == 0
|
|
assert response.usage.prompt_tokens_details.model_dump(exclude_none=True) == expected_usage_details
|
|
|
|
|
|
def test_marengo_3_image_embedding_sends_the_media_under_the_image_key():
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(marengo_3_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
input=MARENGO_3_DUCK,
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
api_key="test-bearer-token-12345",
|
|
input_type="image",
|
|
)
|
|
|
|
assert json.loads(mock_post.call_args.kwargs["data"]) == {
|
|
"inputType": "image",
|
|
"image": {"mediaSource": {"base64String": "ZHVjaw=="}},
|
|
}
|
|
assert len(response.data[0]["embedding"]) == 512
|
|
assert response.data[0]["embedding"][:2] == [0.0, 0.01]
|
|
assert response.usage.prompt_tokens == 0
|
|
assert response.usage.prompt_tokens_details.model_dump(exclude_none=True) == {"image_count": 1}
|
|
|
|
|
|
def test_marengo_2_7_embedding_keeps_the_flat_payload():
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(twelvelabs_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/us.twelvelabs.marengo-embed-2-7-v1:0",
|
|
input="a duck on water",
|
|
client=client,
|
|
aws_region_name="us-east-1",
|
|
api_key="test-bearer-token-12345",
|
|
input_type="text",
|
|
)
|
|
|
|
assert json.loads(mock_post.call_args.kwargs["data"]) == {
|
|
"inputType": "text",
|
|
"inputText": "a duck on water",
|
|
"textTruncate": "end",
|
|
}
|
|
assert response.data[0]["embedding"] == [0.1, 0.2, 0.3]
|
|
assert response.usage.prompt_tokens == 0
|
|
assert response.usage.prompt_tokens_details.model_dump(exclude_none=True) == {"query_count": 1}
|
|
|
|
|
|
def test_marengo_usage_counts_text_requests_and_images_across_a_batch():
|
|
duck = {"mediaType": "image", "base64String": "ZHVjaw=="}
|
|
response = TwelveLabsMarengoEmbeddingConfig().transform_response(
|
|
response_list=[marengo_3_embedding_response, marengo_3_embedding_response, marengo_3_embedding_response],
|
|
model="us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
batch_data=[
|
|
{"inputType": "text", "text": {"inputText": "a duck"}},
|
|
{"inputType": "image", "image": {"mediaSource": {"base64String": "ZHVjaw=="}}},
|
|
{
|
|
"inputType": "multi_input",
|
|
"multi_input": {"mediaSources": [{"name": "a", **duck}, {"name": "b", **duck}]},
|
|
},
|
|
],
|
|
)
|
|
|
|
assert [item["index"] for item in response.data] == [0, 1, 2]
|
|
assert response.usage.prompt_tokens == 0
|
|
assert response.usage.total_tokens == 0
|
|
assert response.usage.prompt_tokens_details.model_dump(exclude_none=True) == {"query_count": 1, "image_count": 3}
|
|
|
|
|
|
def test_marengo_usage_without_request_data_bills_nothing():
|
|
response = TwelveLabsMarengoEmbeddingConfig().transform_response(
|
|
response_list=[marengo_3_embedding_response], model="us.twelvelabs.marengo-embed-3-0-v1:0"
|
|
)
|
|
|
|
assert len(response.data[0]["embedding"]) == 512
|
|
assert response.usage.prompt_tokens == 0
|
|
assert response.usage.prompt_tokens_details is None
|
|
|
|
|
|
def test_marengo_response_items_without_an_embedding_are_skipped():
|
|
response = TwelveLabsMarengoEmbeddingConfig().transform_response(
|
|
response_list=[{"data": [{"embeddingOption": "visual-text", "startSec": 0.0}, {"embedding": [0.1, 0.2, 0.3]}]}],
|
|
model="us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
)
|
|
|
|
assert [item["embedding"] for item in response.data] == [[0.1, 0.2, 0.3]]
|
|
assert response.data[0]["index"] == 0
|
|
|
|
|
|
def test_marengo_3_text_image_without_media_source_is_a_bad_request():
|
|
with pytest.raises(litellm.BadRequestError, match=r"text_image.*media_source"):
|
|
litellm.embedding(
|
|
model="bedrock/us.twelvelabs.marengo-embed-3-0-v1:0",
|
|
input="a duck on water",
|
|
aws_region_name="us-east-1",
|
|
api_key="test-bearer-token-12345",
|
|
input_type="text_image",
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"model,input_type,embed_response",
|
|
[
|
|
(
|
|
"bedrock/amazon.titan-embed-text-v1",
|
|
"text",
|
|
titan_embedding_response,
|
|
), # V1 text model
|
|
(
|
|
"bedrock/amazon.titan-embed-text-v2:0",
|
|
"text",
|
|
titan_embedding_response,
|
|
), # V2 text model
|
|
(
|
|
"bedrock/amazon.titan-embed-g1-text-02",
|
|
"text",
|
|
titan_embedding_response,
|
|
), # G1 text model
|
|
(
|
|
"bedrock/amazon.titan-embed-image-v1",
|
|
"image",
|
|
titan_embedding_response,
|
|
), # Image model
|
|
(
|
|
"bedrock/cohere.embed-english-v3",
|
|
"text",
|
|
cohere_embedding_response,
|
|
), # Cohere English
|
|
(
|
|
"bedrock/cohere.embed-multilingual-v3",
|
|
"text",
|
|
cohere_embedding_response,
|
|
), # Cohere Multilingual
|
|
],
|
|
)
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
def test_bedrock_embedding_models(model, input_type, embed_response):
|
|
"""Test embedding functionality for all Bedrock models with different input types"""
|
|
litellm.set_verbose = True
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(embed_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Prepare input based on type
|
|
input_data = (
|
|
img_base_64 if input_type == "image" else "Hello world from litellm"
|
|
)
|
|
|
|
try:
|
|
response = litellm.embedding(
|
|
model=model,
|
|
input=input_data,
|
|
client=client,
|
|
aws_region_name="us-west-2",
|
|
aws_bedrock_runtime_endpoint="https://bedrock-runtime.us-west-2.amazonaws.com",
|
|
)
|
|
|
|
# Verify response structure
|
|
assert isinstance(response, litellm.EmbeddingResponse)
|
|
print(response.data)
|
|
assert isinstance(response.data[0]["embedding"], list)
|
|
assert len(response.data[0]["embedding"]) == 3 # Based on mock response
|
|
|
|
# Fetch request body
|
|
request_data = json.loads(mock_post.call_args.kwargs["data"])
|
|
|
|
# Verify AWS params are not in request body
|
|
aws_params = ["aws_region_name", "aws_bedrock_runtime_endpoint"]
|
|
for param in aws_params:
|
|
assert (
|
|
param not in request_data
|
|
), f"AWS param {param} should not be in request body"
|
|
|
|
except Exception as e:
|
|
pytest.fail(f"Error occurred: {e}")
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
def test_e2e_bedrock_async_invoke_embedding_twelvelabs_marengo():
|
|
"""
|
|
Test async invoke embedding with TwelveLabs Marengo.
|
|
Validates that async invoke responses include job ID in hidden parameters.
|
|
"""
|
|
print("Testing async invoke embedding...")
|
|
original_region_name = os.environ.get("AWS_REGION_NAME")
|
|
os.environ["AWS_REGION_NAME"] = "us-east-1"
|
|
litellm.turn_on_debug()
|
|
|
|
# Mock the HTTP call to return async invoke response
|
|
with patch(
|
|
"litellm.llms.bedrock.embed.embedding.BedrockEmbedding._make_sync_call"
|
|
) as mock_call:
|
|
mock_call.return_value = {
|
|
"invocationArn": "arn:aws:bedrock:us-east-1:123456789012:async-invoke/test-job-123"
|
|
}
|
|
|
|
response = litellm.embedding(
|
|
model="bedrock/async_invoke/us.twelvelabs.marengo-embed-2-7-v1:0",
|
|
input=["Hello world from LiteLLM async invoke!"],
|
|
aws_region_name="us-east-1",
|
|
inputType="text",
|
|
output_s3_uri="s3://test-bucket/async-invoke-output/",
|
|
)
|
|
|
|
# Validate response structure
|
|
assert isinstance(
|
|
response, litellm.EmbeddingResponse
|
|
), "Response should be EmbeddingResponse type"
|
|
assert hasattr(
|
|
response, "_hidden_params"
|
|
), "Response should have _hidden_params"
|
|
assert response._hidden_params is not None, "Hidden params should not be None"
|
|
|
|
# Validate hidden params contain invocation ARN
|
|
assert hasattr(
|
|
response._hidden_params, "_invocation_arn"
|
|
), "Hidden params should have _invocation_arn"
|
|
assert (
|
|
response._hidden_params._invocation_arn
|
|
== "arn:aws:bedrock:us-east-1:123456789012:async-invoke/test-job-123"
|
|
), "Invocation ARN should be preserved"
|
|
|
|
# Validate embedding structure
|
|
assert len(response.data) == 1, "Should have one embedding"
|
|
assert (
|
|
response.data[0].object == "embedding"
|
|
), "Embedding object should be 'embedding'"
|
|
assert (
|
|
response.data[0].embedding == []
|
|
), "Embedding should be empty for async jobs"
|
|
|
|
print(
|
|
f"Async invoke embedding successful! Invocation ARN: {response._hidden_params._invocation_arn}"
|
|
)
|
|
|
|
# Restore original region name
|
|
if original_region_name:
|
|
os.environ["AWS_REGION_NAME"] = original_region_name
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
async def test_e2e_bedrock_async_invoke_embedding_async_twelvelabs_marengo():
|
|
"""
|
|
Test async invoke embedding with async calls.
|
|
Validates that async invoke responses work with aembedding.
|
|
"""
|
|
print("Testing async invoke embedding with async calls...")
|
|
original_region_name = os.environ.get("AWS_REGION_NAME")
|
|
os.environ["AWS_REGION_NAME"] = "us-east-1"
|
|
litellm.turn_on_debug()
|
|
|
|
# Mock the async HTTP call to return async invoke response
|
|
with patch(
|
|
"litellm.llms.bedrock.embed.embedding.BedrockEmbedding._make_async_call"
|
|
) as mock_call:
|
|
mock_call.return_value = {
|
|
"invocationArn": "arn:aws:bedrock:us-east-1:123456789012:async-invoke/test-async-job-456"
|
|
}
|
|
|
|
response = await litellm.aembedding(
|
|
model="bedrock/async_invoke/us.twelvelabs.marengo-embed-2-7-v1:0",
|
|
input=["Hello world from LiteLLM async invoke async!"],
|
|
aws_region_name="us-east-1",
|
|
inputType="text",
|
|
output_s3_uri="s3://test-bucket/async-invoke-output/",
|
|
)
|
|
|
|
# Validate response structure
|
|
assert isinstance(
|
|
response, litellm.EmbeddingResponse
|
|
), "Response should be EmbeddingResponse type"
|
|
assert hasattr(
|
|
response, "_hidden_params"
|
|
), "Response should have _hidden_params"
|
|
assert response._hidden_params is not None, "Hidden params should not be None"
|
|
|
|
# Validate hidden params contain invocation ARN
|
|
assert hasattr(
|
|
response._hidden_params, "_invocation_arn"
|
|
), "Hidden params should have _invocation_arn"
|
|
assert (
|
|
response._hidden_params._invocation_arn
|
|
== "arn:aws:bedrock:us-east-1:123456789012:async-invoke/test-async-job-456"
|
|
), "Invocation ARN should be preserved"
|
|
|
|
print(
|
|
f"Async invoke embedding successful! Invocation ARN: {response._hidden_params._invocation_arn}"
|
|
)
|
|
|
|
# Restore original region name
|
|
if original_region_name:
|
|
os.environ["AWS_REGION_NAME"] = original_region_name
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
def test_bedrock_embedding_uses_correct_region_when_specified():
|
|
"""
|
|
Test that when aws_region_name is explicitly passed, it's used correctly
|
|
even if AWS_REGION_NAME env var is set to a different region.
|
|
|
|
relevant issue: https://github.com/BerriAI/litellm/issues/16517
|
|
"""
|
|
# Save original env var
|
|
original_region_name = os.environ.get("AWS_REGION_NAME")
|
|
|
|
# Set env var to a different region (this should NOT be used)
|
|
os.environ["AWS_REGION_NAME"] = "ap-northeast-1"
|
|
|
|
try:
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Call with explicit region
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-image-v1",
|
|
input=["test input"],
|
|
client=client,
|
|
aws_region_name="us-east-1", # Explicitly set to us-east-1
|
|
)
|
|
|
|
# Verify the request was made to the correct region
|
|
assert mock_post.called, "HTTP post should have been called"
|
|
|
|
# Get the URL from the call
|
|
call_args = mock_post.call_args
|
|
url = call_args.kwargs.get("url", "")
|
|
|
|
# The URL should contain us-east-1, NOT ap-northeast-1
|
|
assert "us-east-1" in url, f"URL should contain us-east-1, but got: {url}"
|
|
assert (
|
|
"ap-northeast-1" not in url
|
|
), f"URL should NOT contain ap-northeast-1, but got: {url}"
|
|
|
|
print(f"✓ Test passed: URL contains correct region: {url}")
|
|
|
|
finally:
|
|
# Restore original env var
|
|
if original_region_name:
|
|
os.environ["AWS_REGION_NAME"] = original_region_name
|
|
else:
|
|
os.environ.pop("AWS_REGION_NAME", None)
|
|
|
|
|
|
@pytest.mark.usefixtures("fake_provider_credentials")
|
|
def test_bedrock_embedding_region_bug_reproduction():
|
|
"""
|
|
Reproduces the bug where aws_region_name is ignored when passed explicitly.
|
|
|
|
relevant issue: https://github.com/BerriAI/litellm/issues/16517
|
|
"""
|
|
# Save original env var
|
|
original_region_name = os.environ.get("AWS_REGION_NAME")
|
|
|
|
# Set env var to ap-northeast-1 (this is what the bug report shows)
|
|
os.environ["AWS_REGION_NAME"] = "ap-northeast-1"
|
|
|
|
try:
|
|
client = HTTPHandler()
|
|
|
|
with patch.object(client, "post") as mock_post:
|
|
mock_response = Mock()
|
|
mock_response.status_code = 200
|
|
mock_response.text = json.dumps(titan_embedding_response)
|
|
mock_response.json = lambda: json.loads(mock_response.text)
|
|
mock_post.return_value = mock_response
|
|
|
|
# Call with explicit region (as in the bug report)
|
|
response = litellm.embedding(
|
|
model="bedrock/amazon.titan-embed-image-v1",
|
|
input=["test input"],
|
|
client=client,
|
|
aws_region_name="us-east-1", # Explicitly set to us-east-1
|
|
)
|
|
|
|
# Verify the request was made
|
|
assert mock_post.called, "HTTP post should have been called"
|
|
|
|
# Get the URL from the call
|
|
call_args = mock_post.call_args
|
|
url = call_args.kwargs.get("url", "")
|
|
|
|
print(f"Request URL: {url}")
|
|
print(f"Expected region in URL: us-east-1")
|
|
print(f"Environment AWS_REGION_NAME: {os.environ.get('AWS_REGION_NAME')}")
|
|
|
|
# This assertion will FAIL if the bug exists (it will use ap-northeast-1)
|
|
# This assertion will PASS if the bug is fixed (it will use us-east-1)
|
|
if "ap-northeast-1" in url:
|
|
print(
|
|
"❌ BUG REPRODUCED: Using wrong region from env var instead of explicit parameter"
|
|
)
|
|
pytest.fail(f"Bug reproduced: URL contains ap-northeast-1 instead of us-east-1. URL: {url}")
|
|
else:
|
|
print(
|
|
"✓ Bug NOT reproduced: Using correct region from explicit parameter"
|
|
)
|
|
assert (
|
|
"us-east-1" in url
|
|
), f"URL should contain us-east-1, but got: {url}"
|
|
|
|
finally:
|
|
# Restore original env var
|
|
if original_region_name:
|
|
os.environ["AWS_REGION_NAME"] = original_region_name
|
|
else:
|
|
os.environ.pop("AWS_REGION_NAME", None)
|
|
|
|
|
|
img_base_64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkBAMAAACCzIhnAAAAG1BMVEURAAD///+ln5/h39/Dv79qX18uHx+If39MPz9oMSdmAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABB0lEQVRYhe2SzWrEIBCAh2A0jxEs4j6GLDS9hqWmV5Flt0cJS+lRwv742DXpEjY1kOZW6HwHFZnPmVEBEARBEARB/jd0KYA/bcUYbPrRLh6amXHJ/K+ypMoyUaGthILzw0l+xI0jsO7ZcmCcm4ILd+QuVYgpHOmDmz6jBeJImdcUCmeBqQpuqRIbVmQsLCrAalrGpfoEqEogqbLTWuXCPCo+Ki1XGqgQ+jVVuhB8bOaHkvmYuzm/b0KYLWwoK58oFqi6XfxQ4Uz7d6WeKpna6ytUs5e8betMcqAv5YPC5EZB2Lm9FIn0/VP6R58+/GEY1X1egVoZ/3bt/EqF6malgSAIgiDIH+QL41409QMY0LMAAAAASUVORK5CYII="
|