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

CLIJSONDecodeError: Failed to decode JSON: JSON message exceeded maximum buffer size of 1048576 bytes

Open nsheff opened this issue 1 year ago • 5 comments

When using claude code from the CLI, I pass a PDF and it will summarize it just fine.

When doing the same thing using the Python SDK, it gives a JSON buffer error. Here's the code:

# Read PDF
async def main():
    messages: list[Message] = []
    
    async for message in query(
        prompt="Read the PDF document at 'y.pdf' and summarize its content",
        options=ClaudeCodeOptions(max_turns=3, allowed_tools=["Read"])
    ):
        messages.append(message)
    
    print(messages)

result = anyio.run(main)

result

Here's the error:

CLIJSONDecodeError: Failed to decode JSON: JSON message exceeded maximum buffer size of 1048576 bytes...

I can call other requests where I'm not asking it to Read a file without issue. I was unable to find any documentation if I'm missing something about reading PDFs from within Python.

nsheff avatar Jul 29 '25 17:07 nsheff

Yes, this is pretty annoying. Make it non usable for PDF workflows.

oneryalcin avatar Aug 25 '25 21:08 oneryalcin

I found the root cause of this issue. The buffer size is hardcoded to 1MB in the SDK:

File: src/claude_code_sdk/_internal/transport/subprocess_cli.py Line 25:

_MAX_BUFFER_SIZE = 1024 * 1024  # 1MB buffer limit

Error thrown at lines 324-331:

if len(json_buffer) > _MAX_BUFFER_SIZE:
    json_buffer = ""
    raise SDKJSONDecodeError(
        f"JSON message exceeded maximum buffer size of {_MAX_BUFFER_SIZE} bytes",
        ValueError(f"Buffer size {len(json_buffer)} exceeds limit {_MAX_BUFFER_SIZE}")
    )

The issue is that when PDFs are read, they're base64 encoded in the JSON response, which easily exceeds 1MB for even moderately sized PDFs.

Proposed Solutions:

  1. Make it configurable via environment variable (minimal change):
_MAX_BUFFER_SIZE = int(os.getenv("CLAUDE_SDK_MAX_BUFFER_SIZE", 1024 * 1024))
  1. Increase the default to something more reasonable like 10MB or 50MB

  2. Make it a parameter in ClaudeCodeOptions

This is blocking all PDF workflows. For now, users have to monkey-patch this value, but it would be much better if the SDK handled this properly.

Would you consider accepting a PR to make this configurable?

oneryalcin avatar Aug 25 '25 22:08 oneryalcin

CLAUDE_SDK_MAX_BUFFER_SIZE

where do u write this code? i use claude code sdk and i want to increase the size, so where should i define this parameter? beacuse the limit parameter it is in their sdk code, not mine

amitok2 avatar Sep 19 '25 12:09 amitok2

You can use ClaudeAgentOptions.max_buffer_size option introduced by @Mng-dev-ai (thank you!) to adjust this value.

dicksontsai avatar Sep 30 '25 21:09 dicksontsai

It would be nice if the error message told people they can change ClaudeAgentOptions.max_buffer_size.

Also, is there any reason not to set ClaudeAgentOptions.max_buffer_size to a large value? Should the default be increased?

tmaxwell-anthropic avatar Jan 10 '26 01:01 tmaxwell-anthropic