fix(kiro): resolve Improperly formed request when tool_calls in history (#1184)#1209
Open
DuongStark wants to merge 1 commit into
Open
Conversation
…ry (decolua#1184) - Use toolsAttached flag instead of history.length === 0 to attach tools schema, fixing loss of schema when conversation starts with assistant turns - Synthesize minimal tool schema from tool_calls in history when body.tools is empty — prevents Kiro 400 when clients omit tools but history has tool_calls - Merge userInputMessageContext (toolResults) when collapsing consecutive user messages so tool results are not silently dropped - Replace splice-from-end currentMessage extraction with history.pop() to preserve chronological order; synthesize Continue turn when last message is not a user turn All 7 unit tests pass (npx vitest run tests/unit/openai-to-kiro.test.js)
|
@decolua Could you please check it. It blocked us to use claude code :( |
TuanChill
approved these changes
May 19, 2026
DungAT98
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Kiro returns
400 Improperly formed requestwhen conversations have many tool calls (reported in #1184). Root cause:openai-to-kiro.jsdrops messages and tool schemas under certain conditions.Root Causes Fixed
Bug 1 — tools schema lost when conversation starts with assistant turns
convertMessages()usedhistory.length === 0to decide when to attach the tools schema. When the first messages are assistant turns, the first user flush sees a non-empty history and the schema is never attached.Fix: use a
toolsAttachedflag instead.Bug 2 — missing tool schema when
body.toolsis empty (main cause of #1184)Kiro rejects requests that reference
toolUses/toolResultsin history without a matching tools schema inuserInputMessageContext. When clients omitbody.toolsbut the history still containsassistant.tool_calls, 9router sent no schema → Kiro returned 400.Fix: in
buildKiroPayload(), scan message history fortool_callsand synthesize a minimal tool schema whenbody.toolsis empty.Bug 3 — toolResults lost when merging consecutive user messages
The merge loop concatenated content strings but silently dropped
userInputMessageContext(which carriestoolResults) from the second message.Fix: merge
userInputMessageContextby concatenating array fields.Bonus — currentMessage extraction reordered history
The old splice-from-end scan could pull a non-final user turn out of the middle of history, breaking chronological order.
Fix: use
history.pop()when the last entry is a user turn; otherwise synthesize aContinuemessage.Tests
All 7 existing unit tests pass:
Closes #1184