Agents SDK with Azure hosted models

I am using some instances of gpt4o and some embedding models deployed in azure because I have credits.

I would like to use the agents-sdk.

Is there a way to do it?

Hey! I figured this out — the key is to set up the AsyncAzureOpenAI client yourself and pass it explicitly to the Agent via the openai_client parameter.

Here’s a working example using the Azure OpenAI endpoint with the agent-sdk:

import os
import asyncio
from openai import AsyncAzureOpenAI
from openai import OpenAIError
from agents import Agent, Runner, OpenAIChatCompletionsModel, set_tracing_disabled

# Disable tracing since we're using Azure OpenAI
set_tracing_disabled(disabled=True)

deployment = "gpt-4o"

async def main():
    try:
        # Create the Async Azure OpenAI client
        client = AsyncAzureOpenAI(
            api_key=os.getenv("AZURE_OPENAI_KEY"),
            api_version="2023-09-01-preview",
            azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
        )

        # Configure the agent with Azure OpenAI
        agent = Agent(
            name="Assistant",
            instructions="You are a helpful assistant",
            model=OpenAIChatCompletionsModel(
                model=deployment,
                openai_client=client,
            )
        )

        result = await Runner.run(agent, "Write a haiku about recursion in programming.")
        print(result.final_output)

    except OpenAIError as e:
        print(f"OpenAI API Error: {str(e)}")
    except Exception as e:
        print(f"An unexpected error occurred: {str(e)}")

if __name__ == "__main__":
    asyncio.run(main())

Thanks man!
I tried it and in worked!

1 Like

@pragalvhasharma thanks, however I’ve been trying to use the WebSearchTool with agent sdk and Azure hosted open ai models with this approach and running into an error - Hosted tools are not supported with the ChatCompletions API

Any pointers to resolve this would be appreciated. TIA.

Hey,


Currently the Web Search tool is not supported on azure. I recommend making a custom function and passing that onto the model for google searches.

Sincerely,
Prag

1 Like