Skip to content

15 production-ready Python samples for GitHub Copilot SDK β€” streaming, tools, browser automation, code review, and more. All tested in CI with gpt-5-mini (free tier).

License

Notifications You must be signed in to change notification settings

Michspirit99/copilot-sdk-python

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Copilot SDK for Python β€” Complete Sample Collection πŸš€

Production-ready Python samples for the GitHub Copilot SDK β€” 17 fully-functional examples demonstrating AI agents, custom tools, browser automation, code review, BDD testing, and more. All tested in CI with gpt-5-mini (free tier). Clone, run, and build.

License: MIT Python 3.12+ CI Status E2E Proof

Why This Repository?

This is the most comprehensive collection of Python samples for the GitHub Copilot SDK. Unlike minimal "hello world" examples, these are production-ready patterns you can actually use:

  • βœ… 17 complete samples β€” streaming, tools, BDD testing, browser automation, code review, and more
  • βœ… Proven in CI β€” All samples run end-to-end with gpt-5-mini (GitHub's free tier model)
  • βœ… Single-file simplicity β€” Each sample is self-contained and ready to run
  • βœ… Real-world patterns β€” API testing, log analysis, test data generation, git commit messages
  • βœ… Best practices β€” Type hints, async/await, proper error handling, structured outputs

Perfect whether you're exploring the SDK for the first time or building production AI agents.

What Is This?

The GitHub Copilot SDK gives you programmatic access to the same AI agent runtime powering Copilot CLI and VS Code. This repository shows you how to use it effectively through practical, battle-tested examples.

Each script demonstrates a key SDK capability:

  • AI conversation patterns (streaming, multi-turn, interactive)
  • Custom tool definitions (function calling)
  • Real-world automation (browser control, code review, testing)
  • Production patterns (error handling, retries, structured output)

Prerequisites

Quick Start

# Clone this repo
git clone https://github.com/Michspirit99/copilot-sdk-python.git
cd copilot-sdk-python

# Install dependencies (creates venv recommended)
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -r requirements.txt

# Run any sample β€” instant AI!
python samples/hello_copilot.py
python samples/streaming_chat.py "Explain Python decorators"
python samples/code_reviewer.py samples/hello_copilot.py

That's it. No API keys needed if you have Copilot CLI access. All samples work with gpt-5-mini (free tier).

Complete Sample Catalog

🎯 Core SDK Patterns

Sample What It Shows Key Techniques
hello_copilot.py Minimal example β€” send prompt, get response Session management, basic async
streaming_chat.py Token-by-token streaming output Event handlers, real-time display
interactive_chat.py Full terminal chat with history Multi-turn conversations, message retrieval
multi_turn_agent.py Stateful agent across turns Session persistence, context management
multi_model.py Compare gpt-4.1 vs gpt-5-mini responses Model selection, parallel queries
resilient_client.py Retries, timeouts, error handling Production error patterns

πŸ”§ Advanced Features

Sample What It Shows Key Techniques
custom_tools.py Define Python functions callable by AI @define_tool, Pydantic models, function calling
code_reviewer.py AI code review with structured findings Tool-based structured output, streaming
model_explorer.py Inspect available models and capabilities API introspection, model metadata

πŸ€– Automation & Real-World Use Cases

Sample What It Does Use Cases
playwright_agent.py AI-guided browser automation Web scraping, testing, form automation
log_analyzer.py Analyze logs with custom tools Error detection, security analysis, performance
api_test_generator.py Generate pytest tests from OpenAPI specs API testing, test automation
test_data_generator.py Create realistic test data (JSON/SQL/CSV) Database seeding, test fixtures
file_summarizer.py Summarize any text file Documentation, README generation
git_commit_writer.py Generate conventional commit messages Git workflow automation

πŸ§ͺ AI-Enhanced Testing

Sample What It Shows Key Techniques
pytest_ai_validation.py AI-enhanced pytest with intelligent assertions AI-as-judge, ast.parse validation, JSON schema checks, copilot_session fixture
robot_copilot_library.py Robot Framework keyword library for AI agents BDD/Gherkin scenarios, keyword-driven AI testing, enterprise test integration
copilot_bdd.robot BDD test suite (Given/When/Then) for AI behaviour Robot Framework .robot file, Gherkin syntax, AI code generation + review

All samples include:

  • βœ… Complete, runnable code
  • βœ… Type hints and documentation
  • βœ… Error handling
  • βœ… CLI argument parsing
  • βœ… Tested in CI with gpt-5-mini

Proven Quality β€” E2E Proof

Unlike most SDK examples, every sample in this repository is proven to work end-to-end in CI:

+ API_TEST_GENERATOR       [OK] Test generation complete!
+ CODE_REVIEWER            [OK] Review complete  
+ CUSTOM_TOOLS             [OK] Tool calls executed
+ FILE_SUMMARIZER          [OK] Summary generated
+ GIT_COMMIT_WRITER        [OK] Commit message created
+ HELLO_COPILOT            [OK] Basic prompt/response
+ LOG_ANALYZER             [OK] Log analysis complete
+ MODEL_EXPLORER           [OK] 14 models discovered
+ MULTI_MODEL              [OK] 2 models compared
+ PYTEST_AI_VALIDATION     [OK] 4/4 AI tests passed
+ RESILIENT_CLIENT         [OK] 3 prompts with retries
+ ROBOT_COPILOT_LIBRARY    [OK] 3/3 BDD scenarios passed
+ STREAMING_CHAT           [OK] Token streaming works
+ TEST_DATA_GENERATOR      [OK] Test data generated
+ MULTI_TURN_AGENT         [OK] Stateful conversation
+ INTERACTIVE_CHAT         [SKIP] Interactive (requires stdin)
+ PLAYWRIGHT_AGENT         [SKIP] Requires browser setup

Summary: 17/17 scenarios validated (14 run, 3 skipped for interactivity)

See E2E workflow runs for full transcripts showing what each agent actually does.

Usage Examples

# Basic usage
python samples/hello_copilot.py

# Core samples with arguments
python samples/code_reviewer.py path/to/file.py
python samples/file_summarizer.py README.md
python samples/streaming_chat.py "Explain decorators in Python"

# Automation samples
python samples/playwright_agent.py https://example.com "Describe the page"
python samples/log_analyzer.py app.log errors
python samples/api_test_generator.py swagger.json pytest
python samples/test_data_generator.py user 50 json

AI-Enhanced Testing

These samples show how to integrate AI validation into established test frameworks β€” proving that Copilot SDK agents work correctly using the same tools enterprises already use.

πŸ§ͺ pytest (pytest_ai_validation.py)

  • AI-as-judge pattern: one AI call validates another's output
  • Deterministic + AI assertions: ast.parse, json.loads + AI relevance checks
  • copilot_session fixture for test lifecycle
  • Runs standalone OR with pytest -v

πŸ€– Robot Framework (copilot_bdd.robot + robot_copilot_library.py)

  • BDD/Gherkin syntax: Given I have a Copilot session / When I ask Copilot to generate code / Then the code should be valid Python
  • Python keyword library wrapping the entire Copilot SDK
  • Enterprise-ready: integrates AI agent testing into existing Robot Framework suites
  • 4 scenarios: code generation, bug detection, JSON output, concept explanation
# Run pytest AI tests
pytest samples/pytest_ai_validation.py -v

# Run Robot Framework BDD tests
robot samples/copilot_bdd.robot

# Both also run standalone (no test framework required)
python samples/pytest_ai_validation.py
python samples/robot_copilot_library.py

Automation Use Cases

The automation scripts demonstrate practical AI-powered workflows:

🌐 Browser Automation (playwright_agent.py)

  • Navigate websites and extract data
  • AI-guided form filling and interaction
  • Automated testing scenarios
  • Web scraping with natural language commands

πŸ“Š Log Analysis (log_analyzer.py)

  • Find and categorize errors automatically
  • Security threat detection
  • Performance bottleneck identification
  • Automated incident reports

πŸ§ͺ API Testing (api_test_generator.py)

  • Generate pytest/unittest test cases
  • Create Postman collections
  • Generate curl command references
  • Test coverage analysis from OpenAPI specs

🎲 Test Data (test_data_generator.py)

  • Realistic user profiles and datasets
  • Product catalogs and inventories
  • Order histories and transactions
  • Custom schemas in JSON/SQL/CSV formats

How It Works

The Copilot SDK

import asyncio
from copilot import CopilotClient

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

    session = await client.create_session({"model": "gpt-5-mini"})
    response = await session.send_and_wait(
        {"prompt": "Explain async/await"}
    )
    print(response.data.content)

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

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

Key concepts:

  • CopilotClient β€” Manages the connection to Copilot
  • Session config β€” Pass a dict with model, tools, and options
  • Streaming β€” Use session.on() with event handlers for token-by-token output
  • Tools β€” Define tools with @define_tool decorator and Pydantic models

Custom Tools (Function Calling)

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

class GetWeatherParams(BaseModel):
    city: str = Field(description="The city name")

@define_tool(description="Get the weather for a city")
def get_weather(params: GetWeatherParams) -> str:
    return f"Sunny in {params.city}"

# Pass tools in session config
session = await client.create_session({
    "model": "gpt-5-mini",
    "tools": [get_weather],
})
response = await session.send_and_wait(
    {"prompt": "What's the weather in Seattle?"}
)
print(response.data.content)

Virtual Environments (Recommended)

While not required, using a virtual environment is best practice:

# Create and activate venv
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Run samples
python samples/hello_copilot.py

BYOK (Bring Your Own Key)

Don't have a Copilot subscription? Use your own OpenAI API key:

import os

session = await client.create_session({
    "model": "gpt-5-mini",
    "provider": {
        "type": "openai",
        "base_url": "https://api.openai.com/v1",
        "api_key": os.getenv("OPENAI_API_KEY"),
    },
})

Set your API key:

export OPENAI_API_KEY="sk-..."  # On Windows: set OPENAI_API_KEY=sk-...

Project Structure

copilot-sdk-python-scripts/
β”œβ”€β”€ samples/                    # All runnable scripts
β”‚   β”œβ”€β”€ hello_copilot.py        # Minimal example
β”‚   β”œβ”€β”€ streaming_chat.py       # Streaming responses
β”‚   β”œβ”€β”€ interactive_chat.py     # Interactive terminal chat
β”‚   β”œβ”€β”€ code_reviewer.py        # AI code review
β”‚   β”œβ”€β”€ custom_tools.py         # Custom tool definitions
β”‚   β”œβ”€β”€ multi_model.py          # Multi-model comparison
β”‚   β”œβ”€β”€ file_summarizer.py      # File summarization
β”‚   β”œβ”€β”€ git_commit_writer.py    # Git commit message generation
β”‚   β”œβ”€β”€ playwright_agent.py     # Browser automation
β”‚   β”œβ”€β”€ log_analyzer.py         # Log file analysis
β”‚   β”œβ”€β”€ api_test_generator.py        # API test generation
β”‚   β”œβ”€β”€ test_data_generator.py       # Test data generation
β”‚   β”œβ”€β”€ pytest_ai_validation.py      # AI-enhanced pytest testing
β”‚   β”œβ”€β”€ robot_copilot_library.py     # Robot Framework keyword library
β”‚   └── copilot_bdd.robot            # BDD test suite (Given/When/Then)
β”œβ”€β”€ .github/
β”‚   β”œβ”€β”€ workflows/
β”‚   β”‚   β”œβ”€β”€ ci.yml               # CI validation (no live AI calls)
β”‚   β”‚   └── agent-scenarios.yml  # Optional E2E proof runs
β”œβ”€β”€ requirements.txt             # Python dependencies
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ .gitignore
└── CONTRIBUTING.md

Why Python + Copilot SDK?

Best SDK Sample Collection Available:

This Repository Typical SDK Examples
17 production-ready samples 2-3 "hello world" scripts
E2E tested in CI (see runs) Untested or manual-only
Real-world use cases Toy examples
Error handling + best practices Happy path only
Free tier model (gpt-5-mini) Requires expensive models
Single-file simplicity Complex project structure

Why Python? Python is already the go-to language for quick automation scripts. This repository shows how to make them AI-powered with the same simplicity you expect from Python.

CI/CD & Quality

This repository includes comprehensive CI/CD:

Default CI (ci.yml) β€” Runs on every push:

  • βœ… Lints with ruff
  • βœ… Syntax validation (compileall)
  • βœ… Import smoke tests (ensures samples stay valid)

E2E Proof (agent-scenarios.yml) β€” Optional, manual trigger:

  • βœ… Runs all 15 samples with real AI calls
  • βœ… Captures full execution transcripts
  • βœ… Uses gpt-5-mini (free tier, no cost concerns)
  • βœ… Proves every sample works end-to-end

View latest E2E run to see complete execution logs for all scenarios.

Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines. Whether you're:

  • Adding new sample scripts
  • Improving existing samples
  • Fixing bugs or typos
  • Enhancing documentation

All contributions are appreciated!

License

MIT β€” Use these samples however you like.

Resources

Acknowledgments

  • Built with the GitHub Copilot SDK
  • Samples tested with gpt-5-mini (free tier model)
  • All samples work with GitHub Copilot CLI authentication (no API keys needed)

⭐ Star this repo if you find it useful! Issues and PRs welcome.

About

15 production-ready Python samples for GitHub Copilot SDK β€” streaming, tools, browser automation, code review, and more. All tested in CI with gpt-5-mini (free tier).

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages