Skip to main content

Python SDK for GitHub Copilot CLI

Project description

Copilot Python SDK

Python SDK for programmatic control of GitHub Copilot CLI via JSON-RPC.

Note: This SDK is in technical preview and may change in breaking ways.

Installation

pip install -e ".[dev]"
# or
uv pip install -e ".[dev]"

Quick Start

import asyncio
from copilot import CopilotClient

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

    # Create a session
    session = await client.create_session({"model": "gpt-5"})

    # Wait for response using session.idle event
    done = asyncio.Event()

    def on_event(event):
        if event.type.value == "assistant.message":
            print(event.data.content)
        elif event.type.value == "session.idle":
            done.set()

    session.on(on_event)

    # Send a message and wait for completion
    await session.send({"prompt": "What is 2+2?"})
    await done.wait()

    # Clean up
    await session.destroy()
    await client.stop()

asyncio.run(main())

Features

  • ✅ Full JSON-RPC protocol support
  • ✅ stdio and TCP transports
  • ✅ Real-time streaming events
  • ✅ Session history with get_messages()
  • ✅ Type hints throughout
  • ✅ Async/await native

API Reference

CopilotClient

client = CopilotClient({
    "cli_path": "copilot",  # Optional: path to CLI executable
    "cli_url": None,        # Optional: URL of existing server (e.g., "localhost:8080")
    "log_level": "info",    # Optional: log level (default: "info")
    "auto_start": True,     # Optional: auto-start server (default: True)
    "auto_restart": True,   # Optional: auto-restart on crash (default: True)
})
await client.start()

session = await client.create_session({"model": "gpt-5"})

def on_event(event):
    print(f"Event: {event['type']}")

session.on(on_event)
await session.send({"prompt": "Hello!"})

# ... wait for events ...

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

CopilotClient Options:

  • cli_path (str): Path to CLI executable (default: "copilot" or COPILOT_CLI_PATH env var)
  • cli_url (str): URL of existing CLI server (e.g., "localhost:8080", "http://127.0.0.1:9000", or just "8080"). When provided, the client will not spawn a CLI process.
  • cwd (str): Working directory for CLI process
  • port (int): Server port for TCP mode (default: 0 for random)
  • use_stdio (bool): Use stdio transport instead of TCP (default: True)
  • log_level (str): Log level (default: "info")
  • auto_start (bool): Auto-start server on first use (default: True)
  • auto_restart (bool): Auto-restart on crash (default: True)

Tools

Define tools with automatic JSON schema generation using the @define_tool decorator and Pydantic models:

from pydantic import BaseModel, Field
from copilot import CopilotClient, define_tool

class LookupIssueParams(BaseModel):
    id: str = Field(description="Issue identifier")

@define_tool(description="Fetch issue details from our tracker")
async def lookup_issue(params: LookupIssueParams) -> str:
    issue = await fetch_issue(params.id)
    return issue.summary

session = await client.create_session({
    "model": "gpt-5",
    "tools": [lookup_issue],
})

Note: When using from __future__ import annotations, define Pydantic models at module level (not inside functions).

Low-level API (without Pydantic):

For users who prefer manual schema definition:

from copilot import CopilotClient, Tool

async def lookup_issue(invocation):
    issue_id = invocation["arguments"]["id"]
    issue = await fetch_issue(issue_id)
    return {
        "textResultForLlm": issue.summary,
        "resultType": "success",
        "sessionLog": f"Fetched issue {issue_id}",
    }

session = await client.create_session({
    "model": "gpt-5",
    "tools": [
        Tool(
            name="lookup_issue",
            description="Fetch issue details from our tracker",
            parameters={
                "type": "object",
                "properties": {
                    "id": {"type": "string", "description": "Issue identifier"},
                },
                "required": ["id"],
            },
            handler=lookup_issue,
        )
    ],
})

The SDK automatically handles tool.call, executes your handler (sync or async), and responds with the final result when the tool completes.

Image Support

The SDK supports image attachments via the attachments parameter. You can attach images by providing their file path:

await session.send({
    "prompt": "What's in this image?",
    "attachments": [
        {
            "type": "file",
            "path": "/path/to/image.jpg",
        }
    ]
})

Supported image formats include JPG, PNG, GIF, and other common image types. The agent's view tool can also read images directly from the filesystem, so you can also ask questions like:

await session.send({"prompt": "What does the most recent jpg in this directory portray?"})

Streaming

Enable streaming to receive assistant response chunks as they're generated:

import asyncio
from copilot import CopilotClient

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

    session = await client.create_session({
        "model": "gpt-5",
        "streaming": True
    })

    # Use asyncio.Event to wait for completion
    done = asyncio.Event()

    def on_event(event):
        if event.type.value == "assistant.message_delta":
            # Streaming message chunk - print incrementally
            delta = event.data.delta_content or ""
            print(delta, end="", flush=True)
        elif event.type.value == "assistant.reasoning_delta":
            # Streaming reasoning chunk (if model supports reasoning)
            delta = event.data.delta_content or ""
            print(delta, end="", flush=True)
        elif event.type.value == "assistant.message":
            # Final message - complete content
            print("\n--- Final message ---")
            print(event.data.content)
        elif event.type.value == "assistant.reasoning":
            # Final reasoning content (if model supports reasoning)
            print("--- Reasoning ---")
            print(event.data.content)
        elif event.type.value == "session.idle":
            # Session finished processing
            done.set()

    session.on(on_event)
    await session.send({"prompt": "Tell me a short story"})
    await done.wait()  # Wait for streaming to complete

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

asyncio.run(main())

When streaming=True:

  • assistant.message_delta events are sent with delta_content containing incremental text
  • assistant.reasoning_delta events are sent with delta_content for reasoning/chain-of-thought (model-dependent)
  • Accumulate delta_content values to build the full response progressively
  • The final assistant.message and assistant.reasoning events contain the complete content

Note: assistant.message and assistant.reasoning (final events) are always sent regardless of streaming setting.

Infinite Sessions

By default, sessions use infinite sessions which automatically manage context window limits through background compaction and persist state to a workspace directory.

# Default: infinite sessions enabled with default thresholds
session = await client.create_session({"model": "gpt-5"})

# Access the workspace path for checkpoints and files
print(session.workspace_path)
# => ~/.copilot/session-state/{session_id}/

# Custom thresholds
session = await client.create_session({
    "model": "gpt-5",
    "infinite_sessions": {
        "enabled": True,
        "background_compaction_threshold": 0.80,  # Start compacting at 80% context usage
        "buffer_exhaustion_threshold": 0.95,  # Block at 95% until compaction completes
    },
})

# Disable infinite sessions
session = await client.create_session({
    "model": "gpt-5",
    "infinite_sessions": {"enabled": False},
})

When enabled, sessions emit compaction events:

  • session.compaction_start - Background compaction started
  • session.compaction_complete - Compaction finished (includes token counts)

Requirements

  • Python 3.9+
  • GitHub Copilot CLI installed and accessible

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

github_copilot_sdk-0.1.19.tar.gz (81.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

github_copilot_sdk-0.1.19-py3-none-any.whl (34.2 kB view details)

Uploaded Python 3

File details

Details for the file github_copilot_sdk-0.1.19.tar.gz.

File metadata

  • Download URL: github_copilot_sdk-0.1.19.tar.gz
  • Upload date:
  • Size: 81.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for github_copilot_sdk-0.1.19.tar.gz
Algorithm Hash digest
SHA256 2bba9db1ee0b3b6ff751568489777224e381c15427e253f98731c01a936833c2
MD5 b9912592771ea42c57f5704d895b14cf
BLAKE2b-256 a3e787955115ae99668a7dc1d1314c2ac07a7d5e8d621c52ce322090616d342e

See more details on using hashes here.

Provenance

The following attestation bundles were made for github_copilot_sdk-0.1.19.tar.gz:

Publisher: publish.yml on github/copilot-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file github_copilot_sdk-0.1.19-py3-none-any.whl.

File metadata

File hashes

Hashes for github_copilot_sdk-0.1.19-py3-none-any.whl
Algorithm Hash digest
SHA256 98d2e6ce65b88b470756d0d2ced12714fd76d3394c245e29a42185e3a4e83d0b
MD5 5d34ff7c1cb990205cf4c5460301c83b
BLAKE2b-256 6e8df5b0ddab9b3c4e9d0a8b5233fd12543f07f2fb65ab593652826c1ff96359

See more details on using hashes here.

Provenance

The following attestation bundles were made for github_copilot_sdk-0.1.19-py3-none-any.whl:

Publisher: publish.yml on github/copilot-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page