BerriAI/litellm · error · ValueError
Unable to parse response. Original response
Error message
Unable to parse response. Original response: {chunk} What it means
AI21 streaming parser guard: the chunk JSON could not be parsed into the expected completions[0].data.text shape, so an exception is raised including the original raw chunk for debugging — the provider sent an unexpected stream payload.
Solutions
- The provider returned a non-JSON or malformed chunk; log the original response and validate the endpoint.
- Check for proxies modifying the response body.
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at litellm/litellm_core_utils/streaming_handler.py:540 when the library encounters an invalid state.
Common situations: See trigger scenarios.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/d487fad2666b3547.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/litellm_core_utils/streaming_handler.py:540
"finish_reason": finish_reason,
}
except Exception as e:
raise e
def handle_ai21_chunk(self, chunk): # fake streaming
chunk = chunk.decode("utf-8")
data_json: Final[_Ai21StreamData] = json.loads(chunk)
try:
text: Final = data_json["completions"][0]["data"]["text"]
is_finished: Final = True
finish_reason: Final = "stop"
return {
"text": text,
"is_finished": is_finished,
"finish_reason": finish_reason,
}
except Exception:
raise ValueError(f"Unable to parse response. Original response: {chunk}")
def handle_maritalk_chunk(self, chunk): # fake streaming
chunk = chunk.decode("utf-8")
data_json: Final[_MaritalkStreamData] = json.loads(chunk)
try:
text: Final = data_json["answer"]
is_finished: Final = True
finish_reason: Final = "stop"
return {
"text": text,
"is_finished": is_finished,
"finish_reason": finish_reason,
}
except Exception:
raise ValueError(f"Unable to parse response. Original response: {chunk}")
def handle_nlp_cloud_chunk(self, chunk):
text = ""View on GitHub (pinned to 77b7c6c40c)