Official Python SDK for ChatRoutes API - Conversation branching and multi-model AI chat
Project description
ChatRoutes Python SDK
Official Python SDK for the ChatRoutes API - A powerful conversation management platform with advanced branching capabilities.
⚠️ Beta Release: ChatRoutes is currently in beta. The API may change without maintaining backward compatibility. Please use with caution in production environments.
🚀 Try It Now!
Want to try ChatRoutes immediately? Click the badge below to open an interactive notebook in Google Colab:
No installation required - just run the cells and start experimenting!
Installation
pip install chatroutes
Getting Started
1. Get Your API Key
IMPORTANT: Before you can use ChatRoutes, you must obtain an API key:
- Visit chatroutes.com
- Sign up for a free account
- Go to Dashboard → Navigate to the API section
- Generate your API key
- Copy and save your API key securely
2. Quick Start
from chatroutes import ChatRoutes
client = ChatRoutes(api_key="your-api-key")
conversation = client.conversations.create({
'title': 'My First Conversation',
'model': 'gpt-5' # or 'claude-opus-4-1', 'claude-sonnet-4-5', etc.
})
response = client.messages.send(
conversation['id'],
{
'content': 'Hello, how are you?',
'model': 'gpt-5'
}
)
print(response['message']['content'])
Supported Models
ChatRoutes currently supports the following AI models:
OpenAI:
gpt-5(default) - OpenAI's GPT-5
Anthropic Claude 4:
claude-opus-4-1- Claude Opus 4.1 (most capable)claude-opus-4- Claude Opus 4claude-opus-4-0- Claude Opus 4.0claude-sonnet-4-5- Claude Sonnet 4.5 (best for coding)claude-sonnet-4-0- Claude Sonnet 4.0
Anthropic Claude 3:
claude-3-7-sonnet-latest- Claude 3.7 Sonnet (latest)claude-3-5-haiku-latest- Claude 3.5 Haiku (fastest)
Important: Use these exact model names. Other model names (e.g., gpt-4o, gpt-4o-mini, claude-sonnet-4) are not supported and will result in an error.
Features
- Conversation Management: Create, list, update, and delete conversations
- Message Handling: Send messages with support for streaming responses
- Branch Operations: Create and manage conversation branches for exploring alternatives
- Type Safety: Full type hints using TypedDict for better IDE support
- Error Handling: Comprehensive exception hierarchy for different error scenarios
- Retry Logic: Built-in exponential backoff retry mechanism
Usage Examples
Creating a Conversation
conversation = client.conversations.create({
'title': 'Product Discussion',
'model': 'gpt-5'
})
Sending Messages
response = client.messages.send(
conversation_id='conv_123',
data={
'content': 'What are the key features?',
'model': 'gpt-5',
'temperature': 0.7
}
)
print(response['message']['content']) # AI response
print(f"Tokens used: {response['usage']['totalTokens']}")
Streaming Responses
def on_chunk(chunk):
if chunk.get('type') == 'content' and chunk.get('content'):
print(chunk['content'], end='', flush=True)
def on_complete(message):
print(f"\n\nMessage ID: {message['id']}")
client.messages.stream(
conversation_id='conv_123',
data={'content': 'Tell me a story'},
on_chunk=on_chunk,
on_complete=on_complete
)
Working with Branches
branch = client.branches.create(
conversation_id='conv_123',
data={
'title': 'Alternative Response',
'contextMode': 'FULL'
}
)
fork = client.branches.fork(
conversation_id='conv_123',
data={
'forkPointMessageId': 'msg_456',
'title': 'Exploring Different Approach'
}
)
Listing Conversations
result = client.conversations.list({
'page': 1,
'limit': 10,
'filter': 'all'
})
for conv in result['data']:
print(f"{conv['title']} - {conv['createdAt']}")
Error Handling
The SDK provides specific exception types for different error scenarios:
from chatroutes import (
ChatRoutesError,
AuthenticationError,
RateLimitError,
ValidationError,
NotFoundError,
ServerError
)
try:
conversation = client.conversations.get('conv_123')
except AuthenticationError:
print("Invalid API key")
except NotFoundError:
print("Conversation not found")
except RateLimitError as e:
print(f"Rate limited. Retry after {e.retry_after} seconds")
except ChatRoutesError as e:
print(f"Error: {e.message}")
API Reference
ChatRoutes Client
client = ChatRoutes(
api_key="your-api-key",
base_url="https://api.chatroutes.com/api/v1", # optional
timeout=30, # optional, in seconds
retry_attempts=3, # optional
retry_delay=1.0 # optional, in seconds
)
Conversations Resource
create(data: CreateConversationRequest) -> Conversationlist(params: ListConversationsParams) -> PaginatedResponseget(conversation_id: str) -> Conversationupdate(conversation_id: str, data: dict) -> Conversationdelete(conversation_id: str) -> Noneget_tree(conversation_id: str) -> ConversationTree
Messages Resource
send(conversation_id: str, data: SendMessageRequest) -> SendMessageResponsestream(conversation_id: str, data: SendMessageRequest, on_chunk: Callable, on_complete: Callable) -> Nonelist(conversation_id: str, branch_id: str) -> List[Message]update(message_id: str, content: str) -> Messagedelete(message_id: str) -> None
Branches Resource
list(conversation_id: str) -> List[Branch]create(conversation_id: str, data: CreateBranchRequest) -> Branchfork(conversation_id: str, data: ForkConversationRequest) -> Branchupdate(conversation_id: str, branch_id: str, data: dict) -> Branchdelete(conversation_id: str, branch_id: str) -> Noneget_messages(conversation_id: str, branch_id: str) -> List[Message]merge(conversation_id: str, branch_id: str) -> Branch
Type Definitions
The SDK includes comprehensive type definitions using TypedDict:
ConversationMessageBranchCreateConversationRequestSendMessageRequestSendMessageResponseCreateBranchRequestForkConversationRequestConversationTreeTreeNodeListConversationsParamsPaginatedResponseStreamChunk
Development
Setup
git clone https://github.com/chatroutes/chatroutes-python-sdk.git
cd chatroutes-python-sdk
pip install -e ".[dev]"
Running Tests
pytest
Type Checking
mypy chatroutes
Code Formatting
black chatroutes
License
MIT License - see LICENSE file for details
Support
- Documentation: https://docs.chatroutes.com
- API Reference: https://api.chatroutes.com/docs
- Email: support@chatroutes.com
- GitHub Issues: https://github.com/chatroutes/chatroutes-python-sdk/issues
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chatroutes-0.1.3.tar.gz.
File metadata
- Download URL: chatroutes-0.1.3.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56ba4972a22080e8e51d139ecc21554373e1455f0c5c53edba7569fd7cd473ab
|
|
| MD5 |
27ef863c22a53ac9cc368e7c9949ba57
|
|
| BLAKE2b-256 |
9ad6ffd165e41a50c75fbf0e32071d81efc0f5d75376cc917be76ac6fb42aa21
|
File details
Details for the file chatroutes-0.1.3-py3-none-any.whl.
File metadata
- Download URL: chatroutes-0.1.3-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c9b6f5c48c239f727a52d5e4353bfd0a1b9c06a636995bae6784ec661e4338b
|
|
| MD5 |
3e3e53c7ad9158fe1e97023b86855e49
|
|
| BLAKE2b-256 |
caeda6bfd4b6c93a2d89fe9a563b279eb731d988e586a40d0e42cb2762072286
|