From 6bd2143a3ddd77c73fa09406ca3200eeef5afcd1 Mon Sep 17 00:00:00 2001 From: Darien Kindlund Date: Fri, 27 Feb 2026 22:26:33 -0500 Subject: [PATCH] fix: guard against str response from Azure before calling model_dump() (#21634) The OpenAI SDK raw_response.parse() can return a plain str instead of a Pydantic model when Azure returns a non-JSON content type (e.g., HTML error page, proxy error). Calling .model_dump() on the str then raises AttributeError. Adds isinstance(response, str) checks before all 4 model_dump() call sites in the Azure chat completion and embedding paths. Co-authored-by: Claude Opus 4.6 (1M context) --- litellm/llms/azure/azure.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/litellm/llms/azure/azure.py b/litellm/llms/azure/azure.py index 44ee51d14a..51b98c4af5 100644 --- a/litellm/llms/azure/azure.py +++ b/litellm/llms/azure/azure.py @@ -343,6 +343,11 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM): headers, response = self.make_sync_azure_openai_chat_completion_request( azure_client=azure_client, data=data, timeout=timeout ) + if isinstance(response, str): + raise AzureOpenAIError( + status_code=500, + message=f"Unexpected string response from Azure: {response[:500]}", + ) stringified_response = response.model_dump() ## LOGGING logging_obj.post_call( @@ -432,6 +437,11 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM): ) logging_obj.model_call_details["response_headers"] = headers + if isinstance(response, str): + raise AzureOpenAIError( + status_code=500, + message=f"Unexpected string response from Azure: {response[:500]}", + ) stringified_response = response.model_dump() logging_obj.post_call( input=data["messages"], @@ -690,7 +700,11 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM): status_code=raw_response.status_code or 500, message=f"Failed to parse raw Azure embedding response: {str(json_error)}" ) from json_error - + if isinstance(response, str): + raise AzureOpenAIError( + status_code=raw_response.status_code or 500, + message=f"Unexpected string response from Azure: {response[:500]}", + ) stringified_response = response.model_dump() ## LOGGING @@ -792,6 +806,11 @@ class AzureChatCompletion(BaseAzureLLM, BaseLLM): raw_response = azure_client.embeddings.with_raw_response.create(**data, timeout=timeout) # type: ignore headers = dict(raw_response.headers) response = raw_response.parse() + if isinstance(response, str): + raise AzureOpenAIError( + status_code=raw_response.status_code or 500, + message=f"Unexpected string response from Azure: {response[:500]}", + ) ## LOGGING logging_obj.post_call( input=input,