Skip to content

CLIJSONDecodeError from receive_messages: JSONDecodeError "Extra data" due to multiple JSON objects in one lineΒ #15

@ujitkumar1

Description

@ujitkumar1

I'm encountering a recurring issue when using the claude_code_sdk. The following exception is thrown most of the time during execution:

  + Exception Group Traceback (most recent call last):
  |   File "/claude-testing/workflow.py", line 25, in <module>
  |     anyio.run(query_with_options)
  |   File "/.venv/lib/python3.11/site-packages/anyio/_core/_eventloop.py", line 74, in run
  |     return async_backend.run(func, args, {}, backend_options)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2310, in run
  |     return runner.run(wrapper())
  |            ^^^^^^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
  |     return self._loop.run_until_complete(task)
  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
  |     return future.result()
  |            ^^^^^^^^^^^^^^^
  |   File "/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 2298, in wrapper
  |     return await func(*args)
  |            ^^^^^^^^^^^^^^^^^
  |   File "/workflow.py", line 15, in query_with_options
  |     async for message in query(
  |   File "/.venv/lib/python3.11/site-packages/claude_code_sdk/__init__.py", line 101, in query
  |     async for message in client.process_query(prompt=prompt, options=options):
  |   File "/.venv/lib/python3.11/site-packages/claude_code_sdk/_internal/client.py", line 37, in process_query
  |     async for data in transport.receive_messages():
  |   File "/.venv/lib/python3.11/site-packages/claude_code_sdk/_internal/transport/subprocess_cli.py", line 182, in receive_messages
  |     async with anyio.create_task_group() as tg:
  |   File "/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
  |     raise BaseExceptionGroup(
  | ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "/.venv/lib/python3.11/site-packages/claude_code_sdk/_internal/transport/subprocess_cli.py", line 192, in receive_messages
    |     data = json.loads(line_str)
    |            ^^^^^^^^^^^^^^^^^^^^
    |   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 346, in loads
    |     return _default_decoder.decode(s)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/decoder.py", line 340, in decode
    |     raise JSONDecodeError("Extra data", s, end)
    | json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 546)
    | 
    | The above exception was the direct cause of the following exception:
    | 
    | Traceback (most recent call last):
    |   File "/.venv/lib/python3.11/site-packages/claude_code_sdk/_internal/transport/subprocess_cli.py", line 196, in receive_messages
    |     raise SDKJSONDecodeError(line_str, e) from e
    | claude_code_sdk._errors.CLIJSONDecodeError: Failed to decode JSON: {"type":"assistant","message":{"id":"msg_016WUwP1zUZvB8wcgSguHGBi","type":"message","role":"assistan...
    +------------------------------------

It appears that the issue is caused by multiple JSON objects being emitted in a single line or without proper separation, leading to a json.decoder.JSONDecodeError: Extra data.

ENV:
claude_code_sdk version: 0.0.10
Python version: 3.11
OS: macOS

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions