How to Switch Between Azure OpenAI, OpenAI API, and GitHub Copilot Models

You can switch between Azure OpenAI, OpenAI API, and GitHub Copilot Models by changing the client library import, updating environment variables, and adjusting the endpoint configuration while keeping the core chat completion logic identical.

The microsoft/generative-ai-for-beginners repository provides isolated Python implementations in 06-text-generation-apps/python/ that demonstrate exactly how to migrate between these three providers. Each example follows the same pattern—load credentials with python-dotenv, instantiate a provider-specific client, and execute a chat completion request—making provider switching a matter of updating imports and configuration rather than rewriting application logic.

OpenAI API Implementation

The standard OpenAI implementation in 06-text-generation-apps/python/oai-app.py uses the openai library with automatic API key detection.

from openai import OpenAI
import os
from dotenv import load_dotenv

load_dotenv()
client = OpenAI()
deployment = "gpt-3.5-turbo"

messages = [{"role": "user", "content": "Complete the following: Once upon a time there was a"}]
completion = client.chat.completions.create(
    model=deployment, 
    messages=messages
)

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

This implementation requires only the OPENAI_API_KEY environment variable, which the OpenAI() constructor reads automatically from the environment.

Azure OpenAI Service Configuration

To switch to Azure OpenAI, use the code from 06-text-generation-apps/python/aoai-app.py, which requires explicit endpoint configuration using the AzureOpenAI class.

from openai import AzureOpenAI
import os
from dotenv import load_dotenv

load_dotenv()
client = AzureOpenAI(
    azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
    api_key=os.environ["AZURE_OPENAI_API_KEY"],
    api_version="2024-02-01"
)

deployment = os.environ["AZURE_OPENAI_DEPLOYMENT"]
messages = [{"role": "user", "content": "Complete the following: Once upon a time there was a"}]
completion = client.chat.completions.create(
    model=deployment, 
    messages=messages
)

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

Switching from OpenAI to Azure OpenAI requires changing the client import from OpenAI to AzureOpenAI, adding the azure_endpoint and api_version parameters, and updating the environment variables to include AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, and AZURE_OPENAI_DEPLOYMENT.

GitHub Copilot Models Setup

For GitHub Copilot Models, the repository uses the Azure AI Inference SDK as implemented in 06-text-generation-apps/python/githubmodels-app.py.

import os
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import SystemMessage, UserMessage
from azure.core.credentials import AzureKeyCredential
from dotenv import load_dotenv

load_dotenv()
token = os.environ["GITHUB_TOKEN"]
endpoint = "https://models.inference.ai.azure.com"
model_name = "gpt-4o"

client = ChatCompletionsClient(
    endpoint=endpoint,
    credential=AzureKeyCredential(token),
)

response = client.complete(
    messages=[
        SystemMessage(content="You are a helpful assistant."),
        UserMessage(content="Show me 5 recipes for a dish with chicken, potatoes, and carrots."),
    ],
    model=model_name,
    temperature=1.0,
    max_tokens=1000,
)

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

Switching to GitHub Copilot Models requires installing the azure-ai-inference package, importing ChatCompletionsClient from azure.ai.inference, and using AzureKeyCredential for authentication with your GITHUB_TOKEN.

Environment Variable Management

All three implementations in the microsoft/generative-ai-for-beginners repository rely on python-dotenv to load credentials from a .env file. The repository includes a .env.copy template that you duplicate and populate according to your active provider.

Provider Required Variables Source File
OpenAI OPENAI_API_KEY 06-text-generation-apps/python/oai-app.py
Azure OpenAI AZURE_OPENAI_ENDPOINT, AZURE_OPENAI_API_KEY, AZURE_OPENAI_DEPLOYMENT 06-text-generation-apps/python/aoai-app.py
GitHub Copilot Models GITHUB_TOKEN 06-text-generation-apps/python/githubmodels-app.py

The shared/python/env_utils.py module provides validation helpers to ensure required variables are present before client initialization, preventing runtime authentication errors.

Step-by-Step Provider Migration

To switch between Azure OpenAI, OpenAI API, and GitHub Copilot Models in your application:

  1. Install the appropriate client library:

    • OpenAI/Azure: pip install openai
    • GitHub Models: pip install azure-ai-inference
  2. Update the client import and instantiation using the code blocks from the specific provider sections above.

  3. Configure the model identifier:

    • OpenAI: Use model names like "gpt-3.5-turbo"
    • Azure: Use your deployment name from the Azure portal
    • GitHub: Use model names like "gpt-4o" or "gpt-4o-mini"
  4. Populate environment variables in your .env file for the selected provider.

Because all three clients use similar chat.completions.create() or complete() patterns with message arrays, your core prompting logic remains identical across providers.

Summary

  • The microsoft/generative-ai-for-beginners repository provides isolated examples in 06-text-generation-apps/python/ demonstrating how to switch between Azure OpenAI, OpenAI API, and GitHub Copilot Models.
  • OpenAI uses the openai.OpenAI client with OPENAI_API_KEY and standard model names.
  • Azure OpenAI requires openai.AzureOpenAI with explicit azure_endpoint, api_version, and deployment-specific environment variables.
  • GitHub Copilot Models utilize azure.ai.inference.ChatCompletionsClient with GITHUB_TOKEN and the Azure AI Inference endpoint.
  • All implementations use python-dotenv for credential management, allowing you to switch providers by updating imports, client instantiation, and environment variables while maintaining consistent chat completion logic.

Frequently Asked Questions

Can I use the same prompt code for all three providers?

Yes. The chat completion interface accepts the same messages array format across OpenAI, Azure OpenAI, and GitHub Copilot Models. You only need to change the client initialization and model identifier; the prompting logic and message structure remain identical.

What is the difference between the OpenAI and Azure OpenAI client libraries?

Both use the openai Python package, but Azure OpenAI requires the AzureOpenAI class with explicit azure_endpoint and api_version parameters, while the standard OpenAI client uses OpenAI() which only requires the API key. The Azure client routes requests to your private Azure endpoint rather than OpenAI's public API.

Do I need separate environment files for each provider?

No. You can maintain a single .env file and populate the variables for your active provider. The scripts in microsoft/generative-ai-for-beginners use python-dotenv to load whichever variables are present. For cleaner separation, you can create .env.openai, .env.azure, and .env.github and symlink or copy them to .env as needed.

Which models are available through GitHub Copilot Models?

GitHub Copilot Models provides access to models like gpt-4o, gpt-4o-mini, and others through the Azure AI Inference endpoint at https://models.inference.ai.azure.com. The available models may vary based on your GitHub account permissions and Copilot subscription tier.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →