{"record":{"id":"ef9f11d46da4f397","repo":"BerriAI/litellm","slug":"error-parsing-openrouter-response-e-ef9f11","errorCode":null,"errorMessage":"Error parsing OpenRouter response: {e}","messagePattern":"Error parsing OpenRouter response: (.+?)","errorType":"http","errorClass":"OpenRouterException","httpStatus":null,"severity":"error","filePath":"litellm/llms/openrouter/image_generation/transformation.py","lineNumber":347,"sourceCode":"        Args:\n            model: The model name\n            raw_response: Raw HTTP response from OpenRouter\n            model_response: ImageResponse object to populate\n            logging_obj: Logging object\n            request_data: Original request data\n            optional_params: Optional parameters\n            litellm_params: LiteLLM parameters\n            encoding: Encoding\n            api_key: API key\n            json_mode: JSON mode flag\n\n        Returns:\n            ImageResponse: Populated image response\n        \"\"\"\n        try:\n            response_json: Final = raw_response.json()\n        except Exception as e:\n            raise OpenRouterException(\n                message=f\"Error parsing OpenRouter response: {e}\",\n                status_code=raw_response.status_code,\n                headers=raw_response.headers,\n            )\n\n        if not model_response.data:\n            model_response.data = []\n\n        try:\n            choices: Final = response_json.get(\"choices\", [])\n\n            for choice in choices:\n                message = choice.get(\"message\", {})\n                images = message.get(\"images\", [])\n\n                for image_data in images:\n                    image_url_obj = image_data.get(\"image_url\", {})\n                    image_url = image_url_obj.get(\"url\")","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openrouter/image_generation/transformation.py#L329-L365","documentation":"LiteLLM's OpenRouter image-generation handler calls raw_response.json() before populating the ImageResponse. If the response body is not valid JSON, the parser's exception is wrapped in an OpenRouterException forwarding the real HTTP status code and headers, with the parse error embedded in the message.","triggerScenarios":"Calling litellm.image_generation() with an openrouter/* model when the endpoint returns a non-JSON body: gateway HTML pages, empty bodies from timed-out upstreams, plain-text rate-limit or auth errors, or a custom api_base that returns non-JSON.","commonSituations":"OpenRouter maintenance windows returning HTML; invalid API key producing a non-JSON auth error; routing through a corporate proxy that injects HTML block pages.","solutions":["Log the exception's status_code and headers, then replay the request with curl to inspect the raw body","Confirm OPENROUTER_API_KEY is valid and the model is available for image generation","Remove or fix any custom api_base override","Retry transient 5xx/non-JSON responses with backoff"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"Catch OpenRouterException around litellm.image_generation(); if the message starts with 'Error parsing', the body was not JSON - log status_code/headers, retry once with backoff for transient gateway errors, and fall back to another image provider if it persists.","preventionTips":["Validate the API key and model availability before entering image-generation flows","Do not point api_base at proxies that can return HTML error pages","Monitor for bursts of parse failures as an early signal of provider incidents"],"tags":["openrouter","image-generation","json-parsing","http-response","litellm"],"backgroundTag":"invalid-json-response","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}