mirror of
https://github.com/jwadow/kiro-gateway.git
synced 2026-04-25 09:25:50 +03:00
[GH-ISSUE #60] BUG: Improperly formed request when messages don't alternate or start with assistant #40
Labels
No labels
bug
bug
enhancement
enhancement
fixed
fixed
invalid
needs-info
needs-testing
pull-request
question
upstream
wontfix
workaround
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/kiro-gateway-jwadow#40
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @bhaskoro-muthohar on GitHub (Feb 1, 2026).
Original GitHub issue: https://github.com/jwadow/kiro-gateway/issues/60
Kiro Gateway Version
latest (as of 2026-02-01)
What happened?
When using kiro-gateway with OpenClaw (formerly ClawdBot) as the client, requests fail with HTTP 400
"Improperly formed request"when the conversation history contains:This happens because the Claude/Anthropic API requires:
userandassistantrolesuserroleOpenClaw builds conversation history from chat platforms (WhatsApp, Telegram, etc.) where multiple messages from the same user can arrive before the bot responds, resulting in consecutive user messages.
Root Cause
LiteLLM handles this automatically in
anthropic_messages_pt()by:{"role": "user", "content": [{"type": "text", "text": "."}]}if the first message isn't from userKiro-gateway doesn't have this preprocessing, so malformed requests pass through to the Kiro API.
Suggested Fix
Add a
merge_consecutive_messages()function that runs beforeanthropic_to_kiro():Debug Logs
Example failing request (before fix):
After fix, messages are properly merged (35 → 30 messages) and first message is user.
@bhaskoro-muthohar commented on GitHub (Feb 1, 2026):
@jwadow I also noticed another issue that might affect users:
CONTENT_LENGTH_EXCEEDS_THRESHOLDerrors when conversations exceed the model's context window.Probably the solution would be to add proactive context trimming before sending requests - similar to how LiteLLM handles this with their
trim_messages()function. It could leverage the existingtokenizer.pyinfrastructure and trim oldest messages when approaching the limit.Should I open a separate issue for this?
@jwadow commented on GitHub (Feb 2, 2026):
Hi, our gateway is transparent, we make minimal changes to the user's original request (with the exception of Kiro’s bugs and shortcomings).
If the user nevertheless encounters such a context blocking error, then we do not have the right to change the request itself. Because if a user made the exact same request to the official Anthropic API, they would encounter exactly the same problem. But Anthropic does not fix this problem in any way too.
This problem should be fixed by the client running our gateway, be it OpenCode, Claude Code or something else. That is, this is their problem. Therefore, I will not modify the request in any way, much less delete the context or unnecessary messages. Because user needs are absolutely diverse.
@jwadow commented on GitHub (Feb 2, 2026):
Good catch. Reproduced the issue - Kiro API rejects conversations that don't start with user role.
Fixed by prepending a minimal synthetic user message when needed (just a dot, like LiteLLM does). Tested with your examples, all pass now.
Updated CONTRIBUTORS.md with your contribution. Thanks for the detailed report.