Skip to content

.NET: [Bug]: UseOpenTelemetry breaks background continuation tokens #8243

Description

Description

Issue:

Wrapping an agent with UseOpenTelemetry changes the continuation token from a background RunAsync so opaque save/restore + Continue fails. Without OTel, the same flow works.

Without OTel, ContinuationToken accepts and ToBytes/ FromBytes round-trip works as expected. Adding OTel with the same code throws.

This is an issue for us because we have separate processes that poll our responses, so we must store serialized sessions and tokens across instances of the same agents. We also need OTel for logging and traceability. The combination forces us to write "hacky" helper methods to circumvent these issues.

Code Sample

using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT");
var model = Environment.GetEnvironmentVariable("FOUNDRY_MODEL");

var projectClient = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential());

AIAgent agent = projectClient
    .AsAIAgent(model: model, name: "otel-repro", instructions: "You are a helpful assistant.")
    .AsBuilder()
    .UseOpenTelemetry(sourceName: "repro", configure: o => o.EnableSensitiveData = true) // Commenting out this line yields a successful run
    .Build();

var session = await agent.CreateSessionAsync();
var options = new AgentRunOptions { AllowBackgroundResponses = true };

var start = await agent.RunAsync(
    "Write a long multi-chapter novel outline about otters in space.",
    session,
    options);

Console.WriteLine($"Start token: {start.ContinuationToken?.GetType().FullName}");

options.ContinuationToken = ResponseContinuationToken.FromBytes(start.ContinuationToken!.ToBytes());
await agent.RunAsync(session, options);

Error Messages / Stack Traces

Start token: Microsoft.Extensions.AI.ResponsesClientContinuationToken
Unhandled exception. System.ArgumentException: Failed to create ChatClientAgentContinuationToken from provided token because it is not of the correct type. (Parameter 'token')
   at Microsoft.Shared.Diagnostics.Throw.ArgumentException(String paramName, String message)
   at Microsoft.Agents.AI.ChatClientAgentContinuationToken.ValidateTokenType(Utf8JsonReader reader, ResponseContinuationToken token)
   at Microsoft.Agents.AI.ChatClientAgentContinuationToken.FromToken(ResponseContinuationToken token)
   at Microsoft.Agents.AI.ChatClientAgent.<CreateConfiguredChatOptions>g__ApplyAgentRunOptionsOverrides|37_0(ChatOptions chatOptions, AgentRunOptions agentRunOptions)
   at Microsoft.Agents.AI.ChatClientAgent.CreateConfiguredChatOptions(AgentRunOptions runOptions)
   at Microsoft.Agents.AI.ChatClientAgent.PrepareSessionAndMessagesAsync(AgentSession session, IEnumerable`1 inputMessages, AgentRunOptions runOptions, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.ChatClientAgent.RunCoreAsync(IEnumerable`1 messages, AgentSession session, AgentRunOptions options, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.AIAgent.RunAsync(IEnumerable`1 messages, AgentSession session, AgentRunOptions options, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.OpenTelemetryAgent.ForwardingChatClient.GetResponseAsync(IEnumerable`1 messages, ChatOptions options, CancellationToken cancellationToken)
   at Microsoft.Extensions.AI.OpenTelemetryChatClient.GetResponseAsync(IEnumerable`1 messages, ChatOptions options, CancellationToken cancellationToken)
   at Microsoft.Agents.AI.OpenTelemetryAgent.RunCoreAsync(IEnumerable`1 messages, AgentSession session, AgentRunOptions options, CancellationToken cancellationToken)

Package Versions

Azure.AI.Projects: 2.0.1, Azure.Identity: 1.21.0, Microsoft.Agents.AI.Foundry: 1.5.0, Microsoft.Agents.AI.OpenAI: 1.20.0, Microsoft.Agents.AI: 1.20.0, Microsoft.Agents.AI.Abstractions: 1.20.0, Microsoft.Extensions.AI: 10.9.0, Microsoft.Extensions.AI.Abstractions: 10.9.0

.NET Version

.NET 10

Additional Context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .NetagentsUsage: [Issues, PRs], Target: Single agentreproducedUsage: [Issues], Target: all issues that can be reproduced by the triage workflow

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions