Python SDK
The Pendra Python SDK lets you call every Pendra model from Python, with sync and async clients and streaming. It needs Python 3.10 or later.
Install
$ pip install pendra
For end-to-end encryption,
install pendra[private-inference] instead. The package is on
PyPI.
Send a request
Create a client with your
API key,
or leave out api_key and set PENDRA_API_KEY
instead.
from pendra import Pendra
client = Pendra(api_key="pdr_sk_...")
response = client.chat.completions.create(
model="qwen3.6:27b",
messages=[{"role": "user", "content": "What is the capital of Wales?"}],
)
print(response.choices[0].message.content)
The client also takes these options:
-
private_inference=Trueencrypts every request so only your worker can read it. See End-to-end encryption. timeout, in seconds. The default is 120.
Stream the reply
Set stream=True to print the reply as it's written. The last
few chunks carry usage and timing rather than text, so skip any chunk
without choices.
with client.chat.completions.create(
model="qwen3.6:27b",
messages=[{"role": "user", "content": "Write a poem about London."}],
stream=True,
) as stream:
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
Async
AsyncPendra has the same methods for asyncio apps. To send
many requests at once, see Batch inference.
import asyncio
from pendra import AsyncPendra
async def main():
async with AsyncPendra(api_key="pdr_sk_...") as client:
response = await client.chat.completions.create(
model="qwen3.6:27b",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
asyncio.run(main())
What you can call
Each guide has a worked example. For every parameter, see the API reference.
| Method | For |
|---|---|
client.chat.completions.create() |
Chat, vision, tools and structured outputs. See Chat. |
client.images.generations.create() |
Generate images from a prompt. See Image generation. |
client.audio.transcriptions.create() |
Turn speech into text. See Audio transcription. |
client.audio.speech.create() |
Turn text into speech. See Text to speech. |
client.embeddings.create() |
Turn text into vectors for search. See Embeddings. |
client.rerank.create() |
Sort documents by relevance to a query. See Reranking. |
client.moderations.create() |
Check text or images for harmful content. See Moderation. |
client.models.list() |
List the models you can call. See Models. |
Any other keyword argument is sent to the API as it is, and
extra_headers adds HTTP headers to one request.
Pendra extras
Every reply has a pendra field on top of the usual ones:
-
response.pendra.workernames the worker that ran it, andresponse.pendra.request_idis the id to quote to support. On a stream, readstream.workerandstream.request_idafter the last chunk. -
response.noticeexplains a reply that looks wrong, such as a model that spent all itsmax_tokensthinking. See Notices. -
response.moderationsays why a reply was flagged or blocked, if content filtering is on for your API key.
To send a request to one particular worker, pass
worker_id="...".
response = client.chat.completions.create(
model="qwen3.6:27b",
messages=[{"role": "user", "content": "Hello!"}],
)
if response.pendra and response.pendra.worker:
print(response.pendra.worker.name, response.pendra.request_id)
if response.notice:
print(response.notice.code, response.notice.message)
Errors
Every exception is a pendra.APIError, with a readable message.
| Exception | When |
|---|---|
AuthenticationError | The API key is missing or wrong (401). |
RateLimitError | Every worker for the model is busy (429). Wait for retry_after seconds and try again. |
APIStreamError | A reply failed after it had started, for example if a worker went offline. |
APIStatusError | Any other error response. |
APIConnectionError | Pendra couldn't be reached. |
import time
import pendra
try:
response = client.chat.completions.create(
model="qwen3.6:27b",
messages=[{"role": "user", "content": "Hello!"}],
)
except pendra.RateLimitError as err:
time.sleep(err.retry_after or 5)
# then try again
except pendra.APIStreamError as err:
print(err.message, err.request_id)
A stream that fails part way raises APIStreamError from the
loop, and you keep the text that already arrived. So can a request that
runs for more than about 80 seconds, such as a long transcription. Quote
its request_id if you contact support. For every error code, see
Errors & rate limits.