From 206b6bad4198f1420726aa0313bcc21d4e4495e6 Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Thu, 12 Feb 2026 11:33:45 -0800 Subject: [PATCH] fixing azure bad request error --- .../llms/azure/responses/transformation.py | 10 +++++ .../response/test_azure_transformation.py | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/litellm/llms/azure/responses/transformation.py b/litellm/llms/azure/responses/transformation.py index 44ce368fd49..23f69a71990 100644 --- a/litellm/llms/azure/responses/transformation.py +++ b/litellm/llms/azure/responses/transformation.py @@ -132,6 +132,16 @@ class AzureOpenAIResponsesAPIConfig(OpenAIResponsesAPIConfig): new_tools.append(tool) response_api_optional_request_params["tools"] = new_tools + # Azure Responses API expects context_management as an object with "edits" key, + # but OpenAI/litellm uses array format [{"type": "compaction", "compact_threshold": N}]. + # See: "Invalid type for 'context_management': expected an object, but got an array instead." + if "context_management" in response_api_optional_request_params: + cm = response_api_optional_request_params["context_management"] + if isinstance(cm, list): + response_api_optional_request_params["context_management"] = { + "edits": cm + } + return super().transform_responses_api_request( model=stripped_model_name, input=input, diff --git a/tests/test_litellm/llms/azure/response/test_azure_transformation.py b/tests/test_litellm/llms/azure/response/test_azure_transformation.py index f54724b859f..d38435ad18f 100644 --- a/tests/test_litellm/llms/azure/response/test_azure_transformation.py +++ b/tests/test_litellm/llms/azure/response/test_azure_transformation.py @@ -492,3 +492,48 @@ class TestAzureResponsesAPIConfig: ) assert "tools" not in response_api_params + + def test_azure_responses_api_context_management_array_to_object(self): + """Test that context_management array is converted to object format for Azure. + + Azure Responses API expects context_management as an object with 'edits' key, + but litellm/OpenAI uses array format [{"type": "compaction", "compact_threshold": N}]. + """ + from litellm.types.router import GenericLiteLLMParams + + context_management = [ + {"type": "compaction", "compact_threshold": 200000} + ] + response_api_params = {"context_management": context_management} + litellm_params = GenericLiteLLMParams() + + result = self.config.transform_responses_api_request( + model=self.model, + input="test input", + response_api_optional_request_params=response_api_params, + litellm_params=litellm_params, + headers={}, + ) + + assert "context_management" in result + assert result["context_management"] == { + "edits": [{"type": "compaction", "compact_threshold": 200000}] + } + + def test_azure_responses_api_context_management_already_object(self): + """Test that context_management already in object format is passed through.""" + from litellm.types.router import GenericLiteLLMParams + + context_management = {"edits": [{"type": "compaction", "compact_threshold": 200000}]} + response_api_params = {"context_management": context_management} + litellm_params = GenericLiteLLMParams() + + result = self.config.transform_responses_api_request( + model=self.model, + input="test input", + response_api_optional_request_params=response_api_params, + litellm_params=litellm_params, + headers={}, + ) + + assert result["context_management"] == context_management