Skip to content

fix: include hook lifecycle events in message stream when include_hook_events is enabled#1007

Open
Oxygen56 wants to merge 1 commit into
anthropics:mainfrom
Oxygen56:fix/include-hook-events-991
Open

fix: include hook lifecycle events in message stream when include_hook_events is enabled#1007
Oxygen56 wants to merge 1 commit into
anthropics:mainfrom
Oxygen56:fix/include-hook-events-991

Conversation

@Oxygen56
Copy link
Copy Markdown

Description

When include_hook_events=True in ClaudeAgentOptions, the --include-hook-events CLI flag was passed correctly and hook callbacks executed via the control protocol, but hook_started and hook_response system messages never appeared in the parsed message stream. Only init and status system messages were visible.

Root Cause

The Python SDK passed --include-hook-events as a CLI startup flag but did not send includeHookEvents: true in the initialize request payload over the control protocol. The CLI needs both signals to emit hook lifecycle events (hook_started, hook_progress, hook_response) to stdout in stream-json mode.

Additionally, the message parser only handled hook_started and hook_response subtypes but was missing hook_progress, which the TypeScript SDK supports as an intermediate lifecycle update.

Changes

  1. src/claude_agent_sdk/_internal/query.py: Added include_hook_events parameter to Query.__init__(). When True, sends includeHookEvents: true in the initialize request payload to the CLI.

  2. src/claude_agent_sdk/_internal/client.py: Passes include_hook_events from ClaudeAgentOptions through to the Query constructor.

  3. src/claude_agent_sdk/_internal/message_parser.py: Added hook_progress to the recognized system message subtypes alongside hook_started and hook_response, matching the TypeScript SDK's lifecycle support.

  4. src/claude_agent_sdk/types.py: Updated HookEventMessage docstring to document the hook_progress subtype.

  5. Tests: Added tests for hook_progress parsing and for includeHookEvents being properly included/omitted in the initialize request.

Fixes #991

…k_events is enabled

Bug: When include_hook_events=True in ClaudeAgentOptions, the
--include-hook-events CLI flag was passed correctly and hook callbacks
executed, but hook_started and hook_response system messages never
appeared in the parsed message stream.

Root cause: The CLI needs send includeHookEvents: true in the initialize
request (control protocol) to emit hook lifecycle messages to stdout.
The SDK only passed --include-hook-events as a CLI flag but never told
the CLI via the control protocol.

Changes:
- Add include_hook_events parameter to Query.__init__() and send it as
  includeHookEvents: true in the initialize request payload
- Pass include_hook_events from InternalClient to Query
- Add hook_progress to the recognized system message subtypes in the
  message parser (matching TypeScript SDK's hook_progress lifecycle)
- Update HookEventMessage docstring to document hook_progress
- Add tests for hook_progress parsing and includeHookEvents in initialize

Closes anthropics#991

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] --include-hook-events flag not emitting hook lifecycle events to message stream

1 participant