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

  1. Read the embedded chunk and exception: an unrecognized 'type' field means schema drift — upgrade litellm.
  2. An in-stream error event points to a request-level cause (context length, moderation) — fix and retry.
  3. Truncated payloads indicate transport issues — retry with backoff.
  4. 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

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

Related errors


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