fix(openai,azure): construct sanitized payloads immutably without in-place mutation

This commit is contained in:
VimalN2005 2026-09-19 11:43:48 +05:30
parent 6dc54fe95e
commit ce1d0512bb
2 changed files with 30 additions and 16 deletions

View file

@ -1254,10 +1254,14 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM):
model_response._hidden_params["model"] = litellm_params.get("base_model", None)
# Azure image generation API doesn't support extra_body parameter
extra_body: Final = optional_params.pop("extra_body", {})
flattened_params: Final = {**optional_params, **extra_body}
flattened_params.pop("extra_headers", None)
flattened_params.pop("headers", None)
extra_body: Final = (
optional_params.get("extra_body") if isinstance(optional_params.get("extra_body"), dict) else {}
)
flattened_params: Final = {
k: v
for k, v in {**optional_params, **extra_body}.items()
if k not in ("extra_body", "extra_headers", "headers")
}
base_model: Final = litellm_params.get("base_model", None) if litellm_params else None
data: Final = {"model": base_model or model, "prompt": prompt, **flattened_params}

View file

@ -1426,14 +1426,19 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
},
)
if "extra_body" in data and isinstance(data["extra_body"], dict):
data["extra_body"].pop("extra_headers", None)
data["extra_body"].pop("headers", None)
if not data["extra_body"]:
data.pop("extra_body", None)
sanitized_extra_body: Final = (
{k: v for k, v in data["extra_body"].items() if k not in ("extra_headers", "headers")}
if isinstance(data.get("extra_body"), dict)
else None
)
sanitized_data: Final = {
k: (sanitized_extra_body if k == "extra_body" else v)
for k, v in data.items()
if k != "extra_body" or sanitized_extra_body
}
request_data: Final = ( # mutable-ok: the OpenAI SDK takes the request body as a dict
{**data, "extra_headers": headers} if headers else data
{**sanitized_data, "extra_headers": headers} if headers else sanitized_data
)
response = await openai_aclient.images.generate(**request_data, timeout=timeout)
stringified_response: Final = response.model_dump()
@ -1517,15 +1522,20 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
},
)
if "extra_body" in data and isinstance(data["extra_body"], dict):
data["extra_body"].pop("extra_headers", None)
data["extra_body"].pop("headers", None)
if not data["extra_body"]:
data.pop("extra_body", None)
sanitized_extra_body: Final = (
{k: v for k, v in data["extra_body"].items() if k not in ("extra_headers", "headers")}
if isinstance(data.get("extra_body"), dict)
else None
)
sanitized_data: Final = {
k: (sanitized_extra_body if k == "extra_body" else v)
for k, v in data.items()
if k != "extra_body" or sanitized_extra_body
}
## COMPLETION CALL
request_data: Final = ( # mutable-ok: the OpenAI SDK takes the request body as a dict
{**data, "extra_headers": headers} if headers else data
{**sanitized_data, "extra_headers": headers} if headers else sanitized_data
)
_response: Final = openai_client.images.generate(**request_data, timeout=timeout)