Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 3, 2025

Overview

This PR migrates all usage of AzureOpenAIClient from the Azure.AI.OpenAI package to the base OpenAIClient class from the OpenAI SDK. Per the issue requirements, using OpenAIClient with the /openai/v1 route is recommended over using AzureOpenAIClient.

Changes Made

Code Migration (39 files)

Updated all C# sample files across the repository to use OpenAIClient instead of AzureOpenAIClient:

  • GettingStarted samples: 24 files (Agents, AgentProviders, AgentOpenTelemetry, ModelContextProtocol, Workflows)
  • SemanticKernelMigration samples: 11 files (AzureOpenAI, AzureOpenAIAssistants, AzureOpenAIResponses)

Key Technical Changes

  1. Endpoint Configuration: All Azure OpenAI endpoints now append /openai/v1 to the URI
  2. Authentication: Token-based authentication (e.g., AzureCliCredential, DefaultAzureCredential) is now wrapped in BearerTokenPolicy with scope https://ai.azure.com/.default
  3. Using Statements: Replaced Azure.AI.OpenAI namespace with System.ClientModel and System.ClientModel.Primitives

Package Cleanup (30 files)

  • Removed Azure.AI.OpenAI package reference from 29 .csproj files
  • Removed Azure.AI.OpenAI version declaration from Directory.Packages.props

Before:

using Azure.AI.OpenAI;
using Azure.Identity;

var agent = new AzureOpenAIClient(
    new Uri(endpoint),
    new AzureCliCredential())
     .GetChatClient(deploymentName)
     .CreateAIAgent(instructions, name);

After:

using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.Identity;
using OpenAI;

var agent = new OpenAIClient(
    new BearerTokenPolicy(new AzureCliCredential(), "https://ai.azure.com/.default"),
    new OpenAIClientOptions() { Endpoint = new Uri($"{endpoint}/openai/v1") })
     .GetChatClient(deploymentName)
     .CreateAIAgent(instructions, name);

Verification

  • ✅ All samples build successfully
  • ✅ All unit tests pass (333 tests: 232 + 101)
  • ✅ Zero remaining AzureOpenAIClient usage in the codebase
  • ✅ No Azure.AI.OpenAI package dependencies remain (except Aspire.Azure.AI.OpenAI for Aspire integration)
  • ✅ Merge conflicts resolved with latest main branch

Notes

  • The System.ClientModel package is available transitively through the OpenAI SDK, so no explicit package reference is needed
  • ChatClientExtensions.cs in AgentWebChat was reviewed but not modified as it uses Aspire extension methods, not direct AzureOpenAIClient instantiation
  • All samples maintain their original functionality while using the recommended OpenAI SDK pattern
  • Integrated latest main branch changes including inlined const values and updated package versions (Azure.Identity 1.17.0, CommunityToolkit.Aspire.OllamaSharp 9.8.0)

Fixes #1165

Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: Switch use of AzureOpenAIClient to OpenAIClient</issue_title>
<issue_description>Wherever possible (which is most uses), using OpenAIClient with the /openai/v1 route is recommended instead of using AzureOpenAIClient. We should update all of the samples accordingly. There are currently > 40 uses of AzureOpenAIClient in the repo; that should drop to 0.</issue_description>

<agent_instructions>Please systematically find all use of AzureOpenAIClient in the repo and replace with direct use of the base OpenAIClient. You will need to update the endpoint URIs with "/openai/v1" tacked onto them, and for any that are using authorization via something other than API keys, e.g. DefaultAzureCredential, you'll need to wrap it in a BearerTokenPolicy in order to pass it to the OpenAIClient constructor. Note that to do so, you'll need to ensure the repo is referencing the latest version of the Azure.Identity and System.ClientModel packages. Once all of the replacements have been made, you should be able to remove Azure.AI.OpenAI package references from the repo completely, relying just on the base OpenAI package.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #1165

✨ 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] .NET: Switch use of AzureOpenAIClient to OpenAIClient .NET: [WIP] .NET: Switch use of AzureOpenAIClient to OpenAIClient Oct 3, 2025
Copilot AI changed the title .NET: [WIP] .NET: Switch use of AzureOpenAIClient to OpenAIClient .NET: Switch use of AzureOpenAIClient to OpenAIClient Oct 3, 2025
Copilot AI requested a review from stephentoub October 3, 2025 16:51
Copilot finished work on behalf of stephentoub October 3, 2025 16:51
@stephentoub stephentoub marked this pull request as ready for review October 3, 2025 16:52
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 successfully migrates all usage of AzureOpenAIClient from the Azure.AI.OpenAI package to the base OpenAIClient class from the OpenAI SDK, following Microsoft's recommendation to use OpenAIClient with the /openai/v1 route instead of AzureOpenAIClient.

Key changes:

  • Replaced all AzureOpenAIClient instantiations with OpenAIClient using proper endpoint configuration and authentication
  • Updated authentication to use BearerTokenPolicy wrapper for Azure credentials
  • Removed Azure.AI.OpenAI package dependencies across all affected projects

Reviewed Changes

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

File Description
Multiple Program.cs files Updated client instantiation from AzureOpenAIClient to OpenAIClient with proper endpoint and authentication configuration
Multiple .csproj files Removed Azure.AI.OpenAI package references
Directory.Packages.props Removed centralized Azure.AI.OpenAI package version declaration

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@stephentoub stephentoub force-pushed the copilot/fix-32f34ae3-c04c-4d1d-ab50-b71db78acf2e branch from a37f0fc to c39949c Compare October 3, 2025 17:02
@markwallace-microsoft markwallace-microsoft added the documentation Improvements or additions to documentation label Oct 3, 2025
@SergeyMenshykh
Copy link
Member

@stephentoub
Copy link
Member

will close these two issues, correct?

No, this does nothing about the compatibility of the packages. This just reduces this repo's reliance on Azure.AI.OpenAI, switching to the recommendation of using the base package directly, thereby reducing the impact that those issues have (they still impact the repo, however, due to samples that use SK) on this repo.

For now, I would like to see us:

  • Merge this PR to eliminate the direct dependencies from this repo on Azure.AI.OpenAI.
  • Split any samples/projects that reference both SK's OpenAI connector and either OpenAI / Microsoft.Agents.AI.OpenAI so that no project pulls in both directly or indirectly. SK's OpenAI connector has public surface area from Azure.AI.OpenAI, so referencing it in the same project as something referencing things from OpenAI means that any future disconnect between the latest Azure.AI.OpenAI and OpenAI packages prevents this repo from moving to the latest OpenAI.

@SergeyMenshykh
Copy link
Member

@copilot resolve the conflicts

Copy link
Contributor Author

Copilot AI commented Oct 10, 2025

@copilot resolve the conflicts

Conflicts resolved in 09b83c7. Merged latest main branch changes including inlined const values from #1387 and updated package versions while preserving the OpenAIClient migration.

@stephentoub
Copy link
Member

Replaced by #1402

@crickman crickman deleted the copilot/fix-32f34ae3-c04c-4d1d-ab50-b71db78acf2e branch October 14, 2025 20:22
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 NO-MERGE

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Switch use of AzureOpenAIClient to OpenAIClient

5 participants