BerriAI/litellm · error · OpenRouterException
KeyError: {e}, Got unexpected response from OpenRouter: {chu
Error message
KeyError: {e}, Got unexpected response from OpenRouter: {chunk} What it means
The streaming chunk from OpenRouter lacks expected keys (choices/delta/id), so chunk parsing raised KeyError; the raw chunk is embedded in the error because the response deviates from the documented streaming schema.
Source
Thrown at litellm/llms/openrouter/chat/transformation.py:282
message=error_message["message"],
status_code=error_message["code"],
headers=error_message["metadata"].get("headers", {}),
)
new_choices: Final = []
for choice in chunk["choices"]:
choice["delta"]["reasoning_content"] = choice["delta"].get("reasoning")
new_choices.append(choice)
return ModelResponseStream(
id=chunk["id"],
object="chat.completion.chunk",
created=chunk["created"],
usage=chunk.get("usage"),
model=chunk["model"],
choices=new_choices,
)
except KeyError as e:
raise OpenRouterException(
message=f"KeyError: {e}, Got unexpected response from OpenRouter: {chunk}",
status_code=400,
headers={"Content-Type": "application/json"},
)
except Exception as e:
raise e
View on GitHub (pinned to 77b7c6c40c)
Solutions
- Inspect the unexpected OpenRouter chunk in the message; retry and, if persistent, check OpenRouter status or adjust the model.
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at litellm/llms/openrouter/chat/transformation.py:282 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/5bb3061f989befea.
Report an issue: GitHub.