Typed Python bindings for the Rust genai crate, built with pyo3 and maturin.
This repo uses the upstream GitHub repository for genai directly:
genai = { git = "https://github.com/jeremychone/rust-genai" }The Python extension currently exposes:
ClientChatMessageChatRequestChatOptionsChatResponseChatStreamEventStreamEndToolCallUsage
The main async entry points are:
await client.achat(model, request, options=None)for a full responseawait client.astream_chat(model, request, options=None)for chunked streaming
Editable install with uv:
uv pip install -e .This builds the pyo3 extension and makes genai_pyo3 importable from the active environment.
Minimal async call:
import asyncio
from genai_pyo3 import ChatMessage, ChatOptions, ChatRequest, Client
async def main() -> None:
client = Client()
request = ChatRequest(
messages=[ChatMessage("user", "Say hello in one short sentence")]
)
options = ChatOptions(temperature=0.2)
response = await client.achat("gpt-4o-mini", request, options)
print(response.text)
asyncio.run(main())Minimal async streaming call:
import asyncio
from genai_pyo3 import ChatMessage, ChatOptions, ChatRequest, Client
async def main() -> None:
client = Client()
request = ChatRequest(
messages=[ChatMessage("user", "Write three short bullet points about Rust")]
)
options = ChatOptions(capture_content=True, capture_usage=True)
stream = await client.astream_chat("gpt-4o-mini", request, options)
async for event in stream:
print(event.kind, event.content)
asyncio.run(main())examples/async_chat_basic.py: basicachatexampleexamples/async_stream_chat.py: basicastream_chatexampleexamples/client_overrides.py: explicit provider/API-key override exampleexamples/gemini_env.py: Geminiachatexample using env varsexamples/gemini_stream_env.py: Gemini streaming example using env vars
The Gemini examples use:
GEMINI_API_KEYGEMINI_MODEL(optional, defaults togemini-2.0-flash)GEMINI_BASE_URL(optional, for custom or local Gemini-compatible endpoints)
Non-streaming:
GEMINI_API_KEY=your-key python examples/gemini_env.pyStreaming:
GEMINI_API_KEY=your-key python examples/gemini_stream_env.pyWith a custom endpoint:
GEMINI_API_KEY=your-key \
GEMINI_BASE_URL=http://your-host/v1beta \
python examples/gemini_env.pyGEMINI_API_KEY=your-key \
GEMINI_BASE_URL=http://your-host/v1beta \
python examples/gemini_stream_env.pyIf GEMINI_BASE_URL is unset, the examples use the default Gemini endpoint resolved by the underlying genai client.
Cargo.toml: Rust extension cratepyproject.toml: Python package metadata andmaturinconfigpython/genai_pyo3: Python package entrypointsrc/lib.rs:pyo3binding implementationexamples/: small Python usage examples
Create a virtualenv and install in editable mode:
cd genai-pyo3
python -m venv .venv
source .venv/bin/activate
uv pip install -e .If you want stricter reproducibility, pin that dependency to a branch, tag, or revision.