OpenAI ChatKit (Python SDK)
OpenAI ChatKit is a Python SDK designed for building custom backend servers for OpenAI's ChatKit, enabling developers to integrate advanced AI-powered chat experiences into their applications. It is a key component of the broader OpenAI AgentKit suite, facilitating the creation of conversational AI agents with features like response streaming, tool invocation, and thread management. The library is currently at version 1.6.3 and is actively maintained, with recent updates suggesting ongoing development and integration with OpenAI's evolving agent capabilities.
Warnings
- breaking The `openai-chatkit` library is currently in beta, and the broader ChatKit and AgentKit ecosystem is evolving rapidly. This means there is a higher likelihood of breaking changes and API adjustments between versions, potentially leading to 'Dependency and Versioning Issues'.
- gotcha Never expose your raw OpenAI API key on the client-side. The `openai-chatkit` Python SDK is for backend use, where your server securely generates a short-lived `client_secret` using the main `openai` library's `beta.chatkit.sessions.create` method. This `client_secret` is then safely passed to your frontend ChatKit component for authentication.
- gotcha A common 'blank screen' issue on the frontend occurs if the domain where ChatKit is embedded is not explicitly added to the allowlist in your OpenAI organization settings. This is a critical security feature to prevent unauthorized usage.
- gotcha The `openai-chatkit` Python SDK is a *backend* library. It does not provide a user interface. You will need a separate frontend component (e.g., `@openai/chatkit-react` for React applications or the ChatKit JS web component) to display the chat interface and interact with your `openai-chatkit` backend.
- gotcha Costs for ChatKit solutions can be unpredictable, as they are directly tied to OpenAI API usage (token consumption) by your AI agents. This can fluctuate significantly based on user interaction complexity and volume.
- deprecated There have been reports (as of February 2026) that the official OpenAI ChatKit Starter App repositories experienced issues, specifically returning 404 errors, indicating potential changes or deprecations in how starter templates are meant to be used or deployed.
Install
-
pip install openai-chatkit
Imports
- ChatKitServer
from openai_chatkit.server import ChatKitServer
- Agent
from openai.lib.chatkit import Agent
- sessions.create
client.beta.chatkit.sessions.create(...)
Quickstart
import os
from fastapi import FastAPI, Request
from openai import OpenAI
from openai_chatkit.server import ChatKitServer, UserMessageItem
from openai.lib.chatkit import Agent
app = FastAPI()
openai_client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY', ''))
# Define your AI agent
assistant_agent = Agent(
model="gpt-4o",
name="Assistant",
instructions="You are a helpful assistant."
)
# Implement your ChatKit server
class MyChatKitServer(ChatKitServer):
def __init__(self):
super().__init__()
self.agent = assistant_agent
async def respond(
self, thread_id: str, input: UserMessageItem, request: Request
):
# Example: Process user input with your agent
# In a real application, you would manage state and agent context here
response_content = f"Echoing: {input.text}"
yield {"type": "text", "text": response_content}
chatkit_server = MyChatKitServer()
@app.post("/api/chatkit/session")
async def create_chat_session(request: Request):
# In a real app, authenticate the user and provide a unique user_id
user_id = "user_123"
# Workflow ID would come from your OpenAI Agent Builder workflow
workflow_id = os.environ.get('CHATKIT_WORKFLOW_ID', 'wf-YOUR_WORKFLOW_ID')
# The client secret should be generated on the server-side only
session = openai_client.beta.chatkit.sessions.create(
workflow_id=workflow_id,
user_id=user_id
)
return {"client_secret": session.client_secret}
@app.post("/api/chatkit/respond")
async def chatkit_respond(request: Request):
return await chatkit_server.handle_request(request)