Problem
The first example shown in the Python SDK README does not compile with the latest version of the SDK, which is 0.2.2 at time of writing.
|
## Quick Start |
|
|
|
```python |
|
import asyncio |
|
|
|
from copilot import CopilotClient |
|
from copilot.generated.session_events import AssistantMessageData, SessionIdleData |
|
|
|
async def main(): |
|
# Client automatically starts on enter and cleans up on exit |
|
async with CopilotClient() as client: |
|
# Create a session with automatic cleanup |
|
async with await client.create_session(model="gpt-5") as session: |
|
# Wait for response using session.idle event |
|
done = asyncio.Event() |
|
|
|
def on_event(event): |
|
match event.data: |
|
case AssistantMessageData() as data: |
|
print(data.content) |
|
case SessionIdleData(): |
|
done.set() |
|
|
|
session.on(on_event) |
|
|
|
# Send a message and wait for completion |
|
await session.send("What is 2+2?") |
|
await done.wait() |
|
|
|
asyncio.run(main()) |
|
``` |
To spur on better documentation 🙃, I thought I would point out that Anthropic has nice published Python documentation for their Claude Agent SDK: https://code.claude.com/docs/en/agent-sdk/python.
Working version
Here is a working version. Note that it does use the approve all permission handler.
import asyncio
from copilot import CopilotClient
from copilot.generated.session_events import SessionEventType
from copilot.session import PermissionHandler
async def main():
# Client automatically starts on enter and cleans up on exit
async with CopilotClient() as client:
# Create a session with automatic cleanup
async with await client.create_session(
on_permission_request=PermissionHandler.approve_all,
model="gpt-5.4",
) as session:
# Wait for response using session.idle event
done = asyncio.Event()
def on_event(event):
match event.type:
case SessionEventType.ASSISTANT_MESSAGE:
print(event.data.content)
case SessionEventType.SESSION_IDLE:
done.set()
session.on(on_event)
# Send a message and wait for completion
await session.send("What is 2+2?")
await done.wait()
if __name__ == "__main__":
asyncio.run(main())
Problem
The first example shown in the Python SDK README does not compile with the latest version of the SDK, which is
0.2.2at time of writing.copilot-sdk/python/README.md
Lines 24 to 54 in 06e4964
To spur on better documentation 🙃, I thought I would point out that Anthropic has nice published Python documentation for their Claude Agent SDK: https://code.claude.com/docs/en/agent-sdk/python.
Working version
Here is a working version. Note that it does use the approve all permission handler.