Skip to content

fix(converters): skip thinking tag injection when toolResults present#23

Merged
jwadow merged 1 commit intojwadow:mainfrom
kilhyeonjun:fix/skip-thinking-tags-with-tool-results
Jan 8, 2026
Merged

fix(converters): skip thinking tag injection when toolResults present#23
jwadow merged 1 commit intojwadow:mainfrom
kilhyeonjun:fix/skip-thinking-tags-with-tool-results

Conversation

@kilhyeonjun
Copy link
Copy Markdown
Contributor

Summary

Fixes #20 - OpenCode compaction returns 400 "Improperly formed request" error.

Root Cause

When the last message contains tool results:

  1. merge_adjacent_messages converts tool messages to user message with toolResults
  2. inject_thinking_tags adds <thinking_mode>... tags to content
  3. Kiro API rejects requests with both toolResults in userInputMessageContext AND thinking tags in content
  4. Results in 400 "Improperly formed request" error

Solution

Check for toolResults before injecting thinking tags. Skip injection when toolResults are present in the current message.

Changes

  • Reorder build_kiro_payload to build user_input_context before thinking tag injection
  • Add check to skip thinking tags when toolResults present
  • Add debug log for skipped injection

Tests

  • test_skips_thinking_tags_when_tool_results_present - verifies thinking tags are NOT injected when toolResults present
  • test_injects_thinking_tags_when_no_tool_results - verifies normal messages still get thinking tags

Verification

✅ 213 passed (test_converters.py, test_auth_manager.py, test_thinking_parser.py)

Fixes jwadow#20 - OpenCode compaction returns 400 Improperly formed request error.

Root cause: When the last message contains tool results, Kiro API rejects
requests that have both toolResults in userInputMessageContext AND
thinking tags in content. The combination causes 400 'Improperly formed
request' error.

Solution: Check for toolResults before injecting thinking tags. Skip
injection when toolResults are present in the current message.

Changes:
- Reorder build_kiro_payload to build user_input_context before thinking
  tag injection
- Add check to skip thinking tags when toolResults present
- Add debug log for skipped injection

Tests:
- test_skips_thinking_tags_when_tool_results_present
- test_injects_thinking_tags_when_no_tool_results
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Jan 8, 2026

Thanks for the PR! 🎉

Before merge, we need a one-time CLA confirmation.
It confirms that you have the right to contribute this code and allow the project to use it.

Full CLA text:
https://github.com/jwadow/kiro-gateway/blob/main/CLA.md

Please reply once with:

I have read the CLA and I accept its terms

@kilhyeonjun
Copy link
Copy Markdown
Contributor Author

I have read the CLA and I accept its terms

Copy link
Copy Markdown
Owner

@jwadow jwadow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! You nailed the root cause - Kiro API really doesn't like thinking tags mixed with toolResults. The fix looks good, minimal changes and does exactly what's needed.

One small thing I noticed - the elif has_tool_results could technically trigger even when role isn't user, so the debug log might be a bit misleading in edge cases. Not a blocker though, I can clean that up later if needed.

Thanks for the PR, merging!

@jwadow
Copy link
Copy Markdown
Owner

jwadow commented Jan 8, 2026

@cla-bot check

@cla-bot cla-bot bot added the cla-signed Contributor License Agreement has been signed label Jan 8, 2026
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Jan 8, 2026

The cla-bot has been summoned, and re-checked this pull request!

@jwadow jwadow merged commit ee8b561 into jwadow:main Jan 8, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed Contributor License Agreement has been signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OpenCode compaction returns 400 Improperly formed request

2 participants