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
Bug Description
The SDK fails with
JSONDecodeError: Unterminated stringwhen Claude generates very long responses. This occurs because the subprocess pipe buffer truncates large JSON messages.Reproduction Steps
Root Cause
The issue is in
subprocess_cli.pywhereanyio.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
Proposed Solution
Increase the subprocess buffer size when creating the process:
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
CLIJSONDecodeErrorfrom receive_messages: JSONDecodeError "Extra data" due to multiple JSON objects in one line #15 (CLIJSONDecodeError with multiple JSON objects)SubprocessCLITransport.receive_messages#6 (JSON parsing issues)