Ein OpenAI-SDK-Client fuer Text-, Bild- und Videomodelle: Praxisleitfaden
Der Artikel zeigt Muster wie eine eigene base_url, getrennte Endpunkte pro Ausgabetyp und asynchrone Video-Jobs, die sich auf die meisten OpenAI-kompatiblen Provider uebertragen lassen.

Transparenzhinweis: Ich arbeite an APIrove, der API-Plattform, die in den nachfolgenden Beispielen verwendet wird. Die Muster selbst (eigene base_url, getrennte Endpunkte pro Ausgabetyp, asynchrone Video-Jobs) gelten für die meisten OpenAI-kompatiblen Anbieter, daher habe ich den Artikel so gehalten, dass er auch nützlich bleibt, wenn Sie die Plattform nie nutzen.
Die meisten KI-Nebenprojekte beginnen auf dieselbe Weise: ein SDK, ein Anbieter, ein Schlüssel. Dann braucht ein Feature ein Bildmodell, jemand möchte ein anderes Textmodell ausprobieren, und es taucht ein Videoexperiment auf. Plötzlich hat man drei Clients, drei Authentifizierungsstile und drei Abrechnungs-Dashboards.
Ein Weg, das beherrschbar zu halten, besteht darin, das bereits verwendete OpenAI SDK beizubehalten und nur die base_url zu ändern. Dieser Beitrag führt das anhand einer Basis-URL vor, die Text-, Bild- und Videomodelle bedient, und weist auf die Stellen hin, an denen "OpenAI-kompatibel" trotzdem einen genaueren Blick erfordert.
export APIROVE_API_KEY = 'YOUR_API_KEY' export APIROVE_MODEL = 'YOUR_MODEL_ID' Vollbildmodus aktivieren Vollbildmodus beenden Schritt 2: Eine Zeile im Client ändern Mit dem offiziellen Python-SDK ( pip install openai ) ist die einzige Änderung die base_url. Beachten Sie, dass sie auf /v1 endet; das SDK hängt den Endpunkt-Pfad selbst an.
import os from openai import OpenAI client = OpenAI ( api_key = os. environ [ "APIROVE_API_KEY" ], base_url = "https://apirove.com/v1", ) response = client. responses. create ( model = os. environ [ "APIROVE_MODEL" ], input = "Hello!", max_output_tokens = 16, ) print ( response. output_text ) Vollbildmodus aktivieren Vollbildmodus beenden Der Wert max_output_tokens=16 ist bewusst gewählt. Für die erste Anfrage möchte man den kleinstmöglichen Aufruf, der belegt, dass Authentifizierung, Modellzugriff und Abrechnung funktionieren.
Dies ist der Teil, an dem "kompatibel" einen schmerzen kann. Eine einzige Basis-URL bedeutet nicht, dass jedes Modell jede Anfrageform akzeptiert. Auf APIrove gilt, wie dokumentiert:
Wenn Ihre Anwendung also auf der Responses API aufbaut und Sie ein Grok-Modell ausprobieren möchten, stellen Sie diesen Aufruf auf Chat Completions um:
chat = client. chat. completions. create ( model = "grok-4.7", messages = [ { "role": "user", "content": "Summarize this changelog in one line." } ], max_tokens = 64, ) print ( chat. choices [ 0 ]. message. content ) Vollbildmodus aktivieren Vollbildmodus beenden Derselbe Client, derselbe Schlüssel, anderer Endpunkt. Prüfen Sie vor der Integration die Modellseite auf die unterstützten Endpunkte. Das ist billiger als das Debuggen eines 403-Fehlers.
Die Bildgenerierung verwendet den standardmäßigen Images-API-Aufruf:
image = client. images. generate ( model = "gpt-image-2", prompt = "A red ceramic cup on a pale desk, soft daylight, uncluttered composition", size = "1024x1024", n = 1, ) Vollbildmodus aktivieren Vollbildmodus beenden Ein Detail, das man kennen sollte: Wenn Sie model bei einer Bildanfrage weglassen, wird ein Standardmodell ausgewählt (derzeit gpt-image-2.6-flare ). Wenn Sie reproduzierbare Ausgaben aus einem Skript wünschen, setzen Sie die Modell-ID immer explizit.
Bei der Videogenerierung brauchen die meisten Integrationen echte Änderungen, weil sie asynchron ist. Der Ablauf lautet: erstellen, abfragen, herunterladen:
# 1. Create a task curl https://apirove.com/v1/videos \ -H "Authorization: Bearer $APIROVE_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "grok-imagine-video", "prompt": "A small sailboat crossing calm water at sunrise, steady wide shot", "seconds": "5", "resolution": "720p"}' # 2. Check status with the returned id curl https://apirove.com/v1/videos/ $VIDEO_ID \ -H "Authorization: Bearer $APIROVE_API_KEY" # 3. Download the MP4 once status is "completed" curl -L https://apirove.com/v1/videos/ $VIDEO_ID /content \ -H "Authorization: Bearer $APIROVE_API_KEY" \ -o video.mp4 Vollbildmodus aktivieren Vollbildmodus beenden Aus Python heraus hat das SDK keinen eigenen Helper für diesen Endpunkt, aber die generische post-Methode funktioniert:
task = client. post ( "/videos", cast_to = object, body = { "model": "grok-imagine-video", "prompt": "A small sailboat crossing calm water at sunrise, steady wide shot", "seconds": "5", "resolution": "720p", }, ) print ( task ) # keep the returned id, then poll until completed Vollbildmodus aktivieren Vollbildmodus beenden Die aktuell angebotenen Videomodelle (Seedance 2.0, Grok Imagine Video und Wan 3.0) folgen alle demselben Muster aus Erstellen, Abfragen und Herunterladen. Sobald Ihr Job-Runner für ein Modell funktioniert, ist der Wechsel zu einem anderen im Wesentlichen eine Frage der Anpassung von model, seconds und resolution an die Werte, die die Modellseite des jeweiligen Modells angibt.
Ein praktischer Tipp: Speichern Sie die Task-ID, sobald Sie sie erhalten. Wenn Ihr Worker während des Abfragens abstürzt, können Sie das Abfragen fortsetzen, statt einen (und damit bezahlten) zweiten Clip zu erzeugen.
Eines, das leicht übersehen wird: Schlüssel gibt es in zwei Gruppen. Ein Schlüssel der Gruppe Text (der Standard für neue Schlüssel) deckt GPT-Textmodelle und Bildgenerierung ab. Claude und Video erfordern einen Schlüssel der Gruppe Full. Wenn ein Claude- oder Videoaufruf trotz ansonsten gültigem Schlüssel ein 403 zurückgibt, prüfen Sie zuerst die Schlüsselgruppe.
Die Dokumentation enthält eine kurze Tabelle zur Fehlerbehebung, die auf die meisten Gateways zutrifft:
Der letzte Punkt ist wichtiger, als er aussieht. Bei Streaming-Aufrufen und Video-Jobs bedeutet ein Verbindungsabbruch nicht zwingend, dass die Arbeit nicht erledigt wurde. Ein blindes Wiederholen kann bedeuten, dass man zweimal zahlt.
Da die Kosten meist die erste Frage sind: Die Nutzung wird von einem Prepaid-Guthaben zu veröffentlichten USD-Preisen pro Modell abgezogen. Text wird pro 1M Tokens abgerechnet (Eingabe und Ausgabe getrennt, mit Cache-Preisen bei einigen Modellen), Bilder pro Bild nach Größenstufe und Video pro Sekunde nach Auflösung. Es gibt kein Abo und keine monatliche Gebühr. Die Dokumentation empfiehlt, eine winzige Anfrage zu senden und die tatsächliche Berechnung auf der Nutzungsseite zu prüfen, bevor man irgendetwas im großen Maßstab schätzt, was für jeden Anbieter ein guter Rat ist.
Das allgemeine Rezept lautet: Behalten Sie Ihr SDK, ändern Sie die base_url und prüfen Sie dann pro Modell drei Dinge: welchen Endpunkt es spricht, ob Ihr Schlüssel Zugriff hat und ob die Ausgabe synchron ist oder ein Job. Wenn das stimmt, ist das Hinzufügen eines neuen Modells eine Konfigurationsänderung statt einer neuen Integration.
Die vollständige Endpunkt-Referenz sowie Einrichtungsanleitungen für Cursor, Claude Code, Codex, Cline und andere finden Sie in der APIrove-API-Dokumentation. Wenn Sie auf weitere Fälle von "kompatibel, aber nicht ganz" mit OpenAI-artigen Gateways gestoßen sind, würde ich gern in den Kommentaren davon hören.
Automatisch übersetzt. Das Original auf Englisch ist unten aufklappbar.
Original (Englisch) anzeigen
Disclosure: I work on APIrove, the API platform used in the examples below. The patterns themselves (custom base_url, separate endpoints per output type, async video jobs) apply to most OpenAI-compatible providers, so I've tried to keep this useful even if you never use it.
Most AI side projects start the same way: one SDK, one vendor, one key. Then a feature needs an image model, someone wants to try a different text model, and a video experiment shows up. Suddenly you have three clients, three auth styles and three billing dashboards.
One way to keep this manageable is to keep the OpenAI SDK you already use and change only the base_url. This post walks through doing that against one base URL that serves text, image and video models, and calls out the places where "OpenAI-compatible" still needs a careful look.
export APIROVE_API_KEY = 'YOUR_API_KEY' export APIROVE_MODEL = 'YOUR_MODEL_ID' Enter fullscreen mode Exit fullscreen mode Step 2: Change one line in the client With the official Python SDK ( pip install openai ), the only change is the base_url. Note that it ends at /v1; the SDK appends the endpoint path itself.
import os from openai import OpenAI client = OpenAI ( api_key = os. environ [ "APIROVE_API_KEY" ], base_url = "https://apirove.com/v1", ) response = client. responses. create ( model = os. environ [ "APIROVE_MODEL" ], input = "Hello!", max_output_tokens = 16, ) print ( response. output_text ) Enter fullscreen mode Exit fullscreen mode The max_output_tokens=16 is deliberate. For a first request you want the smallest possible call that proves auth, model access and billing all work.
This is the part where "compatible" can bite you. A single base URL does not mean every model accepts every request shape. On APIrove, as documented:
So if your app is built on the Responses API and you want to try a Grok model, switch that call to Chat Completions:
chat = client. chat. completions. create ( model = "grok-4.7", messages = [ { "role": "user", "content": "Summarize this changelog in one line." } ], max_tokens = 64, ) print ( chat. choices [ 0 ]. message. content ) Enter fullscreen mode Exit fullscreen mode Same client, same key, different endpoint. Check the model's page for its supported endpoint before wiring it in. That's cheaper than debugging a 403.
Image generation uses the standard Images API call:
image = client. images. generate ( model = "gpt-image-2", prompt = "A red ceramic cup on a pale desk, soft daylight, uncluttered composition", size = "1024x1024", n = 1, ) Enter fullscreen mode Exit fullscreen mode One detail worth knowing: if you omit model on an image request, a default model is selected (currently gpt-image-2.6-flare ). If you want reproducible output from a script, always set the model ID explicitly.
Video generation is where most integrations need real changes, because it is asynchronous. The flow is create, poll, download:
# 1. Create a task curl https://apirove.com/v1/videos \ -H "Authorization: Bearer $APIROVE_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model": "grok-imagine-video", "prompt": "A small sailboat crossing calm water at sunrise, steady wide shot", "seconds": "5", "resolution": "720p"}' # 2. Check status with the returned id curl https://apirove.com/v1/videos/ $VIDEO_ID \ -H "Authorization: Bearer $APIROVE_API_KEY" # 3. Download the MP4 once status is "completed" curl -L https://apirove.com/v1/videos/ $VIDEO_ID /content \ -H "Authorization: Bearer $APIROVE_API_KEY" \ -o video.mp4 Enter fullscreen mode Exit fullscreen mode From Python, the SDK has no dedicated helper for this endpoint, but the generic post method works:
task = client. post ( "/videos", cast_to = object, body = { "model": "grok-imagine-video", "prompt": "A small sailboat crossing calm water at sunrise, steady wide shot", "seconds": "5", "resolution": "720p", }, ) print ( task ) # keep the returned id, then poll until completed Enter fullscreen mode Exit fullscreen mode The video models on sale (Seedance 2.0, Grok Imagine Video and Wan 3.0) all follow this same create/poll/download pattern, so once your job runner works for one, swapping models is mostly a matter of changing model, seconds and resolution to values that model's page lists.
A practical tip: store the task ID as soon as you get it. If your worker crashes mid-poll, you can resume polling instead of creating (and paying for) a second clip.
One thing that's easy to miss: keys come in two groups. A Text group key (the default for new keys) covers GPT text models and image generation. Claude and video need a Full group key. If a Claude or video call returns 403 with an otherwise valid key, check the key group first.
The docs list a short troubleshooting table that applies to most gateways:
That last point matters more than it looks. For streaming calls and video jobs, a disconnect doesn't necessarily mean the work wasn't done. Retrying blindly can mean paying twice.
Since cost is usually the first question: usage is deducted from a prepaid balance at published per-model USD rates. Text is priced per 1M tokens (input and output separately, with cache rates on some models), images per image by size tier, and video per second by resolution. There's no subscription or monthly fee. The docs suggest sending one tiny request and reading the actual charge on the usage page before estimating anything at scale, which is good advice for any provider.
The general recipe is: keep your SDK, change base_url, then check three things per model: which endpoint it speaks, whether your key has access, and whether the output is synchronous or a job. Get those right and adding a new model is a config change rather than a new integration.
The full endpoint reference, plus setup guides for Cursor, Claude Code, Codex, Cline and others, is in the APIrove API documentation. If you've hit other "compatible but not quite" edge cases with OpenAI-style gateways, I'd like to hear about them in the comments.