BerriAI/litellm · error · RuntimeError
Error parsing chunk: {e}, Received chunk: {chunk}
Error message
Error parsing chunk: {e},
Received chunk: {chunk} What it means
RuntimeError from the sync Anthropic stream iterator's parsing stage: after a raw line is successfully fetched, converting it into a GenericStreamingChunk raised a ValueError, and the handler re-raises it as RuntimeError including both the error and the raw chunk. The ' Received chunk:' part is the debugging gold — it shows the exact payload that could not be parsed.
Source
Thrown at litellm/llms/anthropic/chat/handler.py:1141
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,
finish_reason="",
usage=None,
index=0,
tool_use=None,
)
except StopIteration:
raise StopIteration
except ValueError as e:
raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")
# Async iterator
def __aiter__(self):
self.async_response_iterator = self.streaming_response.__aiter__()
return self
async def __anext__(self):
while True:
try:
chunk = await self.async_response_iterator.__anext__()
except StopAsyncIteration:
# 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:View on GitHub (pinned to 6c2dcb801b)
Solutions
- Look at 'Received chunk:' in the message to see the exact malformed payload and identify its origin (OpenAI-style, HTML, truncated).
- If the chunk looks like OpenAI SSE ('"choices": [...]'), remove the anthropic/ prefix or fix the model routing so the OpenAI transformation is used.
- If the chunk is truncated JSON, check for proxy buffering/read-timeout settings that cut the stream early.
- Reproduce with stream=False against the same endpoint to inspect the full response body.
- Catch RuntimeError around stream consumption and degrade gracefully (keep partial content, surface the error to the user).
Example fix
# before
result = "".join(c.choices[0].delta.content or "" for c in resp)
# after
result = []
try:
for c in resp:
if c.choices and c.choices[0].delta.content:
result.append(c.choices[0].delta.content)
except RuntimeError as e:
log.warning("anthropic stream parse failure: %s", e)
print("".join(result)) Defensive patterns
Strategy: try-catch
Try / catch
collected = []
try:
for chunk in stream:
collected.append(chunk)
except RuntimeError as e:
if "Error parsing chunk" in str(e):
log.error("unparseable frame: %s", e) # includes raw chunk
return merge_partial(collected) # graceful degradation
raise Prevention
- Verify endpoint speaks Anthropic SSE before switching model prefixes.
- Replay recorded Anthropic SSE fixtures in integration tests.
- Treat 'Received chunk:' content as the primary debugging clue.
When it happens
Trigger: Sync streaming where a 'data:' line's JSON parses but its shape is unexpected (missing fields the parser expects, unexpected event type), or where the chunk text is not valid JSON (e.g. 'data: [DONE]' style payloads from an OpenAI-format upstream served under an anthropic/ model prefix).
Common situations: Pointing anthropic/ models at OpenAI-compatible or mock servers; API format drift after an Anthropic API version change; gateways that append trailers or rewrite events; testing with recorded/replayed SSE fixtures that were truncated.
Related errors
- Failed to decode JSON from chunk: {chunk}
- Error receiving chunk from stream: {e}
- Error parsing chunk: {e}, Received chunk: {chunk}
- Received streaming error - {e}
- error_message or raw_response.text
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/266f59c67721513c.
Report an issue: GitHub.