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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(embedding): default OpenAI-path encoding_format to float
Sameer Kankute8473b70dd8a9
diff --git a/litellm/main.py b/litellm/main.pyindex 0079bd750cf..8cff8c0b9a1 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -4920,11 +4920,12 @@ def embedding( # noqa: PLR0915if headers is not None and headers != {}:optional_params["extra_headers"] = headers- if encoding_format is not None:- optional_params["encoding_format"] = encoding_format- else:- # Omiting causes openai sdk to add default value of "float"- optional_params["encoding_format"] = None+ optional_params["encoding_format"] = (+ encoding_format+ or optional_params.get("encoding_format")+ or get_secret_str("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT")+ or "float"+ )api_version = Nonediff --git a/tests/test_litellm/test_openai_embedding_encoding_format_default.py b/tests/test_litellm/test_openai_embedding_encoding_format_default.pynew file mode 100644index 00000000000..dfeb9975642--- /dev/null+++ b/tests/test_litellm/test_openai_embedding_encoding_format_default.py@@ -0,0 +1,50 @@+from unittest.mock import MagicMock, patch++import pytest++from litellm import embedding+++ "set_env, env_value, expected",+ [+ (False, None, "float"),+ (True, "base64", "base64"),+ ],+)+def test_openai_embedding_encoding_format_default(+ monkeypatch, set_env, env_value, expected+):+ monkeypatch.delenv("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT", raising=False)+ if set_env:+ monkeypatch.setenv("LITELLM_DEFAULT_EMBEDDING_ENCODING_FORMAT", env_value)++ 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",+ )++ call_kwargs = (+ mock_client_instance.embeddings.with_raw_response.create.call_args[1]+ )+ assert call_kwargs["encoding_format"] == expected