From eae815d7bb61612597dfa0f789faf2b0dd3b3237 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sat, 11 Oct 2025 20:04:43 -0300 Subject: [PATCH] Make sure we provide compatible metadata for OpenAI We were missing that from our adaptive model chat client and preventing the ProviderName from being properly logged by telemetry. --- src/AI/OpenAI/OpenAIChatClient.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/AI/OpenAI/OpenAIChatClient.cs b/src/AI/OpenAI/OpenAIChatClient.cs index 68d596a..59ab415 100644 --- a/src/AI/OpenAI/OpenAIChatClient.cs +++ b/src/AI/OpenAI/OpenAIChatClient.cs @@ -17,6 +17,7 @@ public class OpenAIChatClient : IChatClient readonly string modelId; readonly ClientPipeline pipeline; readonly OpenAIClientOptions? options; + readonly ChatClientMetadata? metadata; /// /// Initializes the client with the specified API key, model ID, and optional OpenAI client options. @@ -28,7 +29,12 @@ public OpenAIChatClient(string apiKey, string modelId, OpenAIClientOptions? opti // NOTE: by caching the pipeline, we speed up creation of new chat clients per model, // since the pipeline will be the same for all of them. - pipeline = new OpenAIClient(new ApiKeyCredential(apiKey), options).Pipeline; + var client = new OpenAIClient(new ApiKeyCredential(apiKey), options); + metadata = client.GetChatClient(modelId) + .AsIChatClient() + .GetService(typeof(ChatClientMetadata)) as ChatClientMetadata; + + pipeline = client.Pipeline; } /// @@ -67,7 +73,12 @@ IChatClient GetChatClient(string modelId) => clients.GetOrAdd(modelId, model void IDisposable.Dispose() => GC.SuppressFinalize(this); - public object? GetService(Type serviceType, object? serviceKey = null) => null; + /// + public object? GetService(Type serviceType, object? serviceKey = null) => serviceType switch + { + Type t when t == typeof(ChatClientMetadata) => metadata, + _ => null + }; // Allows creating the base OpenAIClient with a pre-created pipeline. class PipelineClient(ClientPipeline pipeline, OpenAIClientOptions? options) : OpenAIClient(pipeline, options) { }