Skip to content
GuidesJuly 12, 2026

Use the OpenAI SDK with any model

The official openai package for Python and TypeScript doesn't have to mean OpenAI models. Override base_url and api_key, keep every other call the same, and address 170+ models across 28+ providers as provider/model strings — including OpenAI's own.

Can I use the OpenAI SDK with models that aren't OpenAI's?

Yes. The openai package, in Python or JS/TS, only needs two things overridden to talk to a different backend: base_url and api_key. Everything else — client.chat.completions.create, the streaming shape, the message format — is the same call whether the model behind it is GPT, Claude, Gemini, or something else entirely.

AnyRouter implements that same OpenAI-compatible contract in front of 170+ models across 28+ providers, so the SDK you already have installed becomes a universal client instead of an OpenAI-only one.

One client, either language

Only two values change — the base URL and the key. Model choice moves into the model string itself:

from openai import OpenAI

client = OpenAI(
    base_url="https://anyrouter.dev/api/v1",
    api_key="sk-ar-v1-...",
)

resp = client.chat.completions.create(
    model="anthropic/claude-opus-4-8",
    messages=[{"role": "user", "content": "Explain base_url in one sentence"}],
)
print(resp.choices[0].message.content)

Both snippets are the unmodified openai and openai-node packages — nothing installed beyond what an OpenAI integration already needs.

Streaming and tool calls carry over unchanged

Because the contract is the standard Chat Completions API, the parts of your code that already handle it don't need touching. Set stream=True and read deltas exactly as you would against OpenAI directly; function/tool-calling definitions pass through the same way.

  • Streaming — stream=True (Python) or stream: true (TypeScript), same delta shape.
  • Tool calling — the same tools array and tool_choice parameter.
  • Other dialects on the same key — /messages for Anthropic-style clients, /responses for the newer Responses API.

Change the model, not the code

Because every model is addressed as a provider/model string, switching what a request runs on is a config value:

Model idOwner
openai/gpt-5.5OpenAI
anthropic/claude-opus-4-8Anthropic
google/gemini-2.5-proGoogle
z-ai/glm-5.2Z.ai
AnyRouter model page listing a model's provider, context length, and pricing
Every model id resolves to its own page with specs and upstream provider.

No lock-in either direction

Since this is the real OpenAI API contract and not a proprietary wrapper, the same code points straight back at api.openai.com if you ever want to leave — nothing about the call shape is AnyRouter-specific. For the full catalog and failover behavior behind the endpoint, see /blog/one-api-every-llm.

Keep the OpenAI SDK, reach 170+ models across 28+ providers.

Get a key free

Route your first request in 2 minutes

Start free with your own keys, or top up and pay per token. Get $4/mo in credits and free models on Go — $2/mo, or free when you donate a provider key.

Start free