Skip to content

.NET: Fix: Standardize agent construction pattern in samples#4516

Open
hobostay wants to merge 2 commits intomicrosoft:mainfrom
hobostay:fix/agent-construction-consistency
Open

.NET: Fix: Standardize agent construction pattern in samples#4516
hobostay wants to merge 2 commits intomicrosoft:mainfrom
hobostay:fix/agent-construction-consistency

Conversation

@hobostay
Copy link

@hobostay hobostay commented Mar 6, 2026

This PR addresses issue #4505 by standardizing the agent construction pattern across all sample files.

Changes

  • Remove redundant .AsIChatClient() call before .AsAIAgent() since ChatClient already implements IChatClient
  • Update code in AGUI samples (Steps 1-5)
  • Update code in AGUIWebChat and AGUIClientServer samples
  • Update README documentation to reflect the change

Benefits

  • Simpler, more consistent code
  • Reduced dependencies
  • Easier to understand for new users

Remove redundant .AsIChatClient() call before .AsAIAgent() since ChatClient already implements IChatClient. This simplifies the code and reduces dependencies as suggested in issue microsoft#4505.

Changes:
- Update all sample files to use consistent pattern: chatClient.AsAIAgent()
- Update README documentation to reflect the change
- Affects AGUI samples and AGUIWebChat samples
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Mar 6, 2026
@github-actions github-actions bot changed the title Fix: Standardize agent construction pattern in samples .NET: Fix: Standardize agent construction pattern in samples Mar 6, 2026
@hobostay
Copy link
Author

hobostay commented Mar 6, 2026

@microsoft-github-policy-service agree

ChatClient chatClient = s_azureOpenAIClient!.GetChatClient(s_deploymentName!);

return chatClient.AsIChatClient().AsAIAgent(
return chatClient.AsAIAgent(
Copy link
Contributor

Choose a reason for hiding this comment

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

This will require a reference to the Microsoft.Agents.AI.OpenAI project to compile.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the review! I've added the missing using Microsoft.Agents.AI.OpenAI; statements to all files that use chatClient.AsAIAgent().

The AsAIAgent() extension method is defined in the Microsoft.Agents.AI.OpenAI namespace, so this using statement is required for the code to compile when calling chatClient.AsAIAgent() directly.

Updated files:

  • dotnet/samples/02-agents/AGUI/Step01_GettingStarted/Server/Program.cs
  • dotnet/samples/02-agents/AGUI/Step02_BackendTools/Server/Program.cs
  • dotnet/samples/02-agents/AGUI/Step03_FrontendTools/Server/Program.cs
  • dotnet/samples/02-agents/AGUI/Step04_HumanInLoop/Server/Program.cs
  • dotnet/samples/02-agents/AGUI/Step05_StateManagement/Server/Program.cs
  • dotnet/samples/05-end-to-end/AGUIClientServer/AGUIDojoServer/ChatClientAgentFactory.cs
  • dotnet/samples/05-end-to-end/AGUIWebChat/Server/Program.cs

Address review feedback from @westey-m:
- Add using Microsoft.Agents.AI.OpenAI to files that use chatClient.AsAIAgent()
- This is required as AsAIAgent() extension method is defined in this namespace
@westey-m westey-m self-assigned this Mar 6, 2026
@westey-m westey-m moved this to Community PR in Agent Framework Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

Status: Community PR

Development

Successfully merging this pull request may close these issues.

3 participants