[GH-ISSUE #5] "Improperly formed request" error on agent-mode payloads w/ codex client #5

Closed
opened 2026-02-27 07:17:20 +03:00 by kerem · 2 comments
Owner

Originally created by @stunl0ck on GitHub (Dec 17, 2025).
Original GitHub issue: https://github.com/jwadow/kiro-gateway/issues/5

Environment

  • Gateway version: v1.0.2 (via Docker)
  • Client: OpenAI Codex CLI (agent mode)
  • Auth: Refresh token (env var method)

Problem

Simple chat completions work fine. However, when using the gateway with an agent client that makes tool calls, requests fail with:

{"message":"Improperly formed request.","reason":null}

What I Tested

Scenario Result
Simple chat (Message with 180 Lines of text) Works
Fresh session, 48 MCP tools, first agent turn Fails immediately
Fresh session, MCP disabled (~6 core tools), agent mode Fails after 2-3 tool calls

Key finding: Even with MCP tools disabled (only Codex's core shell, apply_patch, update_plan tools), a fresh session fails after just a few tool calls. This isn't about accumulated history — something in the tool call/result conversion breaks for Kiro's API.

Debug Data

Using DEBUG_LAST_REQUEST=true, I captured:

  • request_body.json — incoming OpenAI-format request
  • kiro_request_body.json — converted Kiro-format request (~95KB)

The conversion appears complete (valid JSON, ends with profileArn), but Kiro's API rejects it.

Suspected Cause

Either:

  1. Kiro's API has strict limits on request complexity that agent sessions quickly exceed
  2. Something in the toolResults / assistantResponseMessage.toolUses conversion doesn't match what Kiro expects

Questions

  1. Are there known Kiro API limits for tool-heavy requests?
  2. Is there a known issue with multi-turn tool call history conversion?
Originally created by @stunl0ck on GitHub (Dec 17, 2025). Original GitHub issue: https://github.com/jwadow/kiro-gateway/issues/5 ## Environment - **Gateway version:** v1.0.2 (via Docker) - **Client:** OpenAI Codex CLI (agent mode) - **Auth:** Refresh token (env var method) ## Problem Simple chat completions work fine. However, when using the gateway with an agent client that makes tool calls, requests fail with: ```json {"message":"Improperly formed request.","reason":null} ``` ## What I Tested | Scenario | Result | |----------|--------| | Simple chat (Message with 180 Lines of text) | ✅ Works | | Fresh session, 48 MCP tools, first agent turn | ❌ Fails immediately | | Fresh session, MCP disabled (~6 core tools), agent mode | ❌ Fails after 2-3 tool calls | **Key finding:** Even with MCP tools disabled (only Codex's core `shell`, `apply_patch`, `update_plan` tools), a fresh session fails after just a few tool calls. This isn't about accumulated history — something in the tool call/result conversion breaks for Kiro's API. ## Debug Data Using `DEBUG_LAST_REQUEST=true`, I captured: - `request_body.json` — incoming OpenAI-format request - `kiro_request_body.json` — converted Kiro-format request (~95KB) The conversion appears complete (valid JSON, ends with `profileArn`), but Kiro's API rejects it. ## Suspected Cause Either: 1. Kiro's API has strict limits on request complexity that agent sessions quickly exceed 2. Something in the `toolResults` / `assistantResponseMessage.toolUses` conversion doesn't match what Kiro expects ## Questions 1. Are there known Kiro API limits for tool-heavy requests? 2. Is there a known issue with multi-turn tool call history conversion?
kerem 2026-02-27 07:17:20 +03:00
  • closed this issue
  • added the
    bug
    fixed
    labels
Author
Owner

@jwadow commented on GitHub (Dec 17, 2025):

@stunl0ck thanks for feedback

Good news - v1.0.4 is out and should fix your "MCP disabled, fails after 2-3 tool calls" scenario

What was fixed

The root cause was tool_calls being lost during message merging. When Codex CLI sends multiple assistant messages with tool_calls consecutively, the gateway was losing the second and subsequent tool_calls. This caused Kiro API to return {"message":"Improperly formed request.","reason":null} because it received toolResult without a corresponding toolUse.

What to test

  1. Update to v1.0.4: https://github.com/jwadow/kiro-openai-gateway/releases/tag/v1.0.4

  2. Test your 3rd "MCP disabled" scenario - this should now work

  3. Test your 2nd "48 MCP tools" scenario - this one I'm less certain about. If it still fails, it might be hitting Kiro API's payload size limits. Please report back!

New debugging feature

If you still encounter issues, v1.0.4 has a new debug mode that's much more convenient:

DEBUG_MODE=errors # Only saves logs when requests fail (4xx, 5xx)


P.S. Again, report about your issues. We'll investigate further. If the issues persist, we need you to attach both files for each problem. Because I definitely won't install 48 MCP tools 🤑

<!-- gh-comment-id:3663686658 --> @jwadow commented on GitHub (Dec 17, 2025): @stunl0ck thanks for feedback Good news - **v1.0.4** is out and should fix your "MCP disabled, fails after 2-3 tool calls" scenario ### What was fixed The root cause was **tool_calls being lost during message merging**. When Codex CLI sends multiple assistant messages with tool_calls consecutively, the gateway was losing the second and subsequent tool_calls. This caused Kiro API to return `{"message":"Improperly formed request.","reason":null}` because it received `toolResult` without a corresponding `toolUse`. ### What to test 1. **Update to v1.0.4**: https://github.com/jwadow/kiro-openai-gateway/releases/tag/v1.0.4 2. **Test your 3rd "MCP disabled" scenario** - this should now work ✅ 3. **Test your 2nd "48 MCP tools" scenario** - this one I'm less certain about. If it still fails, it might be hitting Kiro API's payload size limits. Please report back! ### New debugging feature If you still encounter issues, v1.0.4 has a new debug mode that's much more convenient: `DEBUG_MODE=errors # Only saves logs when requests fail (4xx, 5xx)` --- P.S. Again, report about your issues. We'll investigate further. If the issues persist, we need you to attach both files for each problem. Because I definitely won't install 48 MCP tools 🤑
Author
Owner

@stunl0ck commented on GitHub (Dec 17, 2025):

v1.0.4 is working great!

Tested with MCP tools enabled (48 tools) + agent-mode workflow using Codex CLI and all requests succeeding, multiple tool calls in a row, no more "Improperly formed request" errors.

Logs showing consistent 200s:

POST /v1/chat/completions (model=claude-opus-4-5, stream=True) 200 OK
POST /v1/chat/completions (model=claude-opus-4-5, stream=True) 200 OK
... (many more)
The tool_calls merging fix was exactly what was needed. Thanks for the quick turnaround! 😄

<!-- gh-comment-id:3664586385 --> @stunl0ck commented on GitHub (Dec 17, 2025): v1.0.4 is working great! Tested with MCP tools enabled (48 tools) + agent-mode workflow using Codex CLI and all requests succeeding, multiple tool calls in a row, no more "Improperly formed request" errors. Logs showing consistent 200s: POST /v1/chat/completions (model=claude-opus-4-5, stream=True) 200 OK POST /v1/chat/completions (model=claude-opus-4-5, stream=True) 200 OK ... (many more) The tool_calls merging fix was exactly what was needed. Thanks for the quick turnaround! 😄
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/kiro-gateway-jwadow#5
No description provided.