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 iteratorView on GitHub (pinned to 6c2dcb801b)
Solutions
- Read the 'Received chunk:' portion to see the exact malformed payload.
- If the payload is a Cohere error event, address its stated cause (context length, overloaded) and retry.
- Upgrade litellm to pick up current Cohere stream-event parsing.
- 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
- Pin and regularly upgrade litellm to track Cohere v1 stream schema changes.
- Log the 'Received chunk' payload on failure — it is the evidence needed for bug reports.
- Wrap stream consumption in a helper that catches RuntimeError uniformly across providers.
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
- Failed to decode JSON from chunk: {chunk}
- Error receiving chunk from stream: {e}
- Stream ended without a completed response
- Error receiving chunk from stream: {e}
- Error receiving chunk from stream: {e}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/9c5cbcf31abf2a02.
Report an issue: GitHub.