2424
2525logger = logging .getLogger (__name__ )
2626
27- _MAX_BUFFER_SIZE = 1024 * 1024 # 1MB buffer limit
27+ _DEFAULT_MAX_BUFFER_SIZE = 1024 * 1024 # 1MB buffer limit
2828
2929
3030class SubprocessCLITransport (Transport ):
@@ -48,6 +48,11 @@ def __init__(
4848 self ._stderr_task_group : anyio .abc .TaskGroup | None = None
4949 self ._ready = False
5050 self ._exit_error : Exception | None = None # Track process exit errors
51+ self ._max_buffer_size = (
52+ options .max_buffer_size
53+ if options .max_buffer_size is not None
54+ else _DEFAULT_MAX_BUFFER_SIZE
55+ )
5156
5257 def _find_cli (self ) -> str :
5358 """Find Claude Code CLI binary."""
@@ -402,12 +407,13 @@ async def _read_messages_impl(self) -> AsyncIterator[dict[str, Any]]:
402407 # Keep accumulating partial JSON until we can parse it
403408 json_buffer += json_line
404409
405- if len (json_buffer ) > _MAX_BUFFER_SIZE :
410+ if len (json_buffer ) > self ._max_buffer_size :
411+ buffer_length = len (json_buffer )
406412 json_buffer = ""
407413 raise SDKJSONDecodeError (
408- f"JSON message exceeded maximum buffer size of { _MAX_BUFFER_SIZE } bytes" ,
414+ f"JSON message exceeded maximum buffer size of { self . _max_buffer_size } bytes" ,
409415 ValueError (
410- f"Buffer size { len ( json_buffer ) } exceeds limit { _MAX_BUFFER_SIZE } "
416+ f"Buffer size { buffer_length } exceeds limit { self . _max_buffer_size } "
411417 ),
412418 )
413419
@@ -418,7 +424,7 @@ async def _read_messages_impl(self) -> AsyncIterator[dict[str, Any]]:
418424 except json .JSONDecodeError :
419425 # We are speculatively decoding the buffer until we get
420426 # a full JSON object. If there is an actual issue, we
421- # raise an error after _MAX_BUFFER_SIZE .
427+ # raise an error after exceeding the configured limit .
422428 continue
423429
424430 except anyio .ClosedResourceError :
0 commit comments