Repository navigation
fix(sagemaker): send native Cohere embed payload to Cohere SageMaker endpoints #28613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mateo-berri
merged 5 commits into
litellm_internal_staging
from
litellm_fix_sagemaker_cohere_embedding
May 22, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
444e8d3
fix(sagemaker): use Cohere embed payload for Marketplace endpoints
milan-berri 5f35e18
refactor(sagemaker): simplify cohere detection and align with file co…
milan-berri 43c663c
fix(sagemaker): restore provider-supported embedding params after map
milan-berri dec0138
fix(embeddings): only restore non-OpenAI supported params after map
milan-berri da5aab7
fix(sagemaker): avoid double post_call on Cohere embedding response
milan-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
litellm/llms/sagemaker/embedding/cohere_transformation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| """ | ||
| Translate from OpenAI's `/v1/embeddings` to Sagemaker's `/invoke` | ||
|
|
||
| In the native Cohere embed format for self-hosted Cohere endpoints | ||
| (AWS Marketplace / JumpStart). Cohere containers expect | ||
| `{"texts": [...], "input_type": "..."}` and reject the HuggingFace TGI shape | ||
| `{"inputs": [...]}` with `422 EmbedReqV2.inputs is of type string but should | ||
| be of type Object`. | ||
|
|
||
| Reference: https://docs.cohere.com/v2/reference/embed | ||
| """ | ||
|
|
||
| from typing import TYPE_CHECKING, Any, List, Optional, Union, cast | ||
|
|
||
| if TYPE_CHECKING: | ||
| from litellm.types.llms.openai import AllEmbeddingInputValues | ||
|
|
||
| from httpx._models import Headers, Response | ||
|
|
||
| import litellm | ||
| from litellm.llms.base_llm.chat.transformation import BaseLLMException | ||
| from litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfig | ||
| from litellm.llms.bedrock.embed.cohere_transformation import ( | ||
| BedrockCohereEmbeddingConfig, | ||
| ) | ||
| from litellm.llms.cohere.embed.v1_transformation import CohereEmbeddingConfig | ||
| from litellm.types.utils import EmbeddingResponse | ||
|
|
||
| from ..common_utils import SagemakerError | ||
|
|
||
|
|
||
| class SagemakerCohereEmbeddingConfig(BaseEmbeddingConfig): | ||
| """ | ||
| SageMaker invoke payload for self-hosted Cohere embed models. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
| def get_supported_openai_params(self, model: str) -> List[str]: | ||
| return ["encoding_format", "dimensions", "input_type"] | ||
|
|
||
| def map_openai_params( | ||
| self, | ||
| non_default_params: dict, | ||
| optional_params: dict, | ||
| model: str, | ||
| drop_params: bool, | ||
| ) -> dict: | ||
| optional_params = BedrockCohereEmbeddingConfig().map_openai_params( | ||
| non_default_params=non_default_params, | ||
| optional_params=optional_params, | ||
| ) | ||
| if "input_type" in non_default_params: | ||
| optional_params["input_type"] = non_default_params["input_type"] | ||
| return optional_params | ||
|
|
||
| def get_error_class( | ||
| self, error_message: str, status_code: int, headers: Union[dict, Headers] | ||
| ) -> BaseLLMException: | ||
| return SagemakerError( | ||
| message=error_message, status_code=status_code, headers=headers | ||
| ) | ||
|
|
||
| def transform_embedding_request( | ||
| self, | ||
| model: str, | ||
| input: "AllEmbeddingInputValues", | ||
| optional_params: dict, | ||
| headers: dict, | ||
| ) -> dict: | ||
| """ | ||
| Transform embedding request for Cohere models on SageMaker | ||
| """ | ||
| if isinstance(input, str): | ||
| input_list: List[str] = [input] | ||
| elif isinstance(input, list): | ||
| if input and (isinstance(input[0], list) or isinstance(input[0], int)): | ||
| raise ValueError("Input must be a list of strings") | ||
| input_list = cast(List[str], input) | ||
| else: | ||
| input_list = [str(input)] | ||
|
|
||
| return dict( | ||
| BedrockCohereEmbeddingConfig()._transform_request( | ||
| model=model, | ||
| input=input_list, | ||
| inference_params=optional_params, | ||
| ) | ||
| ) | ||
|
|
||
| def transform_embedding_response( | ||
| self, | ||
| model: str, | ||
| raw_response: Response, | ||
| model_response: "EmbeddingResponse", | ||
| logging_obj: Any, | ||
| api_key: Optional[str] = None, | ||
| request_data: dict = {}, | ||
| optional_params: dict = {}, | ||
| litellm_params: dict = {}, | ||
| ) -> "EmbeddingResponse": | ||
| """ | ||
| Transform embedding response for Cohere models on SageMaker. | ||
|
|
||
| Uses `CohereEmbeddingConfig._populate_embedding_response` (not | ||
| `_transform_response`) so we do not log `post_call` a second time | ||
| — the SageMaker embedding handler already logs `post_call` before | ||
| invoking this transform. | ||
| """ | ||
| input_value = ( | ||
| logging_obj.model_call_details.get("input") | ||
| or request_data.get("texts") | ||
| or request_data.get("images") | ||
| or [] | ||
| ) | ||
| if isinstance(input_value, str): | ||
| input_value = [input_value] | ||
|
|
||
| return CohereEmbeddingConfig()._populate_embedding_response( | ||
| response_json=raw_response.json(), | ||
| model_response=model_response, | ||
| model=model, | ||
| encoding=litellm.encoding, | ||
| input=input_value, | ||
| ) | ||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: dict, | ||
| model: str, | ||
| messages: List[Any], | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| api_key: Optional[str] = None, | ||
| api_base: Optional[str] = None, | ||
| ) -> dict: | ||
| """ | ||
| Validate environment for SageMaker Cohere embeddings | ||
| """ | ||
| return {"Content-Type": "application/json"} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.