claude-code-sdk-python/examples/include_partial_messages.py
Dickson Tsai f550c21a7e
Some checks are pending
Lint / lint (push) Waiting to run
Test / test (3.10) (push) Waiting to run
Test / test (3.11) (push) Waiting to run
Test / test-e2e (3.13) (push) Blocked by required conditions
Test / test-examples (3.11) (push) Blocked by required conditions
Test / test-examples (3.12) (push) Blocked by required conditions
Test / test-examples (3.13) (push) Blocked by required conditions
Test / test (3.12) (push) Waiting to run
Test / test (3.13) (push) Waiting to run
Test / test-e2e (3.10) (push) Blocked by required conditions
Test / test-e2e (3.11) (push) Blocked by required conditions
Test / test-e2e (3.12) (push) Blocked by required conditions
Test / test-examples (3.10) (push) Blocked by required conditions
Add support for streaming partial messages via include_partial_messages option (#168)
## Summary
- Adds support for streaming partial messages through the
`include_partial_messages` option
- Introduces `StreamEvent` message type to handle Anthropic API stream
events
- Enables real-time streaming of Claude's responses for building
interactive UIs

## Changes
- Added `StreamEvent` dataclass with proper structure matching
TypeScript SDK (uuid, session_id, event, parent_tool_use_id)
- Added `include_partial_messages` boolean option to `ClaudeCodeOptions`
- Updated message parser to handle `stream_event` message type
- Updated subprocess CLI transport to pass `--include-partial-messages`
flag when enabled
- Added example demonstrating partial message streaming usage

## Test plan
- [x] Verified CLI flag is passed correctly when
`include_partial_messages=True`
- [x] Confirmed `StreamEvent` structure matches TypeScript SDK
implementation
- [x] Added test for user parameter in transport
- [x] Example runs successfully with streaming events

🤖 Generated with [Claude Code](https://claude.ai/code)

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-17 16:32:21 -07:00

62 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
Example of using the "include_partial_messages" option to stream partial messages
from Claude Code SDK.
This feature allows you to receive stream events that contain incremental
updates as Claude generates responses. This is useful for:
- Building real-time UIs that show text as it's being generated
- Monitoring tool use progress
- Getting early results before the full response is complete
Note: Partial message streaming requires the CLI to support it, and the
messages will include StreamEvent messages interspersed with regular messages.
"""
import asyncio
from claude_code_sdk import ClaudeSDKClient
from claude_code_sdk.types import (
ClaudeCodeOptions,
StreamEvent,
AssistantMessage,
UserMessage,
SystemMessage,
ResultMessage,
)
async def main():
# Enable partial message streaming
options = ClaudeCodeOptions(
include_partial_messages=True,
model="claude-sonnet-4-20250514",
max_turns=2,
env={
"MAX_THINKING_TOKENS": "8000",
},
)
client = ClaudeSDKClient(options)
try:
await client.connect()
# Send a prompt that will generate a streaming response
# prompt = "Run a bash command to sleep for 5 seconds"
prompt = "Think of three jokes, then tell one"
print(f"Prompt: {prompt}\n")
print("=" * 50)
await client.query(prompt)
async for message in client.receive_response():
print(message)
finally:
await client.disconnect()
if __name__ == "__main__":
print("Partial Message Streaming Example")
print("=" * 50)
asyncio.run(main())