Skip to content

.NET: AgentThread is not persisted when RunStreamingAsync exits due to error or cancellation #2889

@cride9

Description

@cride9

When using RunStreamingAsync in the .NET Agent Framework, the AgentThread is not properly updated/saved if the execution exits early due to:

  • API errors (e.g. HTTP 400, resource exhausted, etc.)
  • A triggered CancellationToken

In these cases, the agent stops execution, but the thread context (memory) is not updated, resulting in lost conversation state.
This makes it difficult to reliably persist conversation history when streaming is interrupted or fails.

Steps to Reproduce

  1. Restore or create an AgentThread:
AgentThread thread;
if (string.IsNullOrEmpty(threadStateJson))
    thread = _agent.GetNewThread();
else
    try
    {
        thread = _agent.DeserializeThread(JsonDocument.Parse(threadStateJson).RootElement);
    }
    catch
    {
        thread = _agent.GetNewThread();
    }
  1. Start a streaming run:
await _agent.RunStreamingAsync(
    job.UserMessage,
    thread,
    null,
    cancellationToken: ct
);
  1. Cause one of the following:
  • An API error (e.g. invalid input → 400, resource exhaustion)
  • Trigger the CancellationToken while streaming
  1. Attempt to persist the AgentThread after execution.

Expected Behavior

  • The AgentThread should be updated with the latest context up to the point of failure.
  • Partial assistant responses and/or user messages should still be stored.
  • Cancellation should gracefully finalize and persist the thread state.

Am I doing something wrong? Or is this a real issue?

Metadata

Metadata

Assignees

Labels

.NETagentsIssues related to single agentsrequested-infov1.0Features being tracked for the version 1.0 GA

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions