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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_fix_openai_embedding_encoding_format_omit

mateo-berri11e050223930
diff --cc litellm/llms/openai/openai.py
index 7b71e532b61,6e66c998acf..c8e616506c3
--- a/litellm/llms/openai/openai.py
+++ b/litellm/llms/openai/openai.py
@@@ -327,24 -322,6 +327,28 @@@ class OpenAIChatCompletionResponseItera
raise e
+_EXTRA_HEADERS_ADAPTER: Final = TypeAdapter(dict[str, str] | None)
+_EXTRA_QUERY_ADAPTER: Final = TypeAdapter(dict[str, object] | None)
++_NO_EXTRA_HEADERS: Final[Mapping[str, str]] = types.MappingProxyType({})
++_SDK_OPTION_KEYS: Final = frozenset(("extra_headers", "extra_query", "extra_body"))
+
+
+def _embedding_request_without_sdk_defaults(
+ data: Mapping[str, object], timeout: float | httpx.Timeout
- ) -> tuple[dict[str, object], RequestOptions]:
- body: Final = {k: v for k, v in data.items() if k not in ("extra_headers", "extra_query", "extra_body")}
- extra_headers: Final = _EXTRA_HEADERS_ADAPTER.validate_python(data.get("extra_headers"))
++) -> tuple[Mapping[str, object], RequestOptions]:
++ body: Final = { # mutable-ok: the SDK json-encodes the body and needs a plain dict
++ k: v for k, v in data.items() if k not in _SDK_OPTION_KEYS
++ }
++ extra_headers: Final = _EXTRA_HEADERS_ADAPTER.validate_python(data.get("extra_headers")) or _NO_EXTRA_HEADERS
+ options: Final = make_request_options(
- extra_headers={**(extra_headers or {}), RAW_RESPONSE_HEADER: "true"},
++ extra_headers=types.MappingProxyType({**extra_headers, RAW_RESPONSE_HEADER: "true"}),
+ extra_query=_EXTRA_QUERY_ADAPTER.validate_python(data.get("extra_query")),
+ extra_body=data.get("extra_body"),
+ timeout=timeout,
+ )
+ return body, options
+
+
class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
def __init__(self) -> None:
super().__init__()
@@@ -1171,16 -1148,19 +1175,15 @@@
data: dict,
timeout: float | httpx.Timeout,
logging_obj: LiteLLMLoggingObj,
- ) -> tuple[dict[str, str], CreateEmbeddingResponse]:
- ):
- """
- Helper to:
- - call embeddings.create.with_raw_response when litellm.return_response_headers is True
- - call embeddings.create by default
- """
- try:
- raw_response = await openai_aclient.embeddings.with_raw_response.create(**data, timeout=timeout)
- headers: Final = dict(raw_response.headers)
- response: Final = raw_response.parse()
- return headers, response
- except Exception as e:
- raise e
++ ) -> LegacyAPIResponse[CreateEmbeddingResponse]:
+ if "encoding_format" not in data:
+ body, options = _embedding_request_without_sdk_defaults(data, timeout)
+ bypass_response: Final = await openai_aclient.post(
+ "/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse
+ )
+ assert isinstance(bypass_response, LegacyAPIResponse)
- return dict(bypass_response.headers), bypass_response.parse(to=CreateEmbeddingResponse)
- raw_response: Final = await openai_aclient.embeddings.with_raw_response.create(**data, timeout=timeout)
- return dict(raw_response.headers), raw_response.parse()
++ return bypass_response
++ return await openai_aclient.embeddings.with_raw_response.create(**data, timeout=timeout)
@track_llm_api_timing()
def make_sync_openai_embedding_request(
@@@ -1189,16 -1169,20 +1192,15 @@@
data: dict,
timeout: float | httpx.Timeout,
logging_obj: LiteLLMLoggingObj,
- ) -> tuple[dict[str, str], CreateEmbeddingResponse]:
- ):
- """
- Helper to:
- - call embeddings.create.with_raw_response when litellm.return_response_headers is True
- - call embeddings.create by default
- """
- try:
- raw_response = openai_client.embeddings.with_raw_response.create(**data, timeout=timeout)
-
- headers: Final = dict(raw_response.headers)
- response: Final = raw_response.parse()
- return headers, response
- except Exception as e:
- raise e
++ ) -> LegacyAPIResponse[CreateEmbeddingResponse]:
+ if "encoding_format" not in data:
+ body, options = _embedding_request_without_sdk_defaults(data, timeout)
+ bypass_response: Final = openai_client.post(
+ "/embeddings", body=body, options=options, cast_to=CreateEmbeddingResponse
+ )
+ assert isinstance(bypass_response, LegacyAPIResponse)
- return dict(bypass_response.headers), bypass_response.parse(to=CreateEmbeddingResponse)
- raw_response: Final = openai_client.embeddings.with_raw_response.create(**data, timeout=timeout)
- return dict(raw_response.headers), raw_response.parse()
++ return bypass_response
++ return openai_client.embeddings.with_raw_response.create(**data, timeout=timeout)
async def aembedding(
self,
@@@ -1223,14 -1207,14 +1225,15 @@@
client=client,
shared_session=shared_session,
)
-- headers, response = await self.make_openai_embedding_request(
++ raw_response: Final = await self.make_openai_embedding_request(
openai_aclient=openai_aclient,
data=data,
timeout=timeout,
logging_obj=logging_obj,
)
++ headers: Final = dict(raw_response.headers)
logging_obj.model_call_details["response_headers"] = headers
-- stringified_response: Final = response.model_dump()
++ stringified_response: Final = raw_response.parse().model_dump()
## LOGGING
logging_obj.post_call(
input=input,
@@@ -1322,13 -1306,13 +1325,14 @@@
)
## embedding CALL
-- headers: dict | None = None
-- headers, sync_embedding_response = self.make_sync_openai_embedding_request(
++ raw_response: Final = self.make_sync_openai_embedding_request(
openai_client=openai_client,
data=data,
timeout=timeout,
logging_obj=logging_obj,
)
++ headers: Final = dict(raw_response.headers)
++ sync_embedding_response: Final = raw_response.parse()
## LOGGING
logging_obj.model_call_details["response_headers"] = headers
diff --cc ruff-strict-budget.json
index 73c69a732fe,fce91d57d88..534b1524581
--- a/ruff-strict-budget.json
+++ b/ruff-strict-budget.json
@@@ -6,10 -6,10 +6,10 @@@
"limit": 71
},
"ANN003": {
- "limit": 827
+ "limit": 809
},
"ANN201": {
- "limit": 2003
+ "limit": 2001
},
"ANN202": {
"limit": 845