Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion litellm/llms/bedrock/embed/cohere_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def map_openai_params(
) -> 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
Expand Down
47 changes: 47 additions & 0 deletions tests/test_litellm/llms/bedrock/embed/test_bedrock_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


@pytest.mark.parametrize(
"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()
Comment on lines +970 to +981

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 litellm.set_verbose = True is set globally without being restored, so the verbose flag leaks into tests that run later in the same process. Wrapping it with a fixture or saving/restoring the original value keeps the test hermetic.

Suggested change
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()
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.
"""
original_verbose = litellm.set_verbose
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)
Loading