Skip to content

Python: Fix Bedrock Cohere embeddings when embedding_types is set - #14678

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/bedrock-cohere-embedding-types
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/bedrock-cohere-embedding-types

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Motivation and Context

BedrockTextEmbedding accepts the Cohere embedding_types option and forwards it in the request body. With it set, Cohere on Bedrock returns the embeddings keyed by type ({"embeddings": {"float": [[...]], "int8": [[...]]}}) instead of a plain list of vectors, and parse_text_embedding_response only accepted a list, so the call failed with ServiceInvalidResponseError: The response from Cohere model does not contain embeddings. even though the request succeeded.

Description

parse_text_embedding_response in bedrock_cohere.py now also accepts the keyed form: it returns the float embedding when that type is present, otherwise the first type in the response. The plain list form and the error for missing or malformed embeddings are unchanged.

Tests (tests/unit/connectors/ai/bedrock/services/test_bedrock_text_embedding_generation.py): typed responses (float only, float plus int8, int8 only) and the plain list through generate_embeddings with embedding_types in the settings, and empty or malformed typed responses still raising ServiceInvalidResponseError. tests/unit/connectors/ai/bedrock: 123 passed; 3 of the new cases fail without the change. Not run against the real Bedrock API.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • I didn't break anyone 😄

Disclosure: this change was prepared with an AI coding assistant (Claude) working for the account owner.

When embedding_types is set, Cohere on Bedrock returns the embeddings keyed by type
(a dict) instead of a list of vectors, and parse_text_embedding_response rejected it
with ServiceInvalidResponseError. Return the float embedding when present, otherwise
the first type in the response.
Copilot AI balanced review requested due to automatic review settings October 29, 2026 12:37
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 29, 2026

Copilot AI left a comment

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.

🟡 Changes recommended

Typed embedding responses can accept malformed vectors or select inconsistent encodings across a batch, potentially causing dimension errors.

2 open issues
What changed in this PR

This PR extends the Python Bedrock Cohere embedding parser to handle responses keyed by embedding type, addressing failures when embedding_types is requested.

Changes:

  • Accept typed responses, preferring float and otherwise selecting the first returned type.
  • Preserve the existing plain-list response path.
  • Add tests for typed embeddings, request forwarding, and empty or malformed typed responses.
File Description
python/​tests/​unit/​connectors/​ai/​bedrock/​services/​test_bedrock_text_embedding_generation.py Adds successful and invalid typed-response test cases.
python/​semantic_kernel/​connectors/​ai/​bedrock/​services/​model_provider/​bedrock_cohere.py Adds extraction of embeddings from type-keyed responses.

Review effort: Balanced

if isinstance(embeddings, dict):
# With `embedding_types` set, the embeddings are keyed by type ({"float": [[...]], "int8": [[...]]}).
# Return the float embedding when it was requested, otherwise the first type in the response.
embeddings = embeddings.get("float") or next(iter(embeddings.values()), None)
if isinstance(embeddings, dict):
# With `embedding_types` set, the embeddings are keyed by type ({"float": [[...]], "int8": [[...]]}).
# Return the float embedding when it was requested, otherwise the first type in the response.
embeddings = embeddings.get("float") or next(iter(embeddings.values()), None)

This branch was successfully deployed

1 active deployment
github-app-auth — 7d513966 Deployed Oct 29, 2026 by pcbeingused333 via add_label #29484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants