microsoft / microsoft/agent-framework

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

Open
#8,243 1 comment 0 reactions 1 assignee Claimed by @rogerbarreto View on GitHub
.NET agents reproduced
Dominant language
Python
Stars
13.6k
Forks
2.3k
Avg merge
2d 45m
Merged PRs (30d)
358

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

```markdown
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

```markdown
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.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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.