Skip to content

Generate dedicated Python session event types#1063

Merged
stephentoub merged 14 commits intomainfrom
stephentoub/align-python-event-generation
Apr 14, 2026
Merged

Generate dedicated Python session event types#1063
stephentoub merged 14 commits intomainfrom
stephentoub/align-python-event-generation

Conversation

@stephentoub
Copy link
Copy Markdown
Collaborator

Summary

Python session events still used quicktype and collapsed payload members into one merged Data model, unlike the newer Go implementation. That made the language bindings inconsistent and increased the chance of naming conflicts as the runtime schema grows.

This change switches Python session-event generation to dedicated per-event payload dataclasses, matching the newer Go-style approach. SessionEvent.from_dict() now dispatches to typed payload classes, unknown events still round-trip through RawSessionEventData, and the Python runtime/tests were updated to handle the typed payload union.

The one non-obvious part is compatibility: the generator also restores legacy top-level helper exports and keeps arbitrary nested mappings in the Data shim as plain dicts so older callers do not break while the generated model becomes more structured.

Validation

  • python -m pytest test_event_forward_compatibility.py test_commands_and_elicitation.py
  • python -m pytest --ignore=e2e

@stephentoub stephentoub requested a review from a team as a code owner April 11, 2026 18:51
Copilot AI review requested due to automatic review settings April 11, 2026 18:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Python SDK’s session-events generation to emit dedicated per-event payload dataclasses (instead of a single merged Data payload), and updates the Python runtime/tests to consume these typed payloads while retaining compatibility shims.

Changes:

  • Replaced Python session-events quicktype generation with a custom generator that emits per-event payload dataclasses and dispatches in SessionEvent.from_dict().
  • Updated Python session handling and tests to use typed payload classes (e.g., CommandExecuteData, ElicitationRequestedData) and validate legacy top-level exports + Data shim behavior.
  • Preserved forward compatibility for unknown event types via SessionEventType.UNKNOWN + RawSessionEventData, plus added several legacy helper aliases.
Show a summary per file
File Description
scripts/codegen/python.ts Implements custom Python session-events codegen emitting per-event payload dataclasses, compatibility shims, and typed dispatch.
python/test_event_forward_compatibility.py Extends forward-compat tests to cover legacy helper exports and Data shim dict-preservation behavior.
python/test_commands_and_elicitation.py Updates tests to construct typed event payloads instead of using the legacy Data container.
python/copilot/session.py Updates broadcast event handling to cast event.data to the appropriate generated payload dataclass before accessing fields.

Copilot's findings

  • Files reviewed: 4/5 changed files
  • Comments generated: 2

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@stephentoub stephentoub force-pushed the stephentoub/align-python-event-generation branch from 723b9d2 to 0b80129 Compare April 13, 2026 02:46
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated by SDK Consistency Review Agent for issue #1063 · ● 488.3K

Copy link
Copy Markdown
Contributor

@SteveSandersonMS SteveSandersonMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good to me, would be interesting to run it by @brettcannon if you want more authoritative Python feedback.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

stephentoub and others added 10 commits April 14, 2026 08:28
Align Python session event generation with the newer Go-style dedicated per-event payload model instead of the old merged quicktype Data shape. This updates the runtime/tests for typed payloads while preserving compatibility aliases and legacy Data behavior for existing callers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Fix the generated Python docstring escaping that CodeQL flagged, correct dotted-key normalization in the Data compatibility shim, update the stale Go local-cli docs snippet for the newer typed event API, and apply the Python formatter change required by CI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
stephentoub and others added 2 commits April 14, 2026 08:34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@stephentoub stephentoub force-pushed the stephentoub/align-python-event-generation branch from cb1499d to 698dc0d Compare April 14, 2026 13:23
@github-actions

This comment has been minimized.

Normalize trailing whitespace in pyDocstringLiteral to prevent
cross-platform codegen drift, and reformat test file with Prettier.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generated by SDK Consistency Review Agent for issue #1063 · ● 687K

- Regenerate session_events.py to match latest schema (removes
  session.import_legacy, adds reasoning_tokens to AssistantUsageData)
- Update docs/setup/local-cli.md Python example to use match-based
  type narrowing with None check, consistent with python/README.md

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Cross-SDK Consistency Review ✅

This PR improves cross-SDK consistency by aligning Python's session event model with Go's approach.

Summary of findings:

SDK Event dispatch pattern sendAndWait return type
Python (this PR) match event.data: case AssistantMessageData() SessionEvent | None
Go switch event.Data.(type) / type assertion *SessionEvent
.NET event is AssistantMessageEvent (polymorphic JSON) AssistantMessageEvent?
TypeScript event.type === "assistant.message" (discriminated union) AssistantMessageEvent | undefined

What this PR does well:

  • ✅ Python now generates dedicated per-event payload dataclasses (AssistantMessageData, SessionIdleData, etc.), matching Go's approach of strongly-typed event data
  • ✅ The match event.data: case XxxData() pattern is idiomatic Python and semantically parallel to Go's type switch
  • ✅ Forward-compatibility is preserved via RawSessionEventData for unknown event types (parallel to Go's RawSessionEventData)
  • ✅ Docs updated consistently across language examples

Pre-existing inconsistency (not introduced by this PR):
Node.js and .NET's sendAndWait/SendAndWaitAsync return a pre-narrowed AssistantMessageEvent type directly (so callers can access .data.content without further narrowing), while Python and Go return the base SessionEvent type requiring an additional type check. This difference predates this PR and is not in scope here.

No action needed from a cross-SDK consistency perspective — this PR is a net improvement to consistency.

Generated by SDK Consistency Review Agent for issue #1063 · ● 807.8K ·

@stephentoub stephentoub added this pull request to the merge queue Apr 14, 2026
Merged via the queue into main with commit 6722743 Apr 14, 2026
30 checks passed
@stephentoub stephentoub deleted the stephentoub/align-python-event-generation branch April 14, 2026 15:30
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.

5 participants