Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 11, 2025

Motivation and Context

All Azure OpenAI usage (except OpenAI Assistants) should use the base OpenAIClient with BearerTokenPolicy instead of the derived AzureOpenAIClient. This aligns with the recommended authentication pattern for Azure AI services.

Description

Replaced all AzureOpenAIClient instantiations across samples, tests, and documentation with OpenAIClient using appropriate authentication:

Token-based authentication (AzureCliCredential/DefaultAzureCredential):

// Before
new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())

// After  
new OpenAIClient(
    new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"),
    new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })

Key-based authentication:

// Before
new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key))

// After
new OpenAIClient(
    new ApiKeyCredential(key),
    new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })

Files updated (67 total):

  • Agent samples (Step01-Step19)
  • AGUI samples and servers
  • MCP, Memory, RAG samples
  • Workflow samples (ConditionalEdges, Concurrent, Foundational, Observability)
  • AzureFunctions samples (01-07)
  • HostedAgents, M365Agent, Purview, DevUI, A2A, DeclarativeAgents samples
  • DurableTask integration test helper
  • README documentation (dotnet, AGUIWebChat, Purview, AzureFunctions)

Code review improvements:

  • Extracted OpenAIClientOptions into a variable in AzureFunctions samples to avoid duplication in ternary expressions
  • Added TrimEnd('/') to endpoint construction in TestHelper.cs to prevent malformed URLs with double slashes
  • Renamed misleading variable azureOpenAIClient to openAIClient in Agent_Step14_Middleware sample

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.
Original prompt

Find all usage of AzureOpenAIClient in the repo. Every use of it (in samples, in markdown, etc.), except for when used with OpenAI Assistants, should be changed to use the base OpenAIClient instead of the derived AzureOpenAIClient. That entails changing code like:

new AzureOpenAIClient(
new Uri(endpoint),
new AzureCliCredential())

to instead be e.g.

new OpenAIClient(
new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"),
new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })

Ensure you keep the same credential that was previously being used, just wrapped as appropriate in a BearerTokenPolicy. If the endpoint already includes the "/openai/v1" route, it needn't be included again.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@github-actions github-actions bot changed the title [WIP] Replace AzureOpenAIClient with OpenAIClient in repo .NET: [WIP] Replace AzureOpenAIClient with OpenAIClient in repo Dec 11, 2025
Copilot AI changed the title .NET: [WIP] Replace AzureOpenAIClient with OpenAIClient in repo Replace AzureOpenAIClient with OpenAIClient using BearerTokenPolicy Dec 11, 2025
Copilot AI requested a review from stephentoub December 11, 2025 17:15
@markwallace-microsoft markwallace-microsoft added the documentation Improvements or additions to documentation label Dec 11, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates from AzureOpenAIClient to the base OpenAIClient class with appropriate authentication policies, aligning with the recommended authentication pattern for Azure AI services. This is a non-breaking change that standardizes the authentication approach across the codebase.

Key Changes:

  • Token-based authentication now uses OpenAIClient with BearerTokenPolicy wrapping AzureCliCredential or DefaultAzureCredential
  • Key-based authentication now uses OpenAIClient with ApiKeyCredential instead of AzureKeyCredential
  • All endpoints are updated to include the /openai/v1 route suffix

Reviewed changes

Copilot reviewed 67 out of 67 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
dotnet/tests/Microsoft.Agents.AI.DurableTask.IntegrationTests/TestHelper.cs Updated test helper to use OpenAIClient with BearerTokenPolicy and ApiKeyCredential
dotnet/src/Microsoft.Agents.AI.Purview/README.md Updated documentation examples with new authentication pattern
dotnet/src/Microsoft.Agents.AI.Hosting.AzureFunctions/README.md Updated documentation examples with new client instantiation
dotnet/samples/Purview/AgentWithPurview/Program.cs Migrated Purview sample to use OpenAIClient
dotnet/samples/M365Agent/Program.cs Updated M365 agent to use new authentication
dotnet/samples/HostedAgents/* Updated all hosted agent samples with new client pattern
dotnet/samples/GettingStarted/Workflows/* Migrated all workflow samples to OpenAIClient
dotnet/samples/GettingStarted/ModelContextProtocol/* Updated MCP samples with new authentication
dotnet/samples/GettingStarted/DevUI/* Updated DevUI samples to use OpenAIClient
dotnet/samples/GettingStarted/DeclarativeAgents/* Migrated declarative agent samples
dotnet/samples/GettingStarted/Agents/* Updated all agent step samples (01-19) with new pattern
dotnet/samples/GettingStarted/AgentWithRAG/* Updated RAG samples with OpenAIClient
dotnet/samples/GettingStarted/AgentWithMemory/* Migrated memory samples to new authentication
dotnet/samples/GettingStarted/AgentProviders/* Updated provider samples with OpenAIClient
dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs Updated observability sample with new client
dotnet/samples/GettingStarted/AGUI/* Migrated all AGUI samples to new pattern
dotnet/samples/GettingStarted/A2A/* Updated A2A sample with OpenAIClient
dotnet/samples/AzureFunctions/* Updated all Azure Functions samples (01-07) with new authentication
dotnet/samples/AGUIWebChat/* Migrated web chat sample and documentation
dotnet/samples/AGUIClientServer/* Updated client-server samples with OpenAIClient
dotnet/README.md Updated main documentation with new authentication example

@stephentoub
Copy link
Member

@copilot, please address all of the feedback

Copy link
Contributor Author

Copilot AI commented Dec 11, 2025

@copilot, please address all of the feedback

Addressed all feedback in commit 9ea27f9:

  • Extracted OpenAIClientOptions to avoid duplication in all AzureFunctions samples (01-07)
  • Added TrimEnd('/') in TestHelper.cs to prevent malformed URLs with double slashes
  • Renamed azureOpenAIClient to openAIClient in Agent_Step14_Middleware sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants