Skip to content

ProcessError loses real CLI error message during initialize() #641

@chehov

Description

@chehov

Problem

When the CLI process fails during initialize() (e.g., resuming a non-existent session), the SDK correctly parses the JSON error result from stdout and sends it to the message stream. However, it then raises a ProcessError with a hardcoded stderr="Check stderr output for details", which overwrites the real error.

The caller receives a useless ProcessError instead of the actual CLI error (e.g., "No conversation found with session ID ab2c985b").

Root Cause

In query.py:_read_messages:

  1. Line 210-214: A result message with subtype=error_during_execution arrives from the transport, gets sent to the message stream
  2. Line 578-583 (subprocess_cli.py): The transport sees non-zero exit code and raises ProcessError with hardcoded stderr="Check stderr output for details"
  3. Line 220-226 (query.py): The ProcessError is caught and propagated to pending control requests — replacing the real error that's sitting unread in the message stream

During initialize(), the caller is waiting on _send_control_request which listens for control responses, not the message stream. So the real error is buffered but never reaches the caller.

Expected Behavior

The actual CLI error text from the error_during_execution result should be included in the exception that reaches the caller.

Possible Fix

In _read_messages, when a result with subtype=error_during_execution is received, store the error text. Then in the except block (line 220-226), use the stored error text instead of (or in addition to) the ProcessError message when signaling pending control requests.

SDK Version

claude-agent-sdk 0.1.47

Metadata

Metadata

Assignees

No one assigned

    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