Skip to content

[Bug]: Tool call arguments JSON serialization truncation causing HTTP 400 errors #3953

@zc4201

Description

@zc4201

Version line

v2 — Go rewrite (1.x), main-v2 (active development)

Exact version

v1.5.0

What happened?

20260609-084614.919975000-deepseek-v4-flash.jsonl.telemetry.json
Reasonix 在将模型输出的工具调用参数序列化为 JSON 时,某些情况下 JSON 没有被完整序列化就发送给了 API,导致 API 收到的是损坏的 JSON,返回 400 错误。

Steps to reproduce

[##]([url](

20260610-063757.391625100-deepseek-v4-flash.jsonl.telemetry.json

)) Summary
Reasonix 在序列化工具调用的 arguments 字段时,某些情况下 JSON 没有被完整序列化就发送给了 LLM API,导致 API 返回 HTTP 400 错误:

  • "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"
  • "Expecting ',' delimiter: line 1 column 11 (char 10)"

Evidence

Case 1: bash tool call (Session: 20260609-084614, Line 102)

Log entry:

{"role":"assistant","content":"**第13题(多选)**:关于考核规则,描述正确的是...","tool_calls":[{"id":"call_2eeda6fb42ed440c9a7997b6","name":"bash","arguments":"{"}]}

Problem: arguments 值仅为 "{",后面应该有完整的命令但全部丢失。
Expected: 类似 "{"command": "bb-browser click @111 --tab ed3e && bb-browser click @115 --tab ed3e && bb-browser click @117 --tab ed3e"}"
API Response:

{"role":"tool","content":"error: invalid args: unexpected end of JSON input\n\nThe arguments were not valid JSON...","tool_call_id":"call_2eeda6fb42ed440c9a7997b6","name":"bash"}

Case 2: browser_wait_for tool call (Session: 20260610-063757, Line 209)

Log entry:

{"role":"assistant","tool_calls":[{"id":"call_2c80570a2cbe4edca3cdf3ba","name":"mcp__playwright__browser_wait_for","arguments":"{\"time\": 2"}]}

Problem: JSON 缺少闭合的 },应该是 "{"time": 2}"
API Response:

error: invalid args: unexpected end of JSON input

Case 3: Another truncation (Recent error)

Error message:

Malformed request (HTTP 400): the request body was rejected.
***.BadRequestError: OpenAIException - {"error":{"message":"Expecting ',' delimiter: line 1 column 11 (char 10)","type":"BadRequestError","param":null,"code":400}}

Analysis: Error at character 10 indicates JSON is being cut off mid-serialization.

Root Cause Analysis

The arguments field in tool calls is being truncated during serialization. This happens:

  1. Not related to context size — Case 2 occurred in a fresh, short session
  2. Affects multiple tool types — Both bash and mcp__playwright__browser_wait_for are affected
  3. Consistent pattern — JSON objects are either incomplete or missing closing braces
    Possible causes:
  • Stream response buffering issue where JSON serialization is interrupted
  • Race condition in tool call argument processing
  • Buffer flush timing issue with certain tool types

Steps to Reproduce

  1. Start a new Reasonix session
  2. Ask the agent to use tools that require complex arguments (e.g., bash with multiple commands, browser_wait_for, or parallel web_fetch calls)
  3. Occasionally the arguments field gets truncated

Environment

  • OS: Windows 11
  • Reasonix config version: 2
  • Default model: deepseek-v4-flash (via DeepSeek API)
  • Alternative model tested: Agnes AI/agnes-2.0-flash

Session Logs

Logs are located at:

  • C:\Users\zheng\AppData\Roaming\reasonix\sessions\20260609-084614.919975000-deepseek-v4-flash.jsonl
  • C:\Users\zheng\AppData\Roaming\reasonix\sessions\20260610-063757.391625100-deepseek-v4-flash.jsonl

Impact

  • Causes HTTP 400 errors from LLM API
  • Interrupts agent workflow
  • Requires user to start a new session to continue
  • Wastes API tokens on failed requests

Suggested Fix

  1. Add validation in the tool call serialization layer to ensure JSON is complete before sending
  2. Implement retry logic when malformed JSON is detected
  3. Add logging to capture the raw model output before serialization to help debug truncation points
  4. Consider adding a buffer/flush mechanism for streaming responses

OS / platform

Windows11

Relevant logs or output

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentCore agent loop (internal/agent, internal/control)bugSomething isn't workingv2Go rewrite (1.x) — main-v2 branch, active developmentwindowsWindows-specific

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions