Skip to content

.NET samples fail to compile: 'ChatClient' does not contain a definition for 'CreateAIAgent' #434

@AdamParker19

Description

@AdamParker19

All .NET code samples in the repository fail to compile with .NET 10 SDK due to a missing AsIChatClient() conversion. The CreateAIAgent extension method requires an IChatClient interface, but the code passes a ChatClient object directly.

Error Message

error CS1929: 'ChatClient' does not contain a definition for 'CreateAIAgent' and the best 
extension method overload 'ChatClientExtensions.CreateAIAgent(IChatClient, string?, string?, 
string?, IList<AITool>?, ILoggerFactory?, IServiceProvider?)' requires a receiver of type 
'Microsoft.Extensions.AI.IChatClient'

Steps to Reproduce

  1. Clone the repository
  2. Ensure .NET 10 SDK is installed (dotnet --version shows 10.0.x)
  3. Navigate to any .NET sample, e.g., 02-explore-agentic-frameworks/code_samples/
  4. Run: dotnet run 02-dotnet-agent-framework.cs
  5. Build fails with the error above

Affected Files
All .NET sample files are affected, including:

  • 01-intro-to-ai-agents/code_samples/01-dotnet-agent-framework.cs
  • 02-explore-agentic-frameworks/code_samples/02-dotnet-agent-framework.cs
  • 03-agentic-design-patterns/code_samples/03-dotnet-agent-framework.cs
  • 04-tool-use/code_samples/04-dotnet-agent-framework.cs
  • And others...

Root Cause
The OpenAIClient.GetChatClient() method returns an OpenAI ChatClient object, but the CreateAIAgent() extension method (from Microsoft.Agents.AI) expects the IChatClient interface from Microsoft.Extensions.AI.

Proposed Fix

  1. Add the Microsoft.Extensions.AI.OpenAI package reference:
    #:package Microsoft.Extensions.AI.OpenAI@10.*-*

  2. Add .AsIChatClient() conversion after GetChatClient():

AIAgent agent = openAIClient
     .GetChatClient(github_model_id)
   .AsIChatClient()
    .CreateAIAgent(
         name: AGENT_NAME,
         instructions: AGENT_INSTRUCTIONS,
         tools: [AIFunctionFactory.Create(GetRandomDestination)]
   );

Environment

  • .NET SDK: 10.0.101
  • OS: Windows 11
  • Packages:
  • Microsoft.Extensions.AI@10.*
  • Microsoft.Agents.AI.OpenAI@1.

Labels
bug, dotnet, good first issue

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions