fix(openai,azure): sanitize payload before logging and check exact call counts

This commit is contained in:
VimalN2005 2026-09-19 22:48:32 +05:30
parent ce1d0512bb
commit 5ee4b49d2f
3 changed files with 58 additions and 56 deletions

View file

@ -1254,12 +1254,12 @@ 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.get("extra_body") if isinstance(optional_params.get("extra_body"), dict) else {}
)
raw_extra_body = optional_params.get("extra_body")
extra_body: Final[dict] = raw_extra_body if isinstance(raw_extra_body, dict) else {}
flattened_params: Final = {
k: v
for k, v in {**optional_params, **extra_body}.items()
for d in (optional_params, extra_body)
for k, v in d.items()
if k not in ("extra_body", "extra_headers", "headers")
}

View file

@ -1415,6 +1415,18 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
client=client,
)
raw_extra_body: Final = data.get("extra_body")
sanitized_extra_body: Final = (
{k: v for k, v in raw_extra_body.items() if k not in ("extra_headers", "headers")} # mutable-ok: sanitized payload dictionary
if isinstance(raw_extra_body, dict)
else None
)
sanitized_data: Final = { # mutable-ok: sanitized payload dictionary
k: (sanitized_extra_body if k == "extra_body" else v)
for k, v in data.items()
if k != "extra_body" or sanitized_extra_body
}
logging_obj.pre_call(
input=prompt,
api_key=openai_aclient.api_key,
@ -1422,21 +1434,10 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
"headers": {"Authorization": f"Bearer {openai_aclient.api_key}"}, # mutable-ok: logged header map
"api_base": str(openai_aclient.base_url),
"acompletion": True,
"complete_input_dict": data,
"complete_input_dict": sanitized_data,
},
)
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
{**sanitized_data, "extra_headers": headers} if headers else sanitized_data
)
@ -1510,6 +1511,18 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
client=client,
)
raw_extra_body: Final = data.get("extra_body")
sanitized_extra_body: Final = (
{k: v for k, v in raw_extra_body.items() if k not in ("extra_headers", "headers")} # mutable-ok: sanitized payload dictionary
if isinstance(raw_extra_body, dict)
else None
)
sanitized_data: Final = { # mutable-ok: sanitized payload dictionary
k: (sanitized_extra_body if k == "extra_body" else v)
for k, v in data.items()
if k != "extra_body" or sanitized_extra_body
}
## LOGGING
logging_obj.pre_call(
input=prompt,
@ -1518,21 +1531,10 @@ class OpenAIChatCompletion(BaseLLM, BaseOpenAILLM):
"headers": {"Authorization": f"Bearer {openai_client.api_key}"},
"api_base": openai_client._base_url._uri_reference,
"acompletion": True,
"complete_input_dict": data,
"complete_input_dict": sanitized_data,
},
)
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
{**sanitized_data, "extra_headers": headers} if headers else sanitized_data

View file

@ -71,10 +71,10 @@ class TestImageGenerationExtraHeaders:
assert "extra_headers" not in optional_params
def test_openai_image_generation_excludes_extra_headers_from_body(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -87,8 +87,8 @@ class TestImageGenerationExtraHeaders:
extra_headers={"cf-aig-auth": "secret-123"},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
assert req.headers.get("cf-aig-auth") == "secret-123"
body: Final = json.loads(req.read())
assert "extra_headers" not in body
@ -96,10 +96,10 @@ class TestImageGenerationExtraHeaders:
@pytest.mark.asyncio
async def test_openai_aimage_generation_excludes_extra_headers_from_body(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -112,18 +112,18 @@ class TestImageGenerationExtraHeaders:
extra_headers={"cf-aig-auth": "async-secret-123"},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
assert req.headers.get("cf-aig-auth") == "async-secret-123"
body: Final = json.loads(req.read())
assert "extra_headers" not in body
assert body == {"prompt": "async test prompt", "model": "gpt-image-2"}
def test_openai_image_generation_with_headers_excludes_from_body(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -136,8 +136,8 @@ class TestImageGenerationExtraHeaders:
headers={"custom-header": "custom-val"},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
assert req.headers.get("custom-header") == "custom-val"
body: Final = json.loads(req.read())
assert "headers" not in body
@ -145,10 +145,10 @@ class TestImageGenerationExtraHeaders:
assert body == {"prompt": "headers test", "model": "gpt-image-2"}
def test_openai_image_generation_sanitizes_extra_body_and_preserves_siblings(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -161,8 +161,8 @@ class TestImageGenerationExtraHeaders:
extra_body={"extra_headers": {"cf-aig-auth": "secret-123"}, "custom_sibling": "sibling_val"},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
body: Final = json.loads(req.read())
assert "extra_headers" not in body
assert body.get("custom_sibling") == "sibling_val"
@ -170,10 +170,10 @@ class TestImageGenerationExtraHeaders:
@pytest.mark.asyncio
async def test_openai_aimage_generation_sanitizes_extra_body_and_preserves_siblings(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -186,18 +186,18 @@ class TestImageGenerationExtraHeaders:
extra_body={"extra_headers": {"cf-aig-auth": "secret-123"}, "custom_sibling": "async_sibling_val"},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
body: Final = json.loads(req.read())
assert "extra_headers" not in body
assert body.get("custom_sibling") == "async_sibling_val"
assert body == {"prompt": "async test prompt", "model": "gpt-image-2", "custom_sibling": "async_sibling_val"}
def test_openai_image_generation_extra_body_only_headers_pops_empty_extra_body(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -210,8 +210,8 @@ class TestImageGenerationExtraHeaders:
extra_body={"extra_headers": {"cf-aig-auth": "secret-123"}},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
body: Final = json.loads(req.read())
assert "extra_headers" not in body
assert "extra_body" not in body
@ -219,10 +219,10 @@ class TestImageGenerationExtraHeaders:
@pytest.mark.asyncio
async def test_openai_aimage_generation_extra_body_only_headers_pops_empty_extra_body(self):
captured: Final[dict[str, httpx.Request]] = {} # mutable-ok: test request capture map
captured: Final[list[httpx.Request]] = [] # mutable-ok: test request capture log
def handle_request(request: httpx.Request) -> httpx.Response:
captured["request"] = request
captured.append(request)
return httpx.Response(200, json={"data": [{"url": "https://example.com/image.png"}]})
transport: Final = httpx.MockTransport(handle_request)
@ -235,8 +235,8 @@ class TestImageGenerationExtraHeaders:
extra_body={"extra_headers": {"cf-aig-auth": "secret-123"}},
)
assert "request" in captured
req: Final = captured["request"]
assert len(captured) == 1
req: Final = captured[0]
body: Final = json.loads(req.read())
assert "extra_headers" not in body
assert "extra_body" not in body