How to switch between OpenAI and Azure OpenAI endpoints with Python

While OpenAI and Azure OpenAI Service rely on a common Python client library, there are small changes you need to make to your code in order to swap back and forth between endpoints. This article walks you through the common changes and differences you'll experience when working across OpenAI and Azure OpenAI.

This article only shows examples with the new OpenAI Python 1.x API library. For information on migrating from 0.28.1 to 1.x refer to our migration guide.

Authentication

We recommend using environment variables. If you haven't done this before our Python quickstarts walk you through this configuration.

API key

OpenAI Azure OpenAI
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY']  
)



import os
from openai import AzureOpenAI
    
client = AzureOpenAI(
    api_key=os.getenv("AZURE_OPENAI_KEY"),  
    api_version="2023-10-01-preview",
    azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
)

Microsoft Entra authentication

OpenAI Azure OpenAI
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY']  
)








from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from openai import AzureOpenAI

token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")

api_version = "2023-12-01-preview"
endpoint = "https://my-resource.openai.azure.com"

client = AzureOpenAI(
    api_version=api_version,
    azure_endpoint=endpoint,
    azure_ad_token_provider=token_provider,
)

Keyword argument for model

OpenAI uses the model keyword argument to specify what model to use. Azure OpenAI has the concept of unique model deployments. When using Azure OpenAI model should refer to the underling deployment name you chose when you deployed the model.

OpenAI Azure OpenAI
completion = client.completions.create(
    model='gpt-3.5-turbo-instruct',
    prompt="<prompt>")
)

chat_completion = client.chat.completions.create(
    model="gpt-4",
    messages="<messages>"
)

embedding = client.embeddings.create(
    input="<input>",
    model="text-embedding-ada-002"
)
completion = client.completions.create(
    model=gpt-35-turbo-instruct, # This must match the custom deployment name you chose for your model.
    prompt=<"prompt">
)

chat_completion = client.chat.completions.create(
    model="gpt-35-turbo", # model = "deployment_name".
    messages=<"messages">
)

embedding = client.embeddings.create(
    input = "<input>",
    model= "text-embedding-ada-002" # model = "deployment_name".
)

Azure OpenAI embeddings multiple input support

OpenAI currently allows a larger number of array inputs with text-embedding-ada-002. Azure OpenAI currently supports input arrays up to 16 for text-embedding-ada-002 Version 2. Both require the max input token limit per API request to remain under 8191 for this model.

OpenAI Azure OpenAI
inputs = ["A", "B", "C"] 

embedding = client.embeddings.create(
  input=inputs,
  model="text-embedding-ada-002"
)


inputs = ["A", "B", "C"] #max array size=16

embedding = client.embeddings.create(
  input=inputs,
  model="text-embedding-ada-002" # This must match the custom deployment name you chose for your model.
  #engine="text-embedding-ada-002"
)

Next steps