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 code

Sameer Kankute5c72a95289a0
diff --git a/litellm/main.py b/litellm/main.py
index 9d385754741..6d5f9f9b05c 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -4700,10 +4700,7 @@ def embedding( # noqa: PLR0915
Parameters:
- model: The embedding model to use.
- input: The input for which embeddings are to be generated.
- - encoding_format: Optional[str] The format to return the embeddings in. Can be either `float` or `base64`.
- For OpenAI-compatible embedding routing (`openai`, custom base URL, etc.), when omitted LiteLLM resolves:
- explicit argument → mapped optional params (e.g. proxy `litellm_params`) → environment variable
- `LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT` → default `"float"`.
+ - encoding_format: Optional[str] The format to return the embeddings in. Can be either `float` or `base64`
- dimensions: The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
- timeout: The timeout value for the API call, default 10 mins
- litellm_call_id: The call ID for litellm logging.
@@ -4923,12 +4920,14 @@ def embedding( # noqa: PLR0915
if headers is not None and headers != {}:
optional_params["extra_headers"] = headers
- optional_params["encoding_format"] = (
- encoding_format
- or optional_params.get("encoding_format")
- or get_secret_str("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT")
- or "float"
- )
+ if encoding_format is not None:
+ optional_params["encoding_format"] = encoding_format
+ else:
+ optional_params["encoding_format"] = (
+ optional_params.get("encoding_format")
+ or get_secret_str("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT")
+ or "float"
+ )
api_version = None
diff --git a/tests/test_litellm/test_openai_embedding_encoding_format_default.py b/tests/test_litellm/test_openai_embedding_encoding_format_default.py
index dfeb9975642..6f34963f615 100644
--- a/tests/test_litellm/test_openai_embedding_encoding_format_default.py
+++ b/tests/test_litellm/test_openai_embedding_encoding_format_default.py
@@ -48,3 +48,39 @@ def test_openai_embedding_encoding_format_default(
mock_client_instance.embeddings.with_raw_response.create.call_args[1]
)
assert call_kwargs["encoding_format"] == expected
+
+
+def test_openai_embedding_encoding_format_explicit_overrides_env(monkeypatch):
+ """Request `encoding_format` wins over LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT."""
+ monkeypatch.setenv("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT", "float")
+
+ mock_response = MagicMock()
+ mock_response.parse.return_value = MagicMock(
+ model_dump=lambda: {
+ "data": [{"embedding": [0.1, 0.2, 0.3], "index": 0}],
+ "model": "text-embedding-ada-002",
+ "object": "list",
+ "usage": {"prompt_tokens": 1, "total_tokens": 1},
+ }
+ )
+ mock_response.headers = {}
+
+ with patch(
+ "litellm.llms.openai.openai.OpenAIChatCompletion._get_openai_client"
+ ) as mock_get_client:
+ mock_client_instance = MagicMock()
+ mock_get_client.return_value = mock_client_instance
+ mock_client_instance.embeddings.with_raw_response.create.return_value = (
+ mock_response
+ )
+
+ embedding(
+ model="text-embedding-ada-002",
+ input="Hello world",
+ encoding_format="base64",
+ )
+
+ call_kwargs = (
+ mock_client_instance.embeddings.with_raw_response.create.call_args[1]
+ )
+ assert call_kwargs["encoding_format"] == "base64"