JSONDecodeError: Subprocess buffer truncates large messages
Bug Description
The SDK fails with JSONDecodeError: Unterminated string when Claude generates very long responses. This occurs because the subprocess pipe buffer truncates large JSON messages.
Reproduction Steps
- Use the SDK with a prompt that generates a long response:
import asyncio
from claude_code_sdk import query, ClaudeCodeOptions
async def test_long_response():
options = ClaudeCodeOptions(
allowed_tools=["*"],
permission_mode="bypassPermissions",
max_turns=None
)
async for message in query(
prompt="Please send me a long message about what you would improve and why. Ultrathink it.",
options=options
):
print(f"Message type: {type(message).__name__}")
- The SDK will fail with an error like:
json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 170 (char 169)
claude_code_sdk._errors.CLIJSONDecodeError: Failed to decode JSON: {"type":"assistant","message":{"id":"msg_01...","type":"message","role":"assistan...
Root Cause
The issue is in subprocess_cli.py where anyio.open_process() is called without specifying a buffer size. The default subprocess pipe buffer (typically 8KB-64KB) is too small for large JSON messages on a single line.
When Claude generates a very long response, the JSON message gets truncated at the pipe buffer limit, causing JSON parsing to fail.
Expected Behavior
The SDK should handle responses of any reasonable size without JSON parsing errors.
Environment
- claude-code-sdk version: 0.0.11
- Python version: 3.12
- OS: Linux (WSL2)
Proposed Solution
Increase the subprocess buffer size when creating the process:
self._process = await anyio.open_process(
cmd,
stdin=None,
stdout=PIPE,
stderr=PIPE,
cwd=self._cwd,
env={**os.environ, "CLAUDE_CODE_ENTRYPOINT": "sdk-py"},
bufsize=10 * 1024 * 1024 # 10MB buffer
)
This is a simple fix that resolves the immediate issue. A more robust solution might involve implementing a JSON accumulator that can handle partial messages, but the buffer size increase should handle most real-world cases.
Related Issues
- #31 (JSON Decode Error)
- #15 (CLIJSONDecodeError with multiple JSON objects)
- #6 (JSON parsing issues)
Please check if a solution like this will help: https://github.com/agronholm/anyio/blob/561d81270a12f7c6bbafb5bc5fad99a2a13f96be/src/anyio/_core/_subprocesses.py#L81
I replaced 'open_process' with 'run_process', and it's working for me. But instead of replacing, a similar solution can be adapted.
@grytrn I just released a forked on my github to fix the problem. It's a problem of buffer size. you can fork it and let me know if it solve your issue. I may clean an make a PR here https://github.com/anisayari/claude-code-sdk-python
fixed with latest release
@ltawfik It still have problem. When I use #34 version, it didn't happen. Please revisit this issue. Thank you!
I'm also still seeing:
claude_code_sdk._errors.CLIJSONDecodeError: Failed to decode JSON: {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_014toiTa1QVbQBjcCfSocCim","...
Fwiw I'm also getting this using the fix mentioned in https://github.com/anthropics/claude-code-sdk-python/pull/34 and the fork mentioned above inhttps://github.com/anisayari/claude-code-sdk-python
me too, still getting this issue even after those fixes:
| raise SDKJSONDecodeError(json_line, e) from e
| claude_code_sdk._errors.CLIJSONDecodeError: Failed to decode JSON: {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_01Shrz4GuFGhLCewAmhUfFtP","...```