Error with Responses API through AzureOpenAI

Hey everyone,

I seem to have an error working with the responses API while my setup works for the completions API with my API key.

More specifically my set up is like this:

client = AzureOpenAI(
    api_key=os.getenv("OPENAI_API_KEY"),
    api_version="2025-04-01-preview",
    azure_endpoint="https://swedencentral.api.cognitive.microsoft.com/",
)

response = client.responses.create(
    model="o4-mini",
    instructions="You are a helpful assistant.",
    input="What is the capital of France?",
)

Now I have the same issue with swapping the model out for gpt-4.1 and api_version 2025-03-01-preview or later

The models are deployed as can be seen here (on Sweden central ofc)

openai.BadRequestError: Error code: 400 - {‘error’: {‘message’: ‘There was an issue with your request. Please check your inputs and try again’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

Welcome to the dev forum @Juembles

There’s no such model as this one available on Azure OpenAI service.

Are you trying o4-mini or gpt-4o-mini?

Oh sorry quick copy paste change error. It is supposed to be o4-mini and gpt-4.1 respectively. Editing my originaly post to clarify

Looking at your code, you are using the AzureOpenAI client but passing it your OpenAI API key… Azure OpenAI models need to be commissioned prior to use in the AI Foundry/ Azure Portal and have a different access mechanism, if you are trying to use your OpenAI API key with the OpenAI API, simply use:

from openai import OpenAI

client = OpenAI()

response = client.responses.create(
  model="gpt-4.1",
  input="Tell me a three sentence bedtime story about a unicorn."
)

print(response)

With your OPENAI_API_KEY environment varible set to a valid OpenAI key, if however you are trying to use the Azure OpenAI Services model system then use

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

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

client = AzureOpenAI(  
  base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",  
  azure_ad_token_provider=token_provider,
  api_version="preview"
)

response = client.responses.create(
    model="gpt-4.1-nano",
    input= "This is a test" 
)

print(response.model_dump_json(indent=2)) 

or

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

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

client = AzureOpenAI(  
  base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",  
  azure_ad_token_provider=token_provider,
  api_version="preview"
)

response = client.responses.create(
    model="gpt-4.1-nano",
    input= "This is a test" 
)

print(response.model_dump_json(indent=2)) 

See: Azure OpenAI Responses API - Azure OpenAI | Microsoft Learn