Add comprehensive dependency injection support with IServiceCollection extension methods #696
Add comprehensive dependency injection support with IServiceCollection extension methods #696mokarchi wants to merge 14 commits intoopenai:mainfrom
Conversation
This commit introduces new features for dependency injection in the OpenAI .NET library, allowing for easier registration and configuration of clients within ASP.NET Core applications. Key changes include: - New extension methods for registering OpenAI clients with support for `appsettings.json` and environment variables. - Introduction of the `OpenAIServiceOptions` class for simplified configuration. - Updates to README.md with detailed usage instructions and examples. - Modifications to project files to include necessary package references. - Comprehensive unit tests to validate the new features and ensure proper functionality.
|
Should this instead be left to Aspire, e.g. https://www.nuget.org/packages/Aspire.OpenAI ? cc: @eerhardt |
|
@stephentoub @KrzysztofCwalina, I was wondering about the current status of this PR. Should I wait or make any changes? |
@m-nash @eerhardt, @jsquire, how would you want to handle this PR? It seems like you are working features similar to it. |
|
@eerhardt Could you review this, please? |
|
Hi @mokarchi. Thank you for your contribution and your interest in improving the OpenAI developer experience. Apologies for the feedback delay, but there was some coordination needed on other workstreams. As Krzysztof mentioned, we are in the process of designing an experience where clients natively integrate with the .NET configuration, options, and DI systems. Part of that work entails defining the common patterns across the OpenAI clients for DI registration, construction, and configuration and ensuring that they align closely with the .NET ecosystem. Because we don't yet have a fully codified design, we're not able to provide the detailed feedback that would be needed to help your contribution align. As a result, we're unable to move forward at this time. We have a couple of options that you can consider for next steps:
Please let us know how you'd like to proceed. //cc: @m-nash for awareness. |
jsquire
left a comment
There was a problem hiding this comment.
Blocking for now to prevent accidental merge.
|
Thank you for the update, @jsquire. |
|
Thank you, @mokarchi. We appreciate your flexibility and understanding. |
Late to the party, but yeah this feels like the best path forward. Once the design is ready to share we can collaborate on the implementation. |
…ests - Deleted ServiceCollectionExtensionsAdvanced.cs which contained methods for adding OpenAI services with enhanced configuration support. - Removed OpenAIServiceOptionsTests.cs that tested the OpenAIServiceOptions class. - Deleted ServiceCollectionExtensionsAdvancedTests.cs which included tests for the advanced service collection methods. - Updated ServiceCollectionExtensionsTests.cs to reflect changes in service registration methods and added new tests for the simplified client builder methods.
@mokarchi You jumped on this faster than I was ready I hadn't put all the details into the issue yet. I updated issues with the rest of the details. |
Ready for your review. let me know if anything needs tweaking or if I missed a detail. |
m-nash
left a comment
There was a problem hiding this comment.
It looks like we are missing a few clients in here like AssistantClient, BatchClient, etc. Lets add those in as well with a settings that matches their main ctor.
m-nash
left a comment
There was a problem hiding this comment.
It looks like we are missing a few clients in here like AssistantClient, BatchClient, etc. Lets add those in as well with a settings that matches their main ctor.
…lified settings ctors & extensions
m-nash
left a comment
There was a problem hiding this comment.
One minor update but this is looking good. @joseharriaga can you do a quick review as well?
Removing blocker to allow m-nash to guide the review.
|
@m-nash, Ready for your review. if there's anything else, please let me know. |
m-nash
left a comment
There was a problem hiding this comment.
Changes LGTM, thanks again @mokarchi .
@joseharriaga can you also take a look to see if anything stands out where do we typically document things like this?
|
@mokarchi looks like CI fails because you forgot to run ./scripts/Export-Api.ps1. You will need to run that and add those changed api files to the PR. |
There was a problem hiding this comment.
Pull request overview
This PR adds first-class configuration binding and dependency injection registration for the OpenAI .NET clients using the System.ClientModel ClientSettings pattern, enabling per-client configuration and host-builder-based registrations.
Changes:
- Introduces
*ClientSettingstypes per specialized client, implementing configuration binding viaClientSettings.BindCore. - Adds new
ClientSettings-based constructors across the specialized clients. - Adds
IHostApplicationBuilderextension methods to register clients (including keyed registrations) viaAddClient<TClient,TSettings>/AddKeyedClient<TClient,TSettings>.
Reviewed changes
Copilot reviewed 38 out of 38 changed files in this pull request and generated 19 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Custom/DependencyInjection/OpenAIHostBuilderExtensions.cs | Adds IHostApplicationBuilder extension methods for registering OpenAI clients (including keyed variants). |
| src/Custom/OpenAIClientOptions.cs | Adds configuration-section-based binding constructor used by settings classes. |
| src/Custom/Chat/ChatClientSettings.cs | Adds settings type to bind Model and nested Options for ChatClient. |
| src/Custom/Chat/ChatClient.cs | Adds settings-based constructor for ChatClient. |
| src/Custom/Embeddings/EmbeddingClientSettings.cs | Adds settings type to bind Model and nested Options for EmbeddingClient. |
| src/Custom/Embeddings/EmbeddingClient.cs | Adds settings-based constructor for EmbeddingClient. |
| src/Custom/Audio/AudioClientSettings.cs | Adds settings type to bind Model and nested Options for AudioClient. |
| src/Custom/Audio/AudioClient.cs | Adds settings-based constructor for AudioClient. |
| src/Custom/Images/ImageClientSettings.cs | Adds settings type to bind Model and nested Options for ImageClient. |
| src/Custom/Images/ImageClient.cs | Adds settings-based constructor for ImageClient. |
| src/Custom/Moderations/ModerationClientSettings.cs | Adds settings type to bind Model and nested Options for ModerationClient. |
| src/Custom/Moderations/ModerationClient.cs | Adds settings-based constructor for ModerationClient. |
| src/Custom/Responses/ResponsesClientSettings.cs | Adds settings type to bind Model and nested Options for ResponsesClient. |
| src/Custom/Responses/ResponsesClient.cs | Adds settings-based constructor for ResponsesClient and a small whitespace-only change. |
| src/Custom/Assistants/AssistantClientSettings.cs | Adds settings type to bind nested Options for AssistantClient. |
| src/Custom/Assistants/AssistantClient.cs | Adds settings-based constructor for AssistantClient. |
| src/Custom/Batch/BatchClientSettings.cs | Adds settings type to bind nested Options for BatchClient. |
| src/Custom/Batch/BatchClient.cs | Adds settings-based constructor for BatchClient. |
| src/Custom/Containers/ContainerClientSettings.cs | Adds settings type to bind nested Options for ContainerClient. |
| src/Custom/Containers/ContainerClient.cs | Adds settings-based constructor for ContainerClient. |
| src/Custom/Conversations/ConversationClientSettings.cs | Adds settings type to bind nested Options for ConversationClient. |
| src/Custom/Conversations/ConversationClient.cs | Adds settings-based constructor for ConversationClient. |
| src/Custom/Evals/EvaluationClientSettings.cs | Adds settings type to bind nested Options for EvaluationClient. |
| src/Custom/Evals/EvaluationClient.cs | Adds settings-based constructor for EvaluationClient. |
| src/Custom/Files/OpenAIFileClientSettings.cs | Adds settings type to bind nested Options for OpenAIFileClient. |
| src/Custom/Files/OpenAIFileClient.cs | Adds settings-based constructor for OpenAIFileClient. |
| src/Custom/FineTuning/FineTuningClientSettings.cs | Adds settings type to bind nested Options for FineTuningClient. |
| src/Custom/FineTuning/FineTuningClient.cs | Adds settings-based constructor for FineTuningClient. |
| src/Custom/Graders/GraderClientSettings.cs | Adds settings type to bind nested Options for GraderClient. |
| src/Custom/Graders/GraderClient.cs | Adds settings-based constructor for GraderClient. |
| src/Custom/Models/OpenAIModelClientSettings.cs | Adds settings type to bind nested Options for OpenAIModelClient. |
| src/Custom/Models/OpenAIModelClient.cs | Adds settings-based constructor for OpenAIModelClient. |
| src/Custom/Realtime/RealtimeClientSettings.cs | Adds settings type to bind nested Options for RealtimeClient. |
| src/Custom/Realtime/RealtimeClient.cs | Adds settings-based constructor for RealtimeClient. |
| src/Custom/VectorStores/VectorStoreClientSettings.cs | Adds settings type to bind nested Options for VectorStoreClient. |
| src/Custom/VectorStores/VectorStoreClient.cs | Adds settings-based constructor for VectorStoreClient. |
| src/Custom/Videos/VideoClientSettings.cs | Adds settings type to bind nested Options for VideoClient. |
| src/Custom/Videos/VideoClient.cs | Adds settings-based constructor for VideoClient. |
…nd default options handling
|
@joseharriaga Thanks for the thorough review and all the detailed feedback! I've now applied all the requested changes. |
|
This PR implements per-client configuration and dependency injection support for the OpenAI .NET library, fully aligning with the design proposed in issue #932 and following the modern .NET / Azure SDK pattern using System.ClientModel.
The new approach replaces manual DI registrations with strongly-typed, configuration-bound settings classes for each specialized client (ChatClient, EmbeddingClient, AudioClient, ImageClient, ModerationClient).
Key Features Added
Dedicated Settings Classes per Client
Each specialized client now has its own settings class inheriting from
ClientSettingsThese classes contain only the properties needed by the client's primary constructor (mainly
ModelandOptions), and credential is handled automatically by the baseClientSettings.Example structure:
New Constructor for Specialized Clients
Each specialized client now has an additional constructor that accepts its settings class:
(Similar constructors added to EmbeddingClient, AudioClient, ImageClient, ModerationClient)
Extension methods on IHostApplicationBuilder for registering each client with configuration support:
Keyed registration is also supported:
Full support for Microsoft.Extensions.Configuration binding with hierarchical keys, $ref syntax, and environment variables:
{ "Chat": { "Model": "gpt-4o-mini", "Options": { "OrganizationId": "org-abc123", "ProjectId": "proj-xyz789" } }, "Embedding": { "Model": "text-embedding-3-small" }, "Audio": { "Model": "whisper-1" } }