API essentials

OpenAI Python SDK

Install the Python client, call Chat Completions and Responses, and pass Kendr-specific options.

Install and configure

Use Python 3.10 or later. Set KENDR_API_KEY as described in the quickstart. Use a Kendr key and model alias with the Kendr base URL.

python -m venv .venv
# macOS/Linux: source .venv/bin/activate
# PowerShell: .venv\Scripts\Activate.ps1
python -m pip install openai

Make a chat request

Save as hello.py and run python hello.py. Automatic retries are disabled so your application can decide how to recover.

import os
from uuid import uuid4
from openai import OpenAI

client = OpenAI(api_key=os.environ["KENDR_API_KEY"],
                base_url="https://api.kendr.org/v1", timeout=180.0, max_retries=0)
request_id = str(uuid4())  # Persist before sending if you need recovery.
response = client.chat.completions.create(
    model="kendr-intelligent",
    messages=[{"role": "user", "content": "Explain API keys in two sentences."}],
    extra_headers={"Idempotency-Key": request_id},
)
if response.choices[0].finish_reason != "stop":
    raise RuntimeError("Incomplete response; inspect before using")
print(response.choices[0].message.content)

Use Responses and read usage

With the same client, use input instead of messages. The SDK collects text blocks into output_text.

response = client.responses.create(
    model="kendr-intelligent", input="Write a three-step launch checklist.",
    extra_headers={"Idempotency-Key": str(uuid4())},
)
print(response.output_text)
print(response.model_dump().get("kendr_usage"))

Continue a conversation

Keep history in your application and send the relevant turns on each request. Do not assume the SDK creates server-side conversation storage.

messages = [{"role": "user", "content": "Suggest a name for a library app."}]
first = client.chat.completions.create(model="kendr-intelligent", messages=messages)
messages.append({"role": "assistant", "content": first.choices[0].message.content or ""})
messages.append({"role": "user", "content": "Give me a tagline for that name."})
second = client.chat.completions.create(model="kendr-intelligent", messages=messages)
print(second.choices[0].message.content)

Attach a tool server

Register your server using the MCP guide, then set KENDR_MCP_SERVER_ID. Use extra_body for Kendr fields outside the upstream SDK schema.

response = client.responses.create(
    model="kendr-intelligent", input="Find the team release checklist.",
    extra_body={"tools": [{"type": "kendr_mcp",
                           "server_id": os.environ["KENDR_MCP_SERVER_ID"]}]},
    extra_headers={"Idempotency-Key": str(uuid4())},
)
print(response.output_text)

Continue with streaming, JSON output, or image generation. Images use a dedicated HTTP route. Compatibility does not imply every OpenAI resource is implemented.

Handle SDK errors

Catch openai.APIStatusError to inspect status_code and openai.APIConnectionError for transport failures. Keep the request ID when a timeout leaves completion uncertain. Do not log keys or private response bodies. Follow recovery rules. SDK setup is described in the official OpenAI quickstart.