Skip to content

Commit bef47c5

Browse files
committed
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.
1 parent 381b2b8 commit bef47c5

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/AI/OpenAI/OpenAIChatClient.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class OpenAIChatClient : IChatClient
1717
readonly string modelId;
1818
readonly ClientPipeline pipeline;
1919
readonly OpenAIClientOptions? options;
20+
readonly ChatClientMetadata? metadata;
2021

2122
/// <summary>
2223
/// 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
2829

2930
// NOTE: by caching the pipeline, we speed up creation of new chat clients per model,
3031
// since the pipeline will be the same for all of them.
31-
pipeline = new OpenAIClient(new ApiKeyCredential(apiKey), options).Pipeline;
32+
var client = new OpenAIClient(new ApiKeyCredential(apiKey), options);
33+
metadata = client.GetChatClient(modelId)
34+
.AsIChatClient()
35+
.GetService(typeof(ChatClientMetadata)) as ChatClientMetadata;
36+
37+
pipeline = client.Pipeline;
3238
}
3339

3440
/// <inheritdoc/>
@@ -67,7 +73,12 @@ IChatClient GetChatClient(string modelId) => clients.GetOrAdd(modelId, model
6773

6874
void IDisposable.Dispose() => GC.SuppressFinalize(this);
6975

70-
public object? GetService(Type serviceType, object? serviceKey = null) => null;
76+
/// <inheritdoc />
77+
public object? GetService(Type serviceType, object? serviceKey = null) => serviceType switch
78+
{
79+
Type t when t == typeof(ChatClientMetadata) => metadata,
80+
_ => null
81+
};
7182

7283
// Allows creating the base OpenAIClient with a pre-created pipeline.
7384
class PipelineClient(ClientPipeline pipeline, OpenAIClientOptions? options) : OpenAIClient(pipeline, options) { }

0 commit comments

Comments
 (0)