Skip to content

Bug Report: JSON Decode Error in Claude Code SDKΒ #31

@lunartown

Description

@lunartown

Summary

Claude Code SDK crashes with CLIJSONDecodeError: Unterminated string due to truncated JSON messages in subprocess communication.

Error

json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 131 (char 130)

Location: claude_code_sdk/_internal/transport/subprocess_cli.py:192

Root Cause

The SDK's json.loads(line_str) fails when receiving partial/truncated JSON from subprocess. The message gets cut off at character 130,
causing parsing to fail.

Issue

  • SDK doesn't handle incomplete JSON messages
  • No buffering or retry for truncated data
  • Crashes entire TaskGroup on JSON parse error

Fix Needed

Add proper JSON validation and buffering in subprocess_cli.py before calling json.loads().

  ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.11/site-packages/claude_code_sdk/_internal/transport/subprocess_cli.py", line 192, in
  receive_messages
    |     data = json.loads(line_str)
    |            ^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads
    |     return _default_decoder.decode(s)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode
    |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/usr/local/lib/python3.11/json/decoder.py", line 353, in raw_decode
    |     obj, end = self.scan_once(s, idx)
    |                ^^^^^^^^^^^^^^^^^^^^^^
    | json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 131 (char 130)
    |
    | The above exception was the direct cause of the following exception:
    |
    | Traceback (most recent call last):
    |   File "/usr/local/lib/python3.11/site-packages/claude_code_sdk/_internal/transport/subprocess_cli.py", line 196, in
  receive_messages
    |     raise SDKJSONDecodeError(line_str, e) from e
    | claude_code_sdk._errors.CLIJSONDecodeError: Failed to decode JSON: 
  {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_01Fkq16zVZJG6jNpd1qWVvmt","...

Metadata

Metadata

Assignees

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