{"record":{"id":"0c2f16828cc54647","repo":"BerriAI/litellm","slug":"error-receiving-chunk-from-stream-e-0c2f16","errorCode":null,"errorMessage":"Error receiving chunk from stream: {e}","messagePattern":"Error receiving chunk from stream: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"litellm/llms/databricks/streaming_utils.py","lineNumber":105,"sourceCode":"                index=0,\n            )\n        except json.JSONDecodeError:\n            raise ValueError(f\"Failed to decode JSON from chunk: {chunk}\")\n\n    # Sync iterator\n    def __iter__(self):\n        self.response_iterator = self.streaming_response\n        return self\n\n    def __next__(self):\n        if not hasattr(self, \"response_iterator\"):\n            self.response_iterator = self.streaming_response\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            chunk = litellm.CustomStreamWrapper._strip_sse_data_from_chunk(chunk) or \"\"\n            chunk = chunk.strip()\n            if len(chunk) > 0:\n                json_chunk: Final = json.loads(chunk)\n                return self.chunk_parser(chunk=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        except StopIteration:\n            raise StopIteration","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/databricks/streaming_utils.py#L87-L123","documentation":"The Databricks streaming iterator wraps the underlying HTTP response iterator: when response_iterator.__next__() raises ValueError (httpx raises it on decoding failures, e.g. content decoding errors or a body that violates Content-Length), it is converted to RuntimeError('Error receiving chunk from stream: ...'). This is a transport-level failure of the byte stream mid-iteration, not a schema problem — the stream is dead and iteration stops. The async path wraps all exceptions the same way.","triggerScenarios":"Streaming a Databricks completion when the connection degrades mid-stream: encoding mismatch ('Unable to decode content with encoding ...'), truncated body (content shorter/longer than Content-Length), proxy closing or corrupting the connection, or any ValueError raised by the httpx line/byte iterator.","commonSituations":"Long streams through flaky proxies/VPNs; servers sending wrong Content-Length or charset headers; mobile/ephemeral networks dropping connections partway; load balancers with aggressive idle timeouts.","solutions":["Inspect the wrapped exception text after 'stream:' — it names the underlying transport failure (decode error, content-length mismatch)","Retry the request with a fresh connection; transient stream corruption usually clears","If it recurs on the same endpoint, check proxy/LB idle timeouts and the endpoint's response headers (Content-Length, Content-Encoding, charset)","For very long completions, reduce max_tokens or chunk the work so streams are shorter-lived"],"exampleFix":"# before\nfor part in litellm.completion(model='databricks/my-model', messages=msgs, stream=True):\n    print(part)  # RuntimeError kills iteration mid-stream\n\n# after — retry streaming once on transport failure\nfrom tenacity import retry, retry_if_exception, stop_after_attempt\n\n@retry(retry=retry_if_exception(lambda e: 'Error receiving chunk from stream' in str(e)),\n       stop=stop_after_attempt(2))\ndef stream_completion():\n    return litellm.completion(model='databricks/my-model', messages=msgs, stream=True)\n\nfor part in stream_completion():\n    print(part)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"def stream_with_retry(model, messages, attempts=2):\n    for i in range(attempts):\n        try:\n            return litellm.completion(model=model, messages=messages, stream=True)\n        except RuntimeError as e:\n            if i == attempts - 1 or 'Error receiving chunk from stream' not in str(e):\n                raise\n    # caller re-iterates; partial output is lost, retry restarts the completion\n\nstream = stream_with_retry('databricks/my-ep', msgs)\nfor chunk in stream:\n    handle(chunk)","preventionTips":["Raise proxy/LB idle timeouts above the longest expected stream duration","Retry streaming requests once on transport failure - most mid-stream ValueErrors are transient","Keep max_tokens modest for unstable network paths so streams are short-lived"],"tags":["databricks","streaming","network","connection-reset"],"backgroundTag":"streaming-read-error","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}