Switch AI Models Without Rewriting Your OpenAI SDK Integration

작성자

카테고리:

← 피드로
DEV Community · RouterBase · 2026-07-16 개발(SW)

RouterBase profile image RouterBase

If your application already uses the OpenAI Python SDK, you can keep the same client and point it at RouterBase by changing the base URL.

This gives the application one request interface while model selection stays configurable.

Prerequisites

  • Python 3.9 or newer
  • The current openai package
  • A RouterBase API key stored in an environment variable

Install the SDK:

pip install --upgrade openai

Enter fullscreen mode Exit fullscreen mode

Set the key in your shell:

export ROUTERBASE_API_KEY="sk-rb-..."

Enter fullscreen mode Exit fullscreen mode

Do not place an API key in browser code, mobile binaries, or a public repository.

Create the client

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["ROUTERBASE_API_KEY"],
    base_url="https://routerbase.com/v1",
)

Enter fullscreen mode Exit fullscreen mode

The important line is base_url. The SDK remains the same; requests go through the RouterBase OpenAI-compatible endpoint.

Make a chat request

Choose a model ID from the current RouterBase model catalog and keep it in configuration instead of hard-coding it throughout the application.

import os

model_id = os.environ.get(
    "ROUTERBASE_MODEL",
    "google/gemini-2.5-flash",
)

response = client.chat.completions.create(
    model=model_id,
    messages=[
        {
            "role": "user",
            "content": "Explain idempotency in one paragraph.",
        }
    ],
)

print(response.choices[0].message.content)

Enter fullscreen mode Exit fullscreen mode

To test a different compatible chat model, change ROUTERBASE_MODEL and run the same request again.

Add a small evaluation before switching

A successful API response is not enough to justify moving production traffic. Test candidate models against the same tasks and rubric.

candidates = [
    "google/gemini-2.5-flash",
    # Add other current RouterBase chat model IDs here.
]

prompt = "Return a JSON object with keys: summary and risk."

for candidate in candidates:
    result = client.chat.completions.create(
        model=candidate,
        messages=[{"role": "user", "content": prompt}],
    )
    print(candidate, result.choices[0].message.content)

Enter fullscreen mode Exit fullscreen mode

In a real evaluation, record correctness, latency, retry rate, and the cost of a successful task.

Important caveats

An OpenAI-compatible endpoint does not mean every model has identical behavior.

Before switching models, verify:

  • tool and function calling behavior;
  • structured output requirements;
  • supported input modalities;
  • context and output limits;
  • streaming behavior;
  • model-specific parameters.

Keep the integration stable, but keep the evaluation model-specific.

RouterBase provides one OpenAI-compatible API for GPT, Claude, Gemini, and 200+ AI models. Check the live catalog before using a model ID in production.

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다