Skip to content

[API Proposal]: GetRequiredService for IChatClient #5922

@eerhardt

Description

@eerhardt

Background and motivation

When working with DI containers, a common method to use is GetRequiredService when you expect the service to be provided by the DI container. In our code alone we have thousands of references to it:

Image

However IChatClient only has object? GetService and TService? GetService<TService>.

API Proposal

namespace Microsoft.Extensions.AI;

public static class ChatClientExtensions
{
    public static TService GetRequiredService<TService>(this IChatClient client, object? serviceKey = null)
}

API Usage

// most of my app is written agnostic of the service it is talking to
IChatClient client = ...;

// this part of my app needs to drop down to a specific client type to get access to its specific functionality
var azureAIClient = client.GetRequiredService<AzureAIInferenceChatClient>();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-aiMicrosoft.Extensions.AI libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions