BerriAI/litellm · error · RuntimeError

Error parsing chunk: {e}, Received chunk: {chunk}

Error message

Error parsing chunk: {e},
Received chunk: {chunk}

What it means

The second stage of the sync Cohere v1 streaming iterator: the raw chunk was received fine, but convert_str_chunk_to_generic_chunk raised ValueError while transforming it (bad JSON inside the data: payload, unexpected event type). It is re-raised as RuntimeError including both the original error and the full received chunk, making diagnosis direct.

Source

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

    # 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:")
            if index != -1:
                str_line = str_line[index:]

        data_json: Final = json.loads(str_line)
        return self.chunk_parser(chunk=data_json)

    # Async iterator

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Read the 'Received chunk:' portion to see the exact malformed payload.
  2. If the payload is a Cohere error event, address its stated cause (context length, overloaded) and retry.
  3. Upgrade litellm to pick up current Cohere stream-event parsing.
  4. As a mitigation, catch RuntimeError around stream iteration and retry or degrade to non-streaming.
Defensive patterns

Strategy: try-catch

Try / catch

try:
    for chunk in stream:
        consume(chunk)
except RuntimeError as e:
    if "Error parsing chunk" in str(e):
        capture_diagnostics(str(e))  # contains 'Received chunk: ...'
        if looks_like_schema_drift(str(e)):
            suggest_upgrade()
        raise

Prevention

When it happens

Trigger: Sync streaming cohere v1 chat where a data: line contains invalid JSON or a v1-unrecognized event shape — in-stream error events, keep-alives with stray payloads, or schema drift between the Cohere API and the litellm version installed.

Common situations: Cohere changes stream event shapes and the installed litellm predates the change; mid-stream error events; binary/UTF-8 decode issues in bytes chunks.

Related errors


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