<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: APIrove</title>
    <description>The latest articles on DEV Community by APIrove (@apirove).</description>
    <link>https://dev.to/apirove</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4262221%2F3693c915-9f80-4a96-b2d8-2cf1808baab3.png</url>
      <title>DEV Community: APIrove</title>
      <link>https://dev.to/apirove</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/apirove"/>
    <language>en</language>
    <item>
      <title>One OpenAI SDK client for text, image and video models: a practical walkthrough</title>
      <dc:creator>APIrove</dc:creator>
      <pubDate>Tue, 24 Nov 2026 09:15:58 +0000</pubDate>
      <link>https://dev.to/apirove/one-openai-sdk-client-for-text-image-and-video-models-a-practical-walkthrough-18pm</link>
      <guid>https://dev.to/apirove/one-openai-sdk-client-for-text-image-and-video-models-a-practical-walkthrough-18pm</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Disclosure:&lt;/strong&gt; I work on &lt;a href="https://apirove.com/en/" rel="noopener noreferrer"&gt;APIrove&lt;/a&gt;, the API platform used in the examples below. The patterns themselves (custom &lt;code&gt;base_url&lt;/code&gt;, 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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;One way to keep this manageable is to keep the OpenAI SDK you already use and change only the &lt;code&gt;base_url&lt;/code&gt;. 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Keep the key out of your code
&lt;/h2&gt;

&lt;p&gt;Before touching the client, put the key and model ID in environment variables. The key should only live on your server, never in browser JavaScript, a mobile app bundle or a public repo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;APIROVE_API_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'YOUR_API_KEY'&lt;/span&gt;
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;APIROVE_MODEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'YOUR_MODEL_ID'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Change one line in the client
&lt;/h2&gt;

&lt;p&gt;With the official Python SDK (&lt;code&gt;pip install openai&lt;/code&gt;), the only change is the &lt;code&gt;base_url&lt;/code&gt;. Note that it ends at &lt;code&gt;/v1&lt;/code&gt;; the SDK appends the endpoint path itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;

&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OpenAI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;api_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"APIROVE_API_KEY"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;base_url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"https://apirove.com/v1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;responses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"APIROVE_MODEL"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;input&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Hello!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_output_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;output_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;max_output_tokens=16&lt;/code&gt; is deliberate. For a first request you want the smallest possible call that proves auth, model access and billing all work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Know which endpoint each model speaks
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GPT text models&lt;/strong&gt;: &lt;code&gt;/v1/responses&lt;/code&gt; and &lt;code&gt;/v1/chat/completions&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grok text models&lt;/strong&gt;: &lt;code&gt;/v1/chat/completions&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Claude models&lt;/strong&gt;: &lt;code&gt;/v1/messages&lt;/code&gt; (the Anthropic-style endpoint that Claude Code expects)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image models&lt;/strong&gt;: &lt;code&gt;/v1/images/generations&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Video models&lt;/strong&gt;: &lt;code&gt;/v1/videos&lt;/code&gt;, asynchronous&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So if your app is built on the Responses API and you want to try a Grok model, switch that call to Chat Completions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;chat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;completions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"grok-4.7"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;messages&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"role"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"user"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Summarize this changelog in one line."&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;max_tokens&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;choices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Images through the same client
&lt;/h2&gt;

&lt;p&gt;Image generation uses the standard Images API call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;images&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"gpt-image-2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;prompt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"A red ceramic cup on a pale desk, soft daylight, uncluttered composition"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"1024x1024"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One detail worth knowing: if you omit &lt;code&gt;model&lt;/code&gt; on an image request, a default model is selected (currently &lt;code&gt;gpt-image-2.6-flare&lt;/code&gt;). If you want reproducible output from a script, always set the model ID explicitly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Video is a job, not a request
&lt;/h2&gt;

&lt;p&gt;Video generation is where most integrations need real changes, because it is asynchronous. The flow is create, poll, download:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Create a task&lt;/span&gt;
curl https://apirove.com/v1/videos \
  -H &lt;span class="s2"&gt;"Authorization: Bearer $APIROVE_API_KEY"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{"model": "grok-imagine-video", "prompt": "A small sailboat crossing calm water at sunrise, steady wide shot", "seconds": "5", "resolution": "720p"}'&lt;/span&gt;

&lt;span class="c"&gt;# 2. Check status with the returned id&lt;/span&gt;
curl https://apirove.com/v1/videos/&lt;span class="nv"&gt;$VIDEO_ID&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Authorization: Bearer $APIROVE_API_KEY"&lt;/span&gt;

&lt;span class="c"&gt;# 3. Download the MP4 once status is "completed"&lt;/span&gt;
curl -L https://apirove.com/v1/videos/&lt;span class="nv"&gt;$VIDEO_ID&lt;/span&gt;/content \
  -H &lt;span class="s2"&gt;"Authorization: Bearer $APIROVE_API_KEY"&lt;/span&gt; \
  -o video.mp4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From Python, the SDK has no dedicated helper for this endpoint, but the generic &lt;code&gt;post&lt;/code&gt; method works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"/videos"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cast_to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"model"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"grok-imagine-video"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"prompt"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"A small sailboat crossing calm water at sunrise, steady wide shot"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"seconds"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"resolution"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"720p"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# keep the returned id, then poll until completed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;model&lt;/code&gt;, &lt;code&gt;seconds&lt;/code&gt; and &lt;code&gt;resolution&lt;/code&gt; to values that model's page lists.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Key groups and permissions
&lt;/h2&gt;

&lt;p&gt;One thing that's easy to miss: keys come in two groups. A &lt;strong&gt;Text group&lt;/strong&gt; key (the default for new keys) covers GPT text models and image generation. Claude and video need a &lt;strong&gt;Full group&lt;/strong&gt; key. If a Claude or video call returns 403 with an otherwise valid key, check the key group first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Errors and retries
&lt;/h2&gt;

&lt;p&gt;The docs list a short troubleshooting table that applies to most gateways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;401&lt;/strong&gt;: wrong, revoked or malformed key. Check the &lt;code&gt;Authorization: Bearer&lt;/code&gt; header for stray whitespace.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;403&lt;/strong&gt;: account status, model access, or the model doesn't support that endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;429&lt;/strong&gt;: slow down and back off; don't hammer retries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5xx&lt;/strong&gt;: record the request ID if present, and confirm whether the original request completed (and was billed) before retrying.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Billing model, briefly
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;The general recipe is: keep your SDK, change &lt;code&gt;base_url&lt;/code&gt;, 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.&lt;/p&gt;

&lt;p&gt;The full endpoint reference, plus setup guides for Cursor, Claude Code, Codex, Cline and others, is in the &lt;a href="https://apirove.com/en/docs/" rel="noopener noreferrer"&gt;APIrove API documentation&lt;/a&gt;. 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.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>api</category>
      <category>openai</category>
      <category>python</category>
    </item>
  </channel>
</rss>
