CLIJSONDecodeError: Failed to decode JSON: JSON message exceeded maximum buffer size of 1048576 bytes
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.
Yes, this is pretty annoying. Make it non usable for PDF workflows.
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:
- Make it configurable via environment variable (minimal change):
_MAX_BUFFER_SIZE = int(os.getenv("CLAUDE_SDK_MAX_BUFFER_SIZE", 1024 * 1024))
-
Increase the default to something more reasonable like 10MB or 50MB
-
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?
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
You can use ClaudeAgentOptions.max_buffer_size option introduced by @Mng-dev-ai (thank you!) to adjust this value.
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?