Skip to content

Commit fcb799c

Browse files
committed
fix: use strict=False for JSON parsing of tool calls and session data
LLM responses sometimes contain raw control characters (newlines, tabs) inside JSON string values in tool call arguments. With the default strict=True, json.loads() rejects these, causing tool call failures and corrupting the session context file so it can never be restored. Use strict=False in all critical JSON parsing paths: - Tool call argument parsing (toolset.py) - Session context restore and checkpoint revert (context.py) - Tool argument extraction for display (tools/__init__.py) - Debug tool call formatting (debug.py) Fixes #1378
1 parent 7ba9695 commit fcb799c

4 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/kimi_cli/soul/context.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def restore(self) -> bool:
3838
async for line in f:
3939
if not line.strip():
4040
continue
41-
line_json = json.loads(line)
41+
line_json = json.loads(line, strict=False)
4242
if line_json["role"] == "_system_prompt":
4343
self._system_prompt = line_json["content"]
4444
continue
@@ -154,7 +154,7 @@ async def revert_to(self, checkpoint_id: int):
154154
if not line.strip():
155155
continue
156156

157-
line_json = json.loads(line)
157+
line_json = json.loads(line, strict=False)
158158
if line_json["role"] == "_checkpoint" and line_json["id"] == checkpoint_id:
159159
break
160160

src/kimi_cli/soul/toolset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def handle(self, tool_call: ToolCall) -> HandleResult:
120120
tool = self._tool_dict[tool_call.function.name]
121121

122122
try:
123-
arguments: JsonType = json.loads(tool_call.function.arguments or "{}")
123+
arguments: JsonType = json.loads(tool_call.function.arguments or "{}", strict=False)
124124
except json.JSONDecodeError as e:
125125
return ToolResult(tool_call_id=tool_call.id, return_value=ToolParseError(str(e)))
126126

src/kimi_cli/tools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def extract_key_argument(json_content: str | streamingjson.Lexer, tool_name: str
2020
else:
2121
json_str = json_content
2222
try:
23-
curr_args: JsonType = json.loads(json_str)
23+
curr_args: JsonType = json.loads(json_str, strict=False)
2424
except json.JSONDecodeError:
2525
return None
2626
if not curr_args:

src/kimi_cli/ui/shell/debug.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _format_tool_call(tool_call: ToolCall) -> Panel:
7070
"""Format a tool call."""
7171
args = tool_call.function.arguments or "{}"
7272
try:
73-
args_formatted = json.dumps(json.loads(args), indent=2)
73+
args_formatted = json.dumps(json.loads(args, strict=False), indent=2)
7474
args_syntax = Syntax(args_formatted, "json", theme="monokai", padding=(0, 1))
7575
except json.JSONDecodeError:
7676
args_syntax = Text(args, style="red")

0 commit comments

Comments
 (0)