diff --git a/src/claude_code_sdk/_internal/transport/subprocess_cli.py b/src/claude_code_sdk/_internal/transport/subprocess_cli.py index 9749384..3ab7803 100644 --- a/src/claude_code_sdk/_internal/transport/subprocess_cli.py +++ b/src/claude_code_sdk/_internal/transport/subprocess_cli.py @@ -18,6 +18,8 @@ from ...types import ClaudeCodeOptions from . import Transport +_MAX_BUFFER_SIZE = 1 * 1024 * 1024 # 1MB + class SubprocessCLITransport(Transport): """Subprocess transport using Claude Code CLI.""" @@ -197,9 +199,13 @@ class SubprocessCLITransport(Transport): json_line = json_line.strip() if not json_line: continue - + + json_buffer += json_line + if(len(json_buffer.encode(self._stdout_stream.encoding)) > _MAX_BUFFER_SIZE): + raise OverflowError(f"JSON message buffer exceeded max size: " + f"{_MAX_BUFFER_SIZE/(1024*1024)} MB") + try: - json_buffer += json_line data = json.loads(json_buffer) json_buffer = "" try: diff --git a/tests/test_subprocess_buffering.py b/tests/test_subprocess_buffering.py index 80da428..1333789 100644 --- a/tests/test_subprocess_buffering.py +++ b/tests/test_subprocess_buffering.py @@ -17,6 +17,7 @@ class MockTextReceiveStream: def __init__(self, lines: list[str]) -> None: self.lines = lines self.index = 0 + self.encoding = "utf-8" def __aiter__(self) -> AsyncIterator[str]: return self @@ -140,7 +141,7 @@ class TestSubprocessBuffering: anyio.run(_test) - def test_object_over_buffer_limit(self) -> None: + def test_objects_over_buffer_limit(self) -> None: """Test parsing with a json object larger than the buffer limit.""" async def _test() -> None: