Python SDK for the Routeway API
Project description
routeway-py
A Python SDK for the Routeway API. It's OpenAI-compatible, so if you've used the OpenAI Python client, this will feel familiar.
What it does
- Streaming: Get responses chunk by chunk instead of waiting for the full response
- Async: Built on
httpxfor non-blocking requests - Tool calling: Function calling support for structured outputs
- Reasoning modes: Extended thinking for complex tasks
- Type hints: Fully typed with
TypedDictand dataclasses
Install
pip install routeway-py
For async support:
pip install routeway-py[async]
Usage
Basic
from routeway import RoutewayClient
client = RoutewayClient(api_key="your-api-key")
response = client.chat_completion(
model="model_id",
messages=[{"role": "user", "content": "Hello, how are you?"}],
)
print(response["choices"][0]["message"]["content"])
client.close()
Or use it as a context manager so cleanup happens automatically:
from routeway import RoutewayClient
with RoutewayClient(api_key="your-api-key") as client:
response = client.chat_completion(
model="model_id",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response["choices"][0]["message"]["content"])
Streaming
from routeway import RoutewayClient, StreamOptions
client = RoutewayClient(api_key="your-api-key")
stream = client.chat_completion(
model="model_id",
messages=[{"role": "user", "content": "Write a poem"}],
stream=True,
stream_options=StreamOptions(include_usage=True),
)
for chunk in stream:
if chunk["choices"]:
delta = chunk["choices"][0].get("delta", {})
if content := delta.get("content"):
print(content, end="", flush=True)
client.close()
Async
import asyncio
from routeway import AsyncRoutewayClient
async def main():
async with AsyncRoutewayClient(api_key="your-api-key") as client:
response = await client.chat_completion(
model="model_id",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response["choices"][0]["message"]["content"])
asyncio.run(main())
Function calling
from routeway import RoutewayClient, create_function, create_tool
weather_function = create_function(
name="get_weather",
description="Get the current weather in a location",
parameters={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city, e.g. San Francisco",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
},
},
"required": ["location"],
},
)
tool = create_tool(weather_function)
with RoutewayClient(api_key="your-api-key") as client:
response = client.chat_completion(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=[tool.to_dict()],
tool_choice="auto",
)
message = response["choices"][0]["message"]
if tool_calls := message.get("tool_calls"):
for call in tool_calls:
print(f"Function: {call['function']['name']}")
print(f"Arguments: {call['function']['arguments']}")
Configuration
Set your API key via environment variable:
export ROUTEWAY_API_KEY="your-api-key"
Then initialize without passing the key:
from routeway import RoutewayClient
client = RoutewayClient()
Or configure manually:
client = RoutewayClient(
api_key="your-api-key",
base_url="https://api.routeway.ai/v1",
timeout=30.0,
max_retries=3,
)
Error handling
from routeway import (
RoutewayError,
RoutewayAuthError,
RoutewayRateLimitError,
RoutewayServerError,
)
try:
response = client.chat_completion(...)
except RoutewayAuthError:
print("Check your API key")
except RoutewayRateLimitError:
print("Hit the rate limit")
except RoutewayServerError:
print("Server error, try again")
except RoutewayError as e:
print(f"Something went wrong: {e}")
Requirements
- Python 3.9+
requests>= 2.31.0httpx>= 0.24.0 (optional, for async)
License
MIT © fabvali this npm package is a renewal and remake of the old clashai one made by verleihernix
Links
☕ Support My Work
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 routeway_py-0.2.0.tar.gz.
File metadata
- Download URL: routeway_py-0.2.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdeb9b97c9105218fc557fed8ecfe7371bd51908bb051dfd2182d4ddfc0da34a
|
|
| MD5 |
cf74f583bbd399a985982e2e80a73000
|
|
| BLAKE2b-256 |
5964f02c8a30b3ce1b04c97dc94bc29d905e371cc695314b1d772363bd2fbd93
|
File details
Details for the file routeway_py-0.2.0-py3-none-any.whl.
File metadata
- Download URL: routeway_py-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e57aac1ffe39b3239f0b6b3f9cd61577f20c148bfb851d657cfb883ea7f39b2
|
|
| MD5 |
40aa00d640b2567108eab4d6f17f6ebe
|
|
| BLAKE2b-256 |
8b62d5c5d167f322ec244812c4515a2831a80e5f6bc52220957205ee524e80f1
|