Skip to content

Fix(lesson 02): Replaced deprecated AgentThread with AgentSession for context management#588

Open
marietta-a wants to merge 2 commits into
microsoft:mainfrom
marietta-a:fix/02-dotnet-agent-framework
Open

Fix(lesson 02): Replaced deprecated AgentThread with AgentSession for context management#588
marietta-a wants to merge 2 commits into
microsoft:mainfrom
marietta-a:fix/02-dotnet-agent-framework

Conversation

@marietta-a

@marietta-a marietta-a commented Jun 16, 2026

Copy link
Copy Markdown

Description

Fixes CS1061 and CS0246 compilation errors in 02-dotnet-agent-framework.cs by updating to the correct agent API methods and types.

Addresses: #587

Root Cause

During the creation of an agent with outdated methods, the code generated:

error CS1061: 'IChatClient' does not contain a definition for 'CreateAIAgent' ...
error CS0246: The type or namespace name 'AgentThread' could not be found ...
error CS1061: 'AIAgent' does not contain a definition for 'GetNewThread' ...

  • The extension method .CreateAIAgent(...) is not defined for IChatClient. The supported method is .AsAIAgent(...).
  • AgentThread is not part of the current framework; the correct type is AgentSession.
  • GetNewThread() has been replaced by CreateSessionAsync() for session management

Reference: Multi-turn Conversations, C#

Changes

  • Replaced .CreateAIAgent(...) with .AsAIAgent(...)
  • Replaced AgentThread with AgentSession
  • Replaced .GetNewThread() with .CreateSessionAsync()
  • Updated inline comments to reflect the correct agent/session workflow

Testing

Verified code compiles and runs without errors on:

  • OS: Windows 11, macOS Tahoe 26.2
  • .NET SDK: 10.0.301
  • Microsoft.Extensions.AI@10.*
  • Microsoft.Extensions.AI.OpenAI@10.-
  • Microsoft.Agents.AI.OpenAI@1.-

Copilot AI review requested due to automatic review settings June 16, 2026 13:42
@github-actions

Copy link
Copy Markdown
Contributor

👋 Thanks for contributing @marietta-a! We will review the pull request and get back to you soon.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates the .NET agentic framework code sample to use the newer agent/session APIs instead of the older agent/thread pattern for maintaining conversational context.

Changes:

  • Replaced CreateAIAgent(...) with AsAIAgent(...) in the chat client pipeline.
  • Replaced AgentThread usage with an async-created AgentSession.
  • Updated context-management comments to refer to sessions (partially).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 108 to 111
// Execute Agent: First Travel Planning Request
// Run the agent with an initial request that will likely trigger the random destination tool
// The agent will analyze the request, use the GetRandomDestination tool, and create an itinerary
// Using the thread parameter maintains conversation context for subsequent interactions
Comment on lines 121 to 123
// Demonstrate contextual conversation by referencing the previous response
// The agent remembers the previous destination suggestion and will provide an alternative
// This showcases the power of conversation threads and contextual understanding in .NET agents
@marietta-a marietta-a changed the title Fix(lesson 02): Switch from AgentThread to AgentSession for context management Fix(lesson 02): Replaced deprecated AgentThread with AgentSession for context management Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants