{"id":7112,"library":"copilotkit","title":"CopilotKit Python SDK","description":"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.","status":"active","version":"0.1.87","language":"en","source_language":"en","source_url":"https://github.com/CopilotKit/CopilotKit/tree/main/sdk-python","tags":["AI","LLM","copilot","tooling","SDK","FastAPI"],"install":[{"cmd":"pip install copilotkit","lang":"bash","label":"Default Install"},{"cmd":"pip install \"copilotkit[fastapi]\"","lang":"bash","label":"Install with FastAPI integration"}],"dependencies":[{"reason":"Required for interacting with OpenAI's LLMs.","package":"openai","optional":false},{"reason":"Recommended for managing environment variables.","package":"python-dotenv","optional":false},{"reason":"Required for the `copilotkit.fastapi` integration to expose tools.","package":"fastapi","optional":true},{"reason":"ASGI server for running FastAPI applications.","package":"uvicorn","optional":true}],"imports":[{"symbol":"CopilotKit","correct":"from copilotkit import CopilotKit"},{"symbol":"tool","correct":"from copilotkit import tool"},{"note":"Used for FastAPI integration to expose the CopilotKit app as an API.","symbol":"CopilotKitFastAPI","correct":"from copilotkit.fastapi import CopilotKitFastAPI"}],"quickstart":{"code":"import os\nfrom openai import OpenAI\nfrom copilotkit import CopilotKit, tool\nfrom copilotkit.fastapi import CopilotKitFastAPI\nfrom fastapi import FastAPI\nimport uvicorn\n\n# Ensure OPENAI_API_KEY is set in environment or .env file\nopenai_api_key = os.environ.get(\"OPENAI_API_KEY\", \"\")\n\nif not openai_api_key:\n    print(\"Warning: OPENAI_API_KEY not found. Please set it as an environment variable.\")\n    # For demonstration, we'll continue, but real usage would require a key.\n\n# Initialize OpenAI client (requires openai>=1.0.0)\nopenai_client = OpenAI(api_key=openai_api_key)\n\n# Create a CopilotKit app instance\napp = CopilotKit(openai_client=openai_client)\n\n# Define a tool using the @tool decorator\n@tool\ndef get_current_weather(location: str, unit: str = \"fahrenheit\") -> dict:\n    \"\"\"Get the current weather in a given location.\n\n    Args:\n        location (str): The city and state, e.g. \"San Francisco, CA\"\n        unit (str, optional): The unit of temperature. Defaults to \"fahrenheit\".\n\n    Returns:\n        dict: A dictionary containing temperature and unit.\n    \"\"\"\n    print(f\"Tool called: get_current_weather for {location} in {unit}\")\n    if \"san francisco\" in location.lower():\n        return {\"temperature\": \"70\", \"unit\": unit}\n    elif \"new york\" in location.lower():\n        return {\"temperature\": \"60\", \"unit\": unit}\n    else:\n        return {\"temperature\": \"unknown\", \"unit\": unit}\n\n# Register the tool with the CopilotKit app\napp.register_tool(get_current_weather)\n\n# Integrate with FastAPI to expose the CopilotKit app\nfast_app = FastAPI()\nfast_app.include_router(CopilotKitFastAPI(app))\n\n@fast_app.get(\"/health\")\nasync def health_check():\n    return {\"status\": \"ok\"}\n\n# To run this example:\n# 1. Save the code above as `main.py`.\n# 2. Install the necessary dependencies: `pip install \"copilotkit[fastapi]\"`.\n# 3. Set your OpenAI API key: `export OPENAI_API_KEY=\"your_key_here\"` (or use a .env file).\n# 4. Run the FastAPI application using Uvicorn from your terminal:\n#    `uvicorn main:fast_app --reload --port 8000`\n# Your CopilotKit backend will then be accessible at http://localhost:8000/copilotkit\nprint(\"CopilotKit app configured. Use `uvicorn main:fast_app --reload --port 8000` to run.\")","lang":"python","description":"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."},"warnings":[{"fix":"Refer to the Python SDK's specific documentation and examples on GitHub for supported features. Align expectations with the current Python SDK version.","message":"The CopilotKit Python SDK (v0.1.x) is at an earlier development stage compared to the JavaScript/TypeScript frontend SDKs (v1.x). This may lead to feature disparities or slower adoption of new features from the broader CopilotKit ecosystem.","severity":"gotcha","affected_versions":"0.1.0 - 0.1.87"},{"fix":"Always provide precise type hints for all arguments and the return type of functions decorated with `@tool`.","message":"Tool definition relies heavily on Python type hints. Without accurate type hints for function arguments and return types, the underlying LLM may fail to correctly interpret the tool's schema, leading to incorrect or failed tool calls.","severity":"gotcha","affected_versions":"All"},{"fix":"Use the provided framework integrations, such as `CopilotKitFastAPI` for FastAPI, to expose your CopilotKit app via a standard web server like Uvicorn.","message":"The `CopilotKit` app is designed to be integrated into a web framework (like FastAPI), not run as a standalone Python script that serves HTTP requests directly. Attempts to call the `CopilotKit` instance as a server will fail.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure your environment uses Python 3.10, 3.11, or 3.12.","message":"Python 3.9 and older versions are not officially supported. The `requires_python` spec is `<3.13,>=3.10`.","severity":"deprecated","affected_versions":"<=0.1.87"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install copilotkit`","cause":"The `copilotkit` package is not installed in your Python environment.","error":"ModuleNotFoundError: No module named 'copilotkit'"},{"fix":"Upgrade the `openai` library: `pip install --upgrade openai`. If using older OpenAI, initialize with `openai.api_key = os.environ.get(\"OPENAI_API_KEY\")`.","cause":"You are likely using an outdated version of the `openai` library (before v1.0.0) with the `OpenAI(api_key=...)` syntax, which is for v1+.","error":"TypeError: 'OpenAI' object has no attribute 'api_key'"},{"fix":"Ensure all arguments and return values of functions decorated with `@tool` have precise and correct Python type hints, and clear docstrings.","cause":"The LLM's understanding of the tool's signature is derived from its Python type hints and docstrings. Missing or ambiguous type hints (e.g., `location: Any` instead of `location: str`) can lead to malformed tool calls.","error":"LLM calls tool incorrectly or generates invalid arguments."},{"fix":"Verify that your application file (e.g., `main.py`) defines a FastAPI instance named `fast_app` (or whatever name you use) and that you're pointing to it correctly in the `uvicorn` command (e.g., `uvicorn your_file_name:your_app_instance_name`).","cause":"The `uvicorn` command requires a reference to your FastAPI application instance. The example uses `main:fast_app` meaning `fast_app` in `main.py`.","error":"uvicorn.config.ConfigError: No 'fast_app' attribute in file 'main.py'."}]}