BerriAI/litellm · error · RuntimeError

Error receiving chunk from stream: {e}

Error message

Error receiving chunk from stream: {e}

What it means

The sync __next__ of the Cohere v1 streaming iterator wraps ValueError from the underlying response_iterator.__next__() as RuntimeError('Error receiving chunk from stream: {e}'). This fires when pulling the next raw chunk itself raises a ValueError (transport/decoding error inside httpx iteration), before LiteLLM's own chunk parsing runs.

Source

Thrown at litellm/llms/cohere/common_utils.py:170

                provider_specific_fields=provider_specific_fields,
            )

            return returned_chunk

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

    # Sync iterator
    def __iter__(self):
        return self

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

        try:
            return self.convert_str_chunk_to_generic_chunk(chunk=chunk)
        except StopIteration:
            raise StopIteration
        except ValueError as e:
            raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")

    def convert_str_chunk_to_generic_chunk(self, chunk: str) -> GenericStreamingChunk:
        """
        Convert a string chunk to a GenericStreamingChunk

        Note: This is used for Cohere pass through streaming logging
        """
        str_line = chunk
        if isinstance(chunk, bytes):  # Handle binary data
            str_line = chunk.decode("utf-8")  # Convert bytes to string
            index: Final = str_line.find("data:")

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Distinguish the two messages: 'Error receiving chunk' = transport-level; 'Error parsing chunk' = content-level — fix accordingly.
  2. Retry the streaming request with backoff for transient transport faults.
  3. Bypass proxies/VPNs between the runtime and api.cohere.com if corruption persists.
  4. Fall back to a non-streaming call to retrieve the full response when streaming reliability is unacceptable.
Defensive patterns

Strategy: retry

Try / catch

try:
    for chunk in stream:
        consume(chunk)
except RuntimeError as e:
    if "Error receiving chunk from stream" in str(e):
        backoff_and_retry_stream()  # transport-level, worth retrying
    raise

Prevention

When it happens

Trigger: Synchronous streaming iteration over a Cohere response where the underlying httpx line iterator raises ValueError while reading — e.g. malformed chunked transfer encoding or decode errors in the byte stream — as distinct from a chunk that parses but is invalid JSON (that path yields 'Error parsing chunk' instead).

Common situations: Interrupted streams (client disconnects mid-read), proxies mangling chunked encoding, rare httpx decoding edge cases. Usually transient; persistent occurrence points at a broken network path.

Related errors


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