{"record":{"id":"02c6d5cbd2ea956d","repo":"BerriAI/litellm","slug":"error-parsing-chunk-e-received-chunk-chunk-02c6d5","errorCode":null,"errorMessage":"Error parsing chunk: {e},\nReceived chunk: {chunk}","messagePattern":"Error parsing chunk: (.+?),\nReceived chunk: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"litellm/llms/base_llm/base_model_iterator.py","lineNumber":163,"sourceCode":"                str_line = chunk\n                if isinstance(chunk, bytes):  # Handle binary data\n                    str_line = chunk.decode(\"utf-8\")  # Convert bytes to string\n                    index = str_line.find(\"data:\")\n                    if index != -1:\n                        str_line = str_line[index:]\n\n                # Skip empty lines (common in SSE streams between events).\n                # Only apply to str chunks — non-string objects (e.g. Pydantic\n                # BaseModel events from the Responses API) must pass through.\n                if isinstance(str_line, str) and (not str_line or not str_line.strip()):\n                    continue\n\n                # chunk is a str at this point\n                return self._handle_string_chunk(str_line=str_line)\n            except StopIteration:\n                raise StopIteration\n            except ValueError as e:\n                raise RuntimeError(f\"Error parsing chunk: {e},\\nReceived chunk: {chunk}\")\n\n    # Async iterator\n    def __aiter__(self):\n        self.async_response_iterator = self.streaming_response.__aiter__()\n        return self\n\n    async def __anext__(self):\n        while True:\n            try:\n                chunk = await self.async_response_iterator.__anext__()\n\n            except StopAsyncIteration:\n                raise StopAsyncIteration\n            except ValueError as e:\n                raise RuntimeError(f\"Error receiving chunk from stream: {e}\")\n\n            try:\n                str_line = chunk","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/base_llm/base_model_iterator.py#L145-L181","documentation":"After a raw line is received and handed to _handle_string_chunk (which typically strips 'data:' prefixes and json.loads the payload), a ValueError from parsing — most commonly invalid JSON — is re-raised as RuntimeError together with the offending chunk. This catches malformed SSE payloads rather than transport errors.","triggerScenarios":"A streaming completion where a 'data:' line contains invalid JSON (truncated by a proxy timeout, HTML error page injected mid-stream, or a provider bug). Also when a custom iterator returns non-conforming strings.","commonSituations":"Corporate proxies or load balancers truncating long SSE responses; provider returning a 200 then error HTML; overloaded gateways splitting chunks at non-token boundaries; custom provider adapters emitting non-JSON data lines.","solutions":["Inspect the 'Received chunk:' portion — if it is HTML or partial JSON, the stream was corrupted upstream (proxy/timeout)","Increase proxy/gateway read timeouts (e.g. nginx proxy_read_timeout) for long generations","Retry with streaming; if reproducible, fall back to stream=False for that model","Report to the provider if the chunk shows valid-looking but malformed JSON from their side"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    for chunk in litellm.completion(model=m, messages=msgs, stream=True):\n        handle(chunk)\nexcept RuntimeError as e:\n    if 'Error parsing chunk' in str(e):\n        if '<html' in str(e).lower():\n            raise GatewayError('proxy injected HTML into SSE stream') from e\n        # else: retry or fall back to stream=False","preventionTips":["Disable response buffering on proxies in front of LLM endpoints","Keep partial-chunk logs so corrupted payloads can be attributed to the proxy or provider"],"tags":["streaming","sync","json","sse","parsing"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}