CopilotKit Python SDK

0.1.87 · active · verified Thu Apr 16

CopilotKit is a framework for building AI Copilots that can deeply integrate with your product's UI. The Python SDK allows you to define AI tools and integrate them into an agent runtime, typically exposed via a web framework like FastAPI. It is currently in version 0.1.87 and is part of a larger, actively developed ecosystem, with the Python SDK evolving rapidly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `CopilotKit` application, define and register a tool, and expose it via a FastAPI server. It requires `fastapi` and `uvicorn` to be installed (e.g., `pip install "copilotkit[fastapi]"`). The `OPENAI_API_KEY` environment variable is necessary for the underlying OpenAI client.

import os
from openai import OpenAI
from copilotkit import CopilotKit, tool
from copilotkit.fastapi import CopilotKitFastAPI
from fastapi import FastAPI
import uvicorn

# Ensure OPENAI_API_KEY is set in environment or .env file
openai_api_key = os.environ.get("OPENAI_API_KEY", "")

if not openai_api_key:
    print("Warning: OPENAI_API_KEY not found. Please set it as an environment variable.")
    # For demonstration, we'll continue, but real usage would require a key.

# Initialize OpenAI client (requires openai>=1.0.0)
openai_client = OpenAI(api_key=openai_api_key)

# Create a CopilotKit app instance
app = CopilotKit(openai_client=openai_client)

# Define a tool using the @tool decorator
@tool
def get_current_weather(location: str, unit: str = "fahrenheit") -> dict:
    """Get the current weather in a given location.

    Args:
        location (str): The city and state, e.g. "San Francisco, CA"
        unit (str, optional): The unit of temperature. Defaults to "fahrenheit".

    Returns:
        dict: A dictionary containing temperature and unit.
    """
    print(f"Tool called: get_current_weather for {location} in {unit}")
    if "san francisco" in location.lower():
        return {"temperature": "70", "unit": unit}
    elif "new york" in location.lower():
        return {"temperature": "60", "unit": unit}
    else:
        return {"temperature": "unknown", "unit": unit}

# Register the tool with the CopilotKit app
app.register_tool(get_current_weather)

# Integrate with FastAPI to expose the CopilotKit app
fast_app = FastAPI()
fast_app.include_router(CopilotKitFastAPI(app))

@fast_app.get("/health")
async def health_check():
    return {"status": "ok"}

# To run this example:
# 1. Save the code above as `main.py`.
# 2. Install the necessary dependencies: `pip install "copilotkit[fastapi]"`.
# 3. Set your OpenAI API key: `export OPENAI_API_KEY="your_key_here"` (or use a .env file).
# 4. Run the FastAPI application using Uvicorn from your terminal:
#    `uvicorn main:fast_app --reload --port 8000`
# Your CopilotKit backend will then be accessible at http://localhost:8000/copilotkit
print("CopilotKit app configured. Use `uvicorn main:fast_app --reload --port 8000` to run.")

view raw JSON →