{"record":{"id":"20ab12eda9db452c","repo":"BerriAI/litellm","slug":"error-receiving-chunk-from-stream-e-20ab12","errorCode":null,"errorMessage":"Error receiving chunk from stream: {e}","messagePattern":"Error receiving chunk from stream: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"litellm/llms/base_llm/base_model_iterator.py","lineNumber":142,"sourceCode":"            return self.chunk_parser(chunk=stripped_json_chunk)\n        else:\n            return GenericStreamingChunk(\n                text=\"\",\n                is_finished=False,\n                finish_reason=\"\",\n                usage=None,\n                index=0,\n                tool_use=None,\n            )\n\n    def __next__(self):\n        while True:\n            try:\n                chunk = self.response_iterator.__next__()\n            except StopIteration:\n                raise StopIteration\n            except ValueError as e:\n                raise RuntimeError(f\"Error receiving chunk from stream: {e}\")\n\n            try:\n                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:","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/base_llm/base_model_iterator.py#L124-L160","documentation":"In the sync chunk-reading loop of BaseModelResponseIterator, a ValueError raised by the underlying response iterator (the provider's HTTP/SSE stream) is re-raised as RuntimeError so it isn't mistaken for a clean stream end. The original error text is preserved after the colon.","triggerScenarios":"The upstream iterator raises ValueError while yielding bytes — e.g. invalid UTF-8 in a chunk being decoded, or an httpx/aiohttp-level protocol error surfacing as ValueError during iteration on a streaming chat completion.","commonSituations":"A proxy or gateway corrupting SSE bytes; provider sending non-UTF8 payloads mid-stream; reading a stream after the connection was closed and reused; rarely, bugs in provider adapters that raise ValueError in their generators.","solutions":["Read the original error after 'Error receiving chunk from stream:' to identify the decode/protocol failure","Retry the request — transient stream corruption often resolves; wrap streaming calls in a retry loop","If persistent for one provider, disable streaming for that model (stream=False) to get the full-body error","Upgrade litellm and httpx — byte-decoding edge cases get patched"],"exampleFix":null,"handlingStrategy":"retry","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 receiving chunk from stream' in str(e):\n        # transient stream corruption: retry once, else degrade to non-streaming\n        response = litellm.completion(model=m, messages=msgs, stream=False)\n    else:\n        raise","preventionTips":["Set explicit read timeouts on streaming clients","Design stream consumers to be idempotent so a retry is safe"],"tags":["streaming","sync","runtime-error","network"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}