Repository navigation
feat(dashscope): add embeddings and reranks(qwen3-rerank) support via OpenAI-compatible endpoint #27508
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
10 commits merged into
BerriAI:shin_agent_oss_staging_05_13_2026
from
yimao:litellm_internal_staging
May 13, 2026
Merged
feat(dashscope): add embeddings and reranks(qwen3-rerank) support via OpenAI-compatible endpoint #27508
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2a0563a
feat(dashscope): add embeddings support via OpenAI-compatible endpoint
yimao aa5969c
Update litellm/llms/dashscope/embed/transformation.py
yimao 87d1208
Update litellm/llms/dashscope/embed/transformation.py
yimao ec05003
refactor(dashscope): consolidate api_key resolution to main.py
yimao e6e7cb7
Update Dashscope support in README
yimao bafed8b
feat(dashscope): add qwen3-rerank support via OpenAI-compatible /v1/r…
yimao 9f7763e
Update Dashscope support status in README
yimao 5495d70
fix(dashscope): forward user param in embed, honour api_base in reran…
yimao 98eafd3
test(dashscope): add coverage for user param forwarding and drop_para…
yimao 69807c9
Update litellm/llms/dashscope/rerank/transformation.py
yimao 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| Common utilities for the DashScope LLM provider. | ||
| """ | ||
|
|
||
| from typing import Optional | ||
|
|
||
| import httpx | ||
|
|
||
| from litellm.llms.base_llm.chat.transformation import BaseLLMException | ||
|
|
||
|
|
||
| class DashScopeError(BaseLLMException): | ||
| """Exception class for DashScope provider errors.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| status_code: int, | ||
| message: str, | ||
| headers: Optional[httpx.Headers] = None, | ||
| ): | ||
| self.status_code = status_code | ||
| self.message = message | ||
| self.headers = headers or httpx.Headers() | ||
| super().__init__( | ||
| status_code=status_code, | ||
| message=message, | ||
| headers=dict(self.headers), | ||
| ) |
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,7 @@ | ||
| """ | ||
| DashScope Embedding Module | ||
| """ | ||
|
|
||
| from .transformation import DashScopeEmbeddingConfig | ||
|
|
||
| __all__ = ["DashScopeEmbeddingConfig"] |
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,190 @@ | ||
| """ | ||
| Transformation logic from OpenAI /v1/embeddings format to DashScope's /v1/embeddings format. | ||
|
|
||
| Supports | ||
| - text-embedding-v4 | ||
| - text-embedding-v3 | ||
|
|
||
| Endpoint | ||
| - https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings | ||
|
|
||
| Docs - https://help.aliyun.com/zh/model-studio/text-embedding-synchronous-api | ||
| """ | ||
|
|
||
| from typing import List, Optional, Union | ||
|
|
||
| import httpx | ||
|
|
||
| from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj | ||
| from litellm.llms.base_llm.chat.transformation import BaseLLMException | ||
| from litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfig | ||
| from litellm.secret_managers.main import get_secret_str | ||
| from litellm.types.llms.openai import AllEmbeddingInputValues, AllMessageValues | ||
| from litellm.types.utils import EmbeddingResponse, Usage | ||
|
|
||
| from ..common_utils import DashScopeError | ||
|
|
||
| DEFAULT_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1" | ||
|
|
||
|
|
||
| class DashScopeEmbeddingConfig(BaseEmbeddingConfig): | ||
| """ | ||
| Reference: https://help.aliyun.com/zh/model-studio/text-embedding-synchronous-api | ||
|
|
||
| DashScope exposes an OpenAI-compatible /v1/embeddings endpoint, so the | ||
| request and response shapes are nearly identical to OpenAI's. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
| def get_supported_openai_params(self, model: str) -> List[str]: | ||
| # DashScope's compatible-mode embeddings API accepts the same params as OpenAI. | ||
| # `dimensions` / `encoding_format` are only honored by text-embedding-v3 / v4; | ||
| # earlier versions silently ignore them server-side. | ||
| return ["dimensions", "encoding_format"] | ||
|
|
||
| def map_openai_params( | ||
| self, | ||
| non_default_params: dict, | ||
| optional_params: dict, | ||
| model: str, | ||
| drop_params: bool = False, | ||
| ) -> dict: | ||
| supported = self.get_supported_openai_params(model) | ||
| for k, v in non_default_params.items(): | ||
| if v is None: | ||
| continue | ||
| if k in supported: | ||
| optional_params[k] = v | ||
| elif not drop_params: | ||
| raise ValueError(f"Unsupported parameter for DashScope embeddings: {k}") | ||
| return optional_params | ||
|
yimao marked this conversation as resolved.
Resolved
|
||
|
|
||
| def validate_environment( | ||
| self, | ||
| headers: dict, | ||
| model: str, | ||
| messages: List[AllMessageValues], | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| api_key: Optional[str] = None, | ||
| api_base: Optional[str] = None, | ||
| ) -> dict: | ||
| resolved_key = api_key or get_secret_str("DASHSCOPE_API_KEY") | ||
| if not resolved_key: | ||
| raise ValueError( | ||
| "Missing API key for DashScope. Set DASHSCOPE_API_KEY environment " | ||
| "variable or pass api_key parameter." | ||
| ) | ||
| default_headers = { | ||
| "Content-Type": "application/json", | ||
| "Authorization": f"Bearer {resolved_key}", | ||
| } | ||
| return {**default_headers, **headers} | ||
|
|
||
| def get_complete_url( | ||
| self, | ||
| api_base: Optional[str], | ||
| api_key: Optional[str], | ||
| model: str, | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| stream: Optional[bool] = None, | ||
| ) -> str: | ||
| base = api_base or get_secret_str("DASHSCOPE_API_BASE") or DEFAULT_API_BASE | ||
|
yimao marked this conversation as resolved.
Resolved
|
||
| base = base.rstrip("/") | ||
| if base.endswith("/embeddings"): | ||
| return base | ||
| return f"{base}/embeddings" | ||
|
|
||
| def transform_embedding_request( | ||
| self, | ||
| model: str, | ||
| input: AllEmbeddingInputValues, | ||
| optional_params: dict, | ||
| headers: dict, | ||
| ) -> dict: | ||
| data: dict = { | ||
| "model": model, | ||
| "input": input, | ||
| } | ||
| for key in ("dimensions", "encoding_format"): | ||
| value = optional_params.get(key) | ||
| if value is not None: | ||
| data[key] = value | ||
| return data | ||
|
|
||
| def transform_embedding_response( | ||
| self, | ||
| model: str, | ||
| raw_response: httpx.Response, | ||
| model_response: EmbeddingResponse, | ||
| logging_obj: LiteLLMLoggingObj, | ||
| api_key: Optional[str], | ||
| request_data: dict, | ||
| optional_params: dict, | ||
| litellm_params: dict, | ||
| ) -> EmbeddingResponse: | ||
| try: | ||
| response_json = raw_response.json() | ||
| except Exception as e: | ||
| raise DashScopeError( | ||
| status_code=raw_response.status_code, | ||
| message=f"Failed to parse DashScope response as JSON: {str(e)}", | ||
| ) | ||
|
|
||
| logging_obj.post_call( | ||
| input=request_data.get("input"), | ||
| api_key=api_key, | ||
| additional_args={"complete_input_dict": request_data}, | ||
| original_response=response_json, | ||
| ) | ||
|
|
||
| if "error" in response_json: | ||
| error = response_json["error"] | ||
| message = ( | ||
| error.get("message", str(error)) | ||
| if isinstance(error, dict) | ||
| else str(error) | ||
| ) | ||
| raise DashScopeError( | ||
| status_code=raw_response.status_code, | ||
| message=message, | ||
| ) | ||
|
|
||
| model_response.object = "list" | ||
| model_response.data = response_json.get("data", []) | ||
| model_response.model = response_json.get("model", model) | ||
|
|
||
| usage = response_json.get("usage") or {} | ||
| prompt_tokens = usage.get("prompt_tokens", 0) | ||
| total_tokens = usage.get("total_tokens", prompt_tokens) | ||
| setattr( | ||
| model_response, | ||
| "usage", | ||
| Usage( | ||
| prompt_tokens=prompt_tokens, | ||
| completion_tokens=0, | ||
| total_tokens=total_tokens, | ||
| ), | ||
| ) | ||
|
|
||
| if "id" in response_json: | ||
| setattr(model_response, "id", response_json["id"]) | ||
|
|
||
| return model_response | ||
|
|
||
| def get_error_class( | ||
| self, | ||
| error_message: str, | ||
| status_code: int, | ||
| headers: Union[dict, httpx.Headers], | ||
| ) -> BaseLLMException: | ||
| if isinstance(headers, dict): | ||
| headers = httpx.Headers(headers) | ||
| return DashScopeError( | ||
| status_code=status_code, | ||
| message=error_message, | ||
| headers=headers, | ||
| ) | ||
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.