Skip to content

JSON parsing error when tool call arguments contain control characters #1378

@AlejandroEsquivel

Description

@AlejandroEsquivel

What version of Kimi Code CLI is running?

1.17.0

Which open platform/subscription were you using?

Moonshot AI Open Platform

Which model were you using?

kimi-2.5

What platform is your computer?

Linux 6.1.158 x86_64 unknown

What issue are you seeing?

When the LLM generates tool_call.function.arguments containing literal control characters (e.g. unescaped newlines in multi-line git commit messages), json.loads() raises a JSONDecodeError because Python's JSON parser rejects unescaped control characters (U+0000 through U+001F) by default.

This causes a cascade of failures:

  1. Tool execution failsjson.loads() in toolset.py:123 and simple.py:122 raises JSONDecodeError
  2. Session history is poisoned — the malformed arguments string is persisted to the context file
  3. Session becomes irrecoverable — on restore, json.loads(line) in context.py:40 fails, and on every subsequent API call _convert_message() sends the malformed arguments back to the API, which returns HTTP 400

What steps can reproduce the bug?

  1. Start a kimi-cli session
  2. Ask the LLM to run a shell command that includes a multi-line string, e.g.:
    Run: git commit -m "feat: add feature
    
    This is a multi-line
    commit message"
    
  3. The LLM generates a Shell tool call with literal newlines in the JSON arguments string
  4. Observe the error:
    ERROR: Error parsing JSON arguments: Invalid control character at: line 1 column 264 (char 263)
    
  5. The session is now broken — resuming it will fail with the same error or HTTP 400

What is the expected behavior?

No response

Additional information

Example trace

{
  "role": "assistant",
  "content": [],
  "tool_calls": [
    {
      "type": "function",
      "id": "functions.Shell:22",
      "function": {
        "name": "Shell",
        "arguments": "{\"command\": \"git commit -m \\\"feat: add feature\n\nMulti-line message\\\"\"}"
      }
    }
  ]
}
{
  "role": "tool",
  "content": "<system>ERROR: Error parsing JSON arguments: Invalid control character at: line 1 column 264 (char 263)</system>",
  "tool_call_id": "functions.Shell:22"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions