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]

fix(bedrock/cohere): send embedding_types as JSON array, not string (#28172)

ishaan-berri8c6625216bbe
diff --git a/litellm/llms/bedrock/embed/cohere_transformation.py b/litellm/llms/bedrock/embed/cohere_transformation.py
index d00cb74aae0..2c0dc834144 100644
--- a/litellm/llms/bedrock/embed/cohere_transformation.py
+++ b/litellm/llms/bedrock/embed/cohere_transformation.py
@@ -22,7 +22,7 @@ class BedrockCohereEmbeddingConfig:
) -> dict:
for k, v in non_default_params.items():
if k == "encoding_format":
- optional_params["embedding_types"] = v
+ optional_params["embedding_types"] = v if isinstance(v, list) else [v]
elif k == "dimensions":
optional_params["output_dimension"] = v
return optional_params
diff --git a/tests/test_litellm/llms/bedrock/embed/test_bedrock_embedding.py b/tests/test_litellm/llms/bedrock/embed/test_bedrock_embedding.py
index c67a8712340..9955851132c 100644
--- a/tests/test_litellm/llms/bedrock/embed/test_bedrock_embedding.py
+++ b/tests/test_litellm/llms/bedrock/embed/test_bedrock_embedding.py
@@ -957,3 +957,50 @@ def test_titan_image_embedding_cost_uses_per_image_rate():
assert response.usage is not None
assert response.usage.prompt_tokens_details is not None
assert response.usage.prompt_tokens_details.image_count == 1
+
+
+ "encoding_format,expected_embedding_types",
+ [
+ ("float", ["float"]),
+ ("base64", ["base64"]),
+ (["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.
+ """
+ litellm.set_verbose = True
+ 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)