{"record":{"id":"226a8f5851a83dbf","repo":"BerriAI/litellm","slug":"unexpected-error-format-in-mid-stream-chunk-erro","errorCode":null,"errorMessage":"Unexpected error format in mid-stream chunk: {error_data}","messagePattern":"Unexpected error format in mid-stream chunk: (.+?)","errorType":"http","errorClass":"VertexAIError","httpStatus":500,"severity":"error","filePath":"litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py","lineNumber":3105,"sourceCode":"        self.streaming_response = streaming_response\n        self.response = response\n        self.chunk_type: Literal[\"valid_json\", \"accumulated_json\"] = \"valid_json\"\n        self.accumulated_json = \"\"\n        self.sent_first_chunk = False\n        self.logging_obj = logging_obj\n        self.response_headers = response_headers or {}\n        self.is_function_call = check_is_function_call(logging_obj)\n        self.cumulative_tool_call_index: int = 0\n        self.has_seen_tool_calls: bool = False\n\n    @staticmethod\n    def _check_streaming_error(chunk: dict) -> None:\n        \"\"\"Detect embedded errors (e.g. 429 RESOURCE_EXHAUSTED) in streaming chunks and raise VertexAIError.\"\"\"\n        if \"error\" not in chunk:\n            return\n        error_data: Final = chunk[\"error\"]\n        if not isinstance(error_data, dict):\n            raise VertexAIError(\n                status_code=500,\n                message=f\"Unexpected error format in mid-stream chunk: {error_data}\",\n            )\n        raw_code = error_data.get(\"code\", 500)\n        if raw_code is None:\n            raw_code = 500\n        try:\n            error_code = int(raw_code)\n        except (TypeError, ValueError):\n            error_code = 500\n        error_message: Final = error_data.get(\"message\", \"Unknown error\")\n        error_status: Final = error_data.get(\"status\", \"UNKNOWN\")\n        raise VertexAIError(\n            status_code=error_code,\n            message=f\"{error_status} - {error_message}\",\n        )\n\n    def _apply_stream_candidates(","sourceCodeStart":3087,"sourceCodeEnd":3123,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/vertex_ai/gemini/vertex_and_google_ai_studio_gemini.py#L3087-L3123","documentation":"Gemini streaming error detector guard: a chunk contains an 'error' key whose value is not a dict (unexpected provider format), so no status/message can be extracted and a generic 500 VertexAIError naming the payload is raised.","triggerScenarios":"Triggered when a mid-stream chunk from Vertex AI Gemini contains an unexpected error format.","commonSituations":"See trigger scenarios.","solutions":["Inspect error_data in the mid-stream chunk; the stream returned an error payload.","Check the request for content that triggered a safety or quota error mid-stream."],"exampleFix":"# log error_data to see the stream error detail.","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}