BerriAI/litellm · error · RuntimeError

Error receiving chunk from stream: {e}

Error message

Error receiving chunk from stream: {e}

What it means

RuntimeError raised in the sync Anthropic stream iterator's __next__ when the underlying response_iterator itself raises a ValueError while producing the next raw chunk. It is a wrapper: '{e}' carries the original error, so the root cause is whatever made the upstream/httpx iterator fail with a ValueError (most often the chunk_parser's JSON-decode ValueError at line 977 bubbling up through the iterator chain).

Source

Thrown at litellm/llms/anthropic/chat/handler.py:1114

    def __iter__(self):
        return self

    def __next__(self):
        while True:
            try:
                chunk = self.response_iterator.__next__()
            except StopIteration:
                # If we have accumulated JSON when stream ends, try to parse it
                if self.accumulated_json:
                    try:
                        data_json = json.loads(self.accumulated_json)
                        self.accumulated_json = ""
                        return self.chunk_parser(chunk=data_json)
                    except json.JSONDecodeError:
                        pass
                raise StopIteration
            except ValueError as e:
                raise RuntimeError(f"Error receiving chunk from stream: {e}")

            try:
                str_line = chunk
                if isinstance(chunk, bytes):  # Handle binary data
                    str_line = chunk.decode("utf-8")  # Convert bytes to string
                    index = str_line.find("data:")
                    if index != -1:
                        str_line = str_line[index:]

                if str_line.startswith("data:"):
                    result = self._parse_sse_data(str_line)
                    if result is not None:
                        return result
                    # If None, continue loop to get more chunks for accumulation
                else:
                    return GenericStreamingChunk(
                        text="",
                        is_finished=False,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Read the inner exception text after 'Error receiving chunk from stream:' — it names the actual failure (usually 'Failed to decode JSON from chunk: ...').
  2. Print/log the offending chunk from the inner message and compare it with Anthropic's documented SSE format (event: ... / data: {json}).
  3. If the base URL is an OpenAI-compatible gateway, call it without the anthropic/ prefix so LiteLLM uses the OpenAI parser.
  4. Test the same request against api.anthropic.com directly to isolate intermediary interference.
  5. Wrap the stream loop in try/except RuntimeError so a single bad frame can be logged instead of crashing the whole consumer.

Example fix

# before
chunks = list(response)  # RuntimeError aborts everything

# after
chunks = []
for c in response:
    try:
        chunks.append(c)
    except RuntimeError as e:
        log.error("stream error, keeping partial result: %s", e)
        break
Defensive patterns

Strategy: try-catch

Try / catch

try:
    chunks = list(stream)
except RuntimeError as e:
    if "Error receiving chunk from stream" in str(e):
        log.error("upstream stream failure: %s", e)
        # decide: retry request or degrade to partial results
    raise_or_return_partial()

Prevention

When it happens

Trigger: Sync streaming call (litellm.completion(..., stream=True) with anthropic/) where the provider stream yields data that fails parsing upstream of this loop: malformed SSE JSON frames, a decoder error in the httpx response iterator, or a ValueError from an embedded transformation step.

Common situations: Calling a non-Anthropic-compatible endpoint under the anthropic/ prefix (e.g. a fake/mock server or an OpenAI-compatible gateway) whose stream format the Anthropic parser cannot consume; corporate proxies injecting keep-alive or non-data lines; partial frames from network interruptions.

Related errors


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