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

bash
$ 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=True encrypts 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.

MethodFor
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.worker names the worker that ran it, and response.pendra.request_id is the id to quote to support. On a stream, read stream.worker and stream.request_id after the last chunk.
  • response.notice explains a reply that looks wrong, such as a model that spent all its max_tokens thinking. See Notices.
  • response.moderation says 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.

ExceptionWhen
AuthenticationErrorThe API key is missing or wrong (401).
RateLimitErrorEvery worker for the model is busy (429). Wait for retry_after seconds and try again.
APIStreamErrorA reply failed after it had started, for example if a worker went offline.
APIStatusErrorAny other error response.
APIConnectionErrorPendra 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.