BerriAI/litellm · error · OpenAIError
raw_response.text
Error message
raw_response.text
What it means
Raised by the Azure AI (MAI) image-generation handler when the upstream HTTP response body cannot be parsed as JSON. The raw response is attempted with raw_response.json(); on any parse failure LiteLLM wraps the body text and HTTP status in an OpenAIError so the caller sees what Azure actually returned instead of a bare JSONDecodeError. Typical causes are non-JSON error pages (HTML auth failures, gateway errors) or truncated bodies.
Source
Thrown at litellm/llms/azure_ai/image_generation/mai_transformation.py:209
)
def transform_image_generation_response(
self,
model: str,
raw_response: httpx.Response,
model_response: ImageResponse,
logging_obj: "LiteLLMLoggingObj",
request_data: dict,
optional_params: dict,
litellm_params: dict,
encoding: Any,
api_key: str | None = None,
json_mode: bool | None = None,
) -> ImageResponse:
try:
response: Final = raw_response.json()
except Exception:
raise OpenAIError(message=raw_response.text, status_code=raw_response.status_code)
if "usage" in response:
response["usage"] = self.normalize_mai_image_usage(response.get("usage"))
logging_obj.post_call(
input=request_data.get("prompt", ""),
api_key=api_key,
additional_args={"complete_input_dict": request_data},
original_response=response,
)
image_response: Final[ImageResponse] = convert_to_model_response_object(
response_object=response,
model_response_object=model_response,
response_type="image_generation",
)
width: Final = optional_params.get("width", self.DEFAULT_WIDTH)View on GitHub (pinned to 6c2dcb801b)
Solutions
- Inspect the OpenAIError message: it contains raw_response.text and status_code from Azure — that body is the real upstream error, fix what it reports (auth, endpoint, model).
- Verify AZURE_AI_API_KEY / api_key and AZURE_AI_API_BASE point to a valid Azure MAI endpoint with curl -i to confirm it returns JSON.
- If a transient 5xx, retry the call or check Azure status; ensure no corporate proxy is injecting HTML error pages.
- Confirm the model string is correct for the MAI image-generation route so you don't land on an error page.
Example fix
# before
response = litellm.image_generation(model="azure_ai/mai/foo", prompt="a cat")
# after
import litellm
from litellm.exceptions import OpenAIError
try:
response = litellm.image_generation(model="azure_ai/mai/foo", prompt="a cat")
except OpenAIError as e:
# e.message carries Azure's raw body + status; surface it for diagnosis
logger.error("Azure MAI image call failed: %s", e) Defensive patterns
Strategy: try-catch
Try / catch
from litellm.exceptions import OpenAIError
try:
resp = litellm.image_generation(model="azure_ai/mai/<model>", prompt=p)
except OpenAIError as e:
# e.message contains Azure's raw body + status code — log and surface it
log.error("azure mai image failed: %s", e)
raise Prevention
- Verify AZURE_AI_API_KEY and api_base with a direct curl call that returns JSON before wiring into LiteLLM.
- Log the wrapped raw_response.text — it names the real upstream problem.
- Keep model strings aligned with the MAI image-generation route.
When it happens
Trigger: Calling image generation via the azure_ai/mai provider (e.g. model 'azure_ai/mai/...') and the endpoint returns a non-JSON body: expired/invalid API key returning an HTML or plain-text 401/403, a 5xx from a proxy/gateway, or a malformed gateway timeout page. Any Exception from raw_response.json() (JSONDecodeError, UnicodeDecodeError) hits this branch.
Common situations: Misconfigured AZURE_AI_API_KEY or wrong api_base pointing at a gateway that returns HTML error pages; Azure outages returning plain-text 503s; regional endpoint mismatches; using a model name the endpoint doesn't host.
Related errors
- {str(e)}
- Azure Document Intelligence analysis failed: {error_msg}
- Failed to parse Azure DI operation response: {e}
- Azure Document Intelligence analysis failed with status: {op
- Unknown hook: {hook_name}. Available hooks: {list(ENTERPRISE
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/ae54ccd0e7090fb0.
Report an issue: GitHub.