From f9a3918916a68aa80864878017403a434922031d Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Wed, 1 Oct 2025 11:37:12 +0100 Subject: [PATCH] Address bug when agent rawrepresentation is a MEAI type and returns it with null RawRep (#1054) --- dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs | 2 +- dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs | 4 ++-- dotnet/src/Shared/Demos/SampleEnvironment.cs | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs b/dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs index bf59beac6f..b835f4ce12 100644 --- a/dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs +++ b/dotnet/samples/GettingStarted/AgentOpenTelemetry/Program.cs @@ -85,7 +85,7 @@ Console.WriteLine(""" """); var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set."); -var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; +var deploymentName = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; // Log application startup appLogger.LogInformation("OpenTelemetry Aspire Demo application started"); diff --git a/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs b/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs index 715e6dbd26..247bd12cbc 100644 --- a/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs +++ b/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs @@ -85,7 +85,7 @@ public sealed class OpenTelemetryAgent : DelegatingAIAgent, IDisposable var response = await this._otelClient.GetResponseAsync(messages, co, cancellationToken).ConfigureAwait(false); - return (AgentRunResponse)response.RawRepresentation!; + return response.RawRepresentation as AgentRunResponse ?? new AgentRunResponse(response); } /// @@ -96,7 +96,7 @@ public sealed class OpenTelemetryAgent : DelegatingAIAgent, IDisposable await foreach (var update in this._otelClient.GetStreamingResponseAsync(messages, co, cancellationToken).ConfigureAwait(false)) { - yield return (AgentRunResponseUpdate)update.RawRepresentation!; + yield return update.RawRepresentation as AgentRunResponseUpdate ?? new AgentRunResponseUpdate(update); } } diff --git a/dotnet/src/Shared/Demos/SampleEnvironment.cs b/dotnet/src/Shared/Demos/SampleEnvironment.cs index 4e506a66d4..850ab2da62 100644 --- a/dotnet/src/Shared/Demos/SampleEnvironment.cs +++ b/dotnet/src/Shared/Demos/SampleEnvironment.cs @@ -7,6 +7,7 @@ using System.Collections; using SystemEnvironment = System.Environment; namespace SampleHelpers; + internal static class SampleEnvironment { public static string? GetEnvironmentVariable(string key)