BerriAI/litellm · error · RuntimeError

Error receiving chunk from stream: {e}

Error message

Error receiving chunk from stream: {e}

What it means

Raised by the sync Databricks streaming iterator when pulling the next chunk from the underlying response raises a ValueError (per the code, chiefly the JSON-decode failure from chunk_parser). The original error text is embedded in a RuntimeError, aborting iteration of the stream.

Source

Thrown at litellm/llms/databricks/streaming_utils.py:105

                index=0,
            )
        except json.JSONDecodeError:
            raise ValueError(f"Failed to decode JSON from chunk: {chunk}")

    # Sync iterator
    def __iter__(self):
        self.response_iterator = self.streaming_response
        return self

    def __next__(self):
        if not hasattr(self, "response_iterator"):
            self.response_iterator = self.streaming_response
        try:
            chunk = self.response_iterator.__next__()
        except StopIteration:
            raise StopIteration
        except ValueError as e:
            raise RuntimeError(f"Error receiving chunk from stream: {e}")

        try:
            chunk = litellm.CustomStreamWrapper._strip_sse_data_from_chunk(chunk) or ""
            chunk = chunk.strip()
            if len(chunk) > 0:
                json_chunk: Final = json.loads(chunk)
                return self.chunk_parser(chunk=json_chunk)
            else:
                return GenericStreamingChunk(
                    text="",
                    is_finished=False,
                    finish_reason="",
                    usage=None,
                    index=0,
                    tool_use=None,
                )
        except StopIteration:
            raise StopIteration

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Inspect the embedded inner message to find the raw chunk that failed to parse
  2. Add retry logic around the whole completion request for stream read failures
  3. Tune or disable proxy buffering/idle timeouts on the path to Databricks
  4. Consider non-streaming mode for unstable network paths

Example fix

# before
for chunk in response:
    print(chunk)

# after
try:
    for chunk in response:
        print(chunk)
except RuntimeError as e:
    logger.error("stream failed: %s", e)
    response = litellm.completion(**params)  # retry once
    for chunk in response:
        print(chunk)
Defensive patterns

Strategy: retry

Try / catch

for attempt in range(2):
    try:
        response = litellm.completion(**params, stream=True)
        for chunk in response:
            handle(chunk)
        break
    except RuntimeError as e:
        if attempt == 1 or "Error receiving chunk from stream" not in str(e):
            raise

Prevention

When it happens

Trigger: Iterating a synchronous Databricks stream (for chunk in response:) where the transport raises a ValueError while reading the next SSE line — most commonly the 'Failed to decode JSON from chunk' failure propagated from the parser.

Common situations: Sync consumers of long Databricks streams through flaky proxies; streams cut mid-event by idle timeouts; partial event delivery under load.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/0c2f16828cc54647. Report an issue: GitHub.