BerriAI/litellm · error · ValueError
Failed to parse v2 chunk: {e}, chunk: {chunk}
Error message
Failed to parse v2 chunk: {e}, chunk: {chunk} What it means
The Cohere v2 streaming chunk parser wraps its entire conversion in a broad except Exception: any failure while turning a v2 SSE event into a GenericStreamingChunk (missing expected fields like type/delta, unexpected event type, JSON issues after data: stripping) becomes ValueError('Failed to parse v2 chunk: {e}, chunk: {chunk}') including the original exception and raw chunk.
Source
Thrown at litellm/llms/cohere/common_utils.py:350
# Handle citations in any chunk type (fallback)
if "citations" in chunk:
if provider_specific_fields is None:
provider_specific_fields = {}
provider_specific_fields["citations"] = chunk["citations"]
return GenericStreamingChunk(
text=text,
tool_use=tool_use,
is_finished=is_finished,
finish_reason=finish_reason,
usage=usage,
index=index,
provider_specific_fields=provider_specific_fields,
)
except Exception as e:
raise ValueError(f"Failed to parse v2 chunk: {e}, 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:View on GitHub (pinned to 6c2dcb801b)
Solutions
- Read the embedded chunk and exception: an unrecognized 'type' field means schema drift — upgrade litellm.
- An in-stream error event points to a request-level cause (context length, moderation) — fix and retry.
- Truncated payloads indicate transport issues — retry with backoff.
- Report unrecognized-but-valid event types to the litellm repo with the chunk payload.
Defensive patterns
Strategy: try-catch
Try / catch
try:
for chunk in cohere_v2_stream:
consume(chunk)
except ValueError as e:
if "Failed to parse v2 chunk" in str(e):
if is_unknown_event_type(str(e)):
file_litellm_issue_with_chunk(str(e))
retry_stream() # truncated/error events are often transient
raise Prevention
- Track litellm release notes for Cohere v2 stream support updates and upgrade promptly.
- Model tool-use and citation flows explicitly in tests — these emit the richest event sets.
- Capture the raw chunk on failure; it distinguishes schema drift from transient errors.
When it happens
Trigger: Streaming cohere v2 models (command-r, command-r-plus, command-a) when an SSE event deviates from the expected v2 shapes: new event types introduced by Cohere (e.g. new content-block events), malformed data payloads, or in-stream error events.
Common situations: Schema drift: Cohere adds event types and the installed litellm version cannot parse them; tool-use streams with event sequences the parser does not cover; truncated events from network faults.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to decode JSON from chunk: {chunk}
- Error receiving chunk from stream: {e}
- Error parsing chunk: {e}, Received chunk: {chunk}
- Chunk cannot be parsed as CohereStreamChunk: {e}
- Braintrust API error: {e.response.text}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/20f05baaa0f3245d.
Report an issue: GitHub.