Skip to content

Fix ImportError: SessionEventType not exported from copilot public API#1093

Merged
aaronpowell merged 2 commits intostagedfrom
copilot/fix-snippet-example-importerror
Mar 19, 2026
Merged

Fix ImportError: SessionEventType not exported from copilot public API#1093
aaronpowell merged 2 commits intostagedfrom
copilot/fix-snippet-example-importerror

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

SessionEventType is not exported from the top-level copilot module (it lives in the internal copilot.generated.session_events module), causing ImportError when running any of the affected Python cookbook recipes.

Changes

  • 3 recipe files (managing_local_files.py, accessibility_report.py, pr_visualization.py): Remove SessionEventType from imports; replace enum comparisons with event.type.value == "..." string comparisons — the pattern used in the official SDK README
  • 3 matching docs (managing-local-files.md, accessibility-report.md, pr-visualization.md): Sync inline code snippets to match the fixed recipes

Before:

from copilot import (
    CopilotClient, SessionConfig, MessageOptions,
    SessionEvent, SessionEventType,  # ← ImportError
)

def handle_event(event: SessionEvent):
    if event.type == SessionEventType.ASSISTANT_MESSAGE:
        ...

After:

from copilot import (
    CopilotClient, SessionConfig, MessageOptions,
    SessionEvent,
)

def handle_event(event: SessionEvent):
    if event.type.value == "assistant.message":
        ...

Pull Request Checklist

  • I have read and followed the CONTRIBUTING.md guidelines.
  • I have read and followed the Guidance for submissions involving paid services.
  • My contribution adds a new instruction, prompt, agent, skill, or workflow file in the correct directory.
  • The file follows the required naming convention.
  • The content is clearly structured and follows the example format.
  • I have tested my instructions, prompt, agent, skill, or workflow with GitHub Copilot.
  • I have run npm start and verified that README.md is up to date.

Description

SessionEventType is not part of the public copilot package API, causing ImportError for all Python cookbook recipes that import it. Replace with event.type.value string comparisons, consistent with the SDK's own documentation.


Type of Contribution

  • New instruction file.
  • New prompt file.
  • New agent file.
  • New plugin.
  • New skill file.
  • New agentic workflow.
  • Update to existing instruction, prompt, agent, plugin, skill, or workflow.
  • Other (please specify):

Additional Notes

SessionEventType exists at copilot.generated.session_events.SessionEventType but is intentionally not re-exported at the package level. The SDK README consistently uses event.type.value == "..." string comparisons, so this fix aligns the cookbook with the documented pattern rather than reaching into internal modules.


By submitting this pull request, I confirm that my contribution abides by the Code of Conduct and will be licensed under the MIT License.


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

…ring comparisons

Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Agent-Logs-Url: https://github.com/github/awesome-copilot/sessions/0c514b95-5157-4aa9-8590-cbf3fd337472
Copilot AI changed the title [WIP] Fix snippet example ImportError in managing local files Fix ImportError: SessionEventType not exported from copilot public API Mar 19, 2026
Copilot AI requested a review from aaronpowell March 19, 2026 22:58
@aaronpowell aaronpowell marked this pull request as ready for review March 19, 2026 23:50
Copilot AI review requested due to automatic review settings March 19, 2026 23:50
@aaronpowell aaronpowell merged commit 6fbbc52 into staged Mar 19, 2026
9 checks passed
@aaronpowell aaronpowell deleted the copilot/fix-snippet-example-importerror branch March 19, 2026 23:51
Copy link
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 fixes Python cookbook recipes that currently raise ImportError by importing SessionEventType from the top-level copilot module, switching the event handling logic to use string comparisons via event.type.value instead.

Changes:

  • Remove SessionEventType imports from 3 runnable Python recipes and update event-type checks to event.type.value == "...".
  • Update the corresponding 3 markdown recipe docs so inline code snippets match the runnable recipes.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
cookbook/copilot-sdk/python/recipe/pr_visualization.py Drops SessionEventType import and compares event.type.value against event name strings.
cookbook/copilot-sdk/python/recipe/managing_local_files.py Same fix: remove SessionEventType import; switch enum comparisons to event.type.value strings.
cookbook/copilot-sdk/python/recipe/accessibility_report.py Same fix for streaming delta events; keeps event handling consistent with other recipes.
cookbook/copilot-sdk/python/pr-visualization.md Syncs doc snippet imports and event checks with the updated runnable recipe.
cookbook/copilot-sdk/python/managing-local-files.md Syncs doc snippet imports and event checks with the updated runnable recipe.
cookbook/copilot-sdk/python/accessibility-report.md Syncs doc snippet imports and event checks with the updated runnable recipe.
Comments suppressed due to low confidence (1)

cookbook/copilot-sdk/python/accessibility-report.md:88

  • This snippet now uses event.type.value == "assistant.message_delta", but the later "How it works" section still says it uses ASSISTANT_MESSAGE_DELTA events. Please update that narrative to match the new string-based event type approach (e.g., refer to assistant.message_delta / event.type.value).
        if event.type.value == "assistant.message_delta":
            print(event.data.delta_content or "", end="", flush=True)
        elif event.type.value == "session.idle":
            done.set()
        elif event.type.value == "session.error":
            print(f"\nError: {event.data.message}")

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.

Snippet example broken: ImportError: cannot import name 'SessionEventType' from 'copilot'. Did you mean: 'SessionEvent'?

3 participants