Commit 5656d20
authored
feat: add session_id option to ClaudeAgentOptions (#750)
## Summary
Adds the `session_id` option to `ClaudeAgentOptions`, allowing users to
specify a custom session ID (UUID) rather than having one
auto-generated. This brings the Python SDK to parity with the TypeScript
SDK's `sessionId` option and the CLI's `--session-id` flag.
Fixes #338
## Problem
Users who want to control session IDs (e.g., for batch processing,
session tracking, or integration with external systems) currently have
to extract the auto-generated ID from the first `SystemMessage`:
```python
session_id = None
async for msg in query(prompt="..."):
if hasattr(msg, "session_id"):
session_id = msg.session_id # awkward extraction
...
```
The CLI supports `--session-id <uuid>` and the TypeScript SDK exposes
`sessionId` in options, but the Python SDK had no equivalent.
## Fix
- **`types.py`**: Add `session_id: str | None = None` to
`ClaudeAgentOptions`
- **`subprocess_cli.py`**: Pass `--session-id` to the CLI when
`session_id` is set
```python
options = ClaudeAgentOptions(
session_id="550e8400-e29b-41d4-a716-446655440000",
)
async for msg in query(prompt="...", options=options):
# msg.session_id == "550e8400-e29b-41d4-a716-446655440000"
```
## Verification
**End-to-end with live SDK instance:**
```
Using custom session_id: f5e728c6-a604-48e2-8728-a08bde1f3c9e
SystemMessage: session_id=N/A
AssistantMessage: session_id=f5e728c6-a604-48e2-8728-a08bde1f3c9e
AssistantMessage: session_id=f5e728c6-a604-48e2-8728-a08bde1f3c9e
RateLimitEvent: session_id=f5e728c6-a604-48e2-8728-a08bde1f3c9e
ResultMessage: session_id=f5e728c6-a604-48e2-8728-a08bde1f3c9e
Expected session_id: f5e728c6-a604-48e2-8728-a08bde1f3c9e
Actual session_id: f5e728c6-a604-48e2-8728-a08bde1f3c9e
PASS: Session ID matches custom ID!
```
**Test suite:**
- 404 tests pass
- 2 unit tests added (`test_session_id`,
`test_session_id_not_set_by_default`)
- `ruff check` + `ruff format` clean
- `mypy src/` clean1 parent 278570d commit 5656d20
File tree
3 files changed
+26
-0
lines changed- src/claude_agent_sdk
- _internal/transport
- tests
3 files changed
+26
-0
lines changedLines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
234 | 237 | | |
235 | 238 | | |
236 | 239 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1088 | 1088 | | |
1089 | 1089 | | |
1090 | 1090 | | |
| 1091 | + | |
1091 | 1092 | | |
1092 | 1093 | | |
1093 | 1094 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
268 | 268 | | |
269 | 269 | | |
270 | 270 | | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
271 | 293 | | |
272 | 294 | | |
273 | 295 | | |
| |||
0 commit comments