sdk-base-url-switch · EN · 2026-11-05

Pointing an Existing OpenAI SDK at the Aggregator: Base URL Override and API Key Swap Without Touching Call Sites

Learn how to point an existing OpenAI SDK project at an aggregator with just two changes—base URL and API key—and verify that streaming and tool calls still work. This guide shows the minimal diff and testing steps.

Why switch with minimal changes

If your codebase already uses the OpenAI SDK, you don't need to rewrite calls or adopt a new client. The SDK's design lets you redirect requests by changing two configuration values: the base URL and the API key. This keeps call sites untouched and reduces migration risk.

The two values to change

Every OpenAI SDK client is initialized with an API key and an optional base URL. By default, the base URL points to OpenAI's API. To use an aggregator, you override both:

  • API key: Use the key issued by the aggregator.
  • Base URL: Set to the aggregator's API endpoint (typically something like https://api.example.com/v1).

These values are usually provided via environment variables or a configuration object. The key is to centralize them so you can swap them without editing every file.

Minimal diff example

Suppose your original initialization looks like this (Python):

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["OPENAI_API_KEY"],
    # base_url defaults to OpenAI's endpoint
)

After the switch, it becomes:

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["AGGREGATOR_API_KEY"],
    base_url=os.environ["AGGREGATOR_BASE_URL"],
)

For Node.js, the equivalent change is:

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: process.env.AGGREGATOR_API_KEY,
  baseURL: process.env.AGGREGATOR_BASE_URL,
});

That's the entire diff. All subsequent calls—client.chat.completions.create, client.embeddings.create, etc.—remain unchanged.

What about model names?

The aggregator routes to multiple model providers. Model names may differ from OpenAI's naming. Check the aggregator's documentation for the exact model identifiers to use. You may need to update the model parameter in your calls if you switch to a non-OpenAI model. However, if you're using an OpenAI model through the aggregator, the model name likely stays the same.

Verifying streaming still works

Streaming responses are a common integration point. After the switch, test a streaming call:

stream = client.chat.completions.create(
    model="gpt-4",  # or aggregator-specific model name
    messages=[{"role": "user", "content": "Say hello"}],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Check that:

  • Chunks arrive incrementally (not all at once).
  • The stream terminates cleanly.
  • Content appears as expected.

If streaming fails, verify the base URL includes the correct path (often /v1) and that your aggregator key has permissions for the model.

Verifying tool calls

Tool (function) calls involve a two-step flow: the model returns a tool call, your code executes it, and you send the result back. Test with a simple function definition:

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get current weather",
        "parameters": {
            "type": "object",
            "properties": {"location": {"type": "string"}},
            "required": ["location"]
        }
    }
}]

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=tools,
    tool_choice="auto"
)
print(response.choices[0].message.tool_calls)

Ensure the response includes a tool_calls array with the expected structure. Then simulate sending the tool result back and confirm the model continues the conversation.

Common pitfalls

  • Missing /v1 in base URL: Many aggregators expect the version path. Omitting it can cause 404s.
  • Key format: Aggregator keys may have a different prefix. Don't assume they look like OpenAI keys.
  • Model availability: Not all models are available through every aggregator. Confirm the model name exists.
  • Rate limits and quotas: Aggregators may have different limits. Check your plan.

Rollback strategy

Because the change is isolated to client initialization, rolling back is as simple as reverting the base URL and key. Keep the old values in environment variables so you can switch back quickly if needed.

Summary

Switching an existing OpenAI SDK project to an aggregator requires only two changes: base URL and API key. By centralizing these values and testing streaming and tool calls, you can migrate with confidence and without touching call sites.