claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

JSONDecodeError: Subprocess buffer truncates large messages

Open grytrn opened this issue 1 year ago • 6 comments

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

  1. 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__}")
  1. 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)

grytrn avatar Jun 21 '25 20:06 grytrn

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.

baijum avatar Jun 25 '25 19:06 baijum

@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

anisayari avatar Jun 26 '25 01:06 anisayari

fixed with latest release

ltawfik avatar Jun 28 '25 01:06 ltawfik

Image

@ltawfik It still have problem. When I use #34 version, it didn't happen. Please revisit this issue. Thank you!

tim-watcha avatar Jun 29 '25 07:06 tim-watcha

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

tha23rd avatar Jun 30 '25 15:06 tha23rd

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","...```

amitok2 avatar Jun 30 '25 17:06 amitok2