Files
awesome-copilot/cookbook/copilot-sdk/python/persisting-sessions.md
Anthony Shaw c65e8ab0b5 Fix Python cookbook recipes to use correct async SDK API
All 5 Python recipes and their markdown docs used a synchronous,
kwargs-based API that doesn't match the real github-copilot-sdk:

- client.start() -> await client.start() (all methods are async)
- create_session(model=...) -> create_session(SessionConfig(model=...))
- session.send(prompt=...) -> session.send(MessageOptions(prompt=...))
- session.wait_for_idle() -> session.send_and_wait() (wait_for_idle doesn't exist)
- event['type']/event['data']['content'] -> event.type/event.data.content
- All code wrapped in async def main() + asyncio.run(main())

Verified all imports resolve against github-copilot-sdk.
2026-02-11 06:19:33 -08:00

2.1 KiB

Session Persistence and Resumption

Save and restore conversation sessions across application restarts.

Example scenario

You want users to be able to continue a conversation even after closing and reopening your application.

Runnable example: recipe/persisting_sessions.py

cd recipe && pip install -r requirements.txt
python persisting_sessions.py

Creating a session with a custom ID

import asyncio
from copilot import CopilotClient, SessionConfig, MessageOptions

async def main():
    client = CopilotClient()
    await client.start()

    # Create session with a memorable ID
    session = await client.create_session(SessionConfig(
        session_id="user-123-conversation",
        model="gpt-5",
    ))

    await session.send_and_wait(MessageOptions(prompt="Let's discuss TypeScript generics"))

    # Session ID is preserved
    print(session.session_id)  # "user-123-conversation"

    # Destroy session but keep data on disk
    await session.destroy()
    await client.stop()

if __name__ == "__main__":
    asyncio.run(main())

Resuming a session

client = CopilotClient()
await client.start()

# Resume the previous session
session = await client.resume_session("user-123-conversation")

# Previous context is restored
await session.send_and_wait(MessageOptions(prompt="What were we discussing?"))

await session.destroy()
await client.stop()

Listing available sessions

sessions = await client.list_sessions()
for s in sessions:
    print("Session:", s.session_id)

Deleting a session permanently

# Remove session and all its data from disk
await client.delete_session("user-123-conversation")

Getting session history

messages = await session.get_messages()
for msg in messages:
    print(f"[{msg.type}] {msg.data.content}")

Best practices

  1. Use meaningful session IDs: Include user ID or context in the session ID
  2. Handle missing sessions: Check if a session exists before resuming
  3. Clean up old sessions: Periodically delete sessions that are no longer needed