Forum Discussion
Azure OpenAI - GPT-4.1 + tools/image_generation doesn't work
Hi,
response = client.responses.create(model='gpt-4.1', input=prompt, tools=[{'type': 'image_generation'}])
When used with Azure/OpenAI it fails with:
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}}
It works fine with OpenAI direct.
1 Reply
Hi RobQ0,
Please keep in mind that when you call tools=[{"type":"image_generation"}] in Azure’s Responses API, you must tell Azure OpenAI which image model deployment to use, otherwise it doesn’t know where to send your DALL·E-style request and returns a generic 400.
However, You can deploy the GPT-image-1 model In the Azure PortalOR, Instantiate your client with the image‐deployment header When you create your AzureOpenAI client.
Add a default header set to your deployment name
x-ms-oai-image-generation-deploymentThen, use the preview API version:
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>.openai.azure.com/openai/v1/", azure_ad_token_provider=token_provider, api_version="preview", default_headers={ "x-ms-oai-image-generation-deployment": "gpt-image-1-deployment" } )This header routes any image_generation tool calls to your DALL·E deployment
Invoke the image tool
response = client.responses.create( model="gpt-4.1", input="Generate an image of a golden retriever puppy playing in autumn leaves", tools=[{"type": "image_generation"}] ) # extract the Base64-encoded image from the response image_b64 = next( out.result for out in response.output if out.type == "image_generation_call" ) with open("puppy.png", "wb") as f: f.write(base64.b64decode(image_b64)) ``` :contentReference[oaicite:1]{index=1}Make sure you’re in a supported region The Responses API (and image tool) is in preview in select regions, check that your resource is in one of: eastus, westus, francecentral, etc.
With those steps, deploying gpt-image-1, adding the header, and using api_version="preview"—your tools=[{"type":"image_generation"}] calls will work just like they do against OpenAI’s platform.