Skip to content

.NET; Setting tools in ChatClientAgentOptions constructor, while also providing a new ChatOptions override the given tools/instructions #1453

Description

When creating an Agent using CreateAIAgent and you need to define AI Reasoning Effort, you need to use the overload that take in a ChatClientAgentOptions (so you can set ChatOptions' RawRepresentationFactory property).

This however lead to the override of any tool given to the constructor of ChatClientAgentOptions, leading to an invalid state of the Agent where you provided Tools, but they are not define anymore.

IMHO: I would say it is wrong to let the constructor receive the tools parameter (and should be removed)

Steps to reproduce:

Paste the following code into a Console App:

//Define the basics
AzureOpenAIClient azureOpenAiClient = new(new Uri("endpoint"), new ApiKeyCredential("key"));
ChatClient chatClient = azureOpenAiClient.GetChatClient("gpt-5-nano");
IList<AITool> aiTools = [AIFunctionFactory.Create(WhatIsTheUsersName)];
string? myInstructions = "Use tools to answer all questions.";
string myQuestion = "What is my Name?";

//Setup to be able to set Reasoning Effort on a Thinking Model
Func<IChatClient, object?> rawRepresentationFactory = _ => new ChatCompletionOptions()
{
#pragma warning disable OPENAI001
    ReasoningEffortLevel = "low",
#pragma warning restore OPENAI001
};

//This setup of an Agent does not work when ChatOptions are set and Tools are given in the ChatClientAgentOptions constructor
Console.WriteLine("FAILING EXAMPLE:");
ChatClientAgent agentSetupThatFailCallingTools = chatClient.CreateAIAgent(
    options: new ChatClientAgentOptions(instructions: myInstructions, tools: aiTools)
    {
        ChatOptions = new ChatOptions
        {
            RawRepresentationFactory = rawRepresentationFactory
        }
    });

AgentRunResponse response1 = await agentSetupThatFailCallingTools.RunAsync(myQuestion);
Console.WriteLine(response1);

Console.WriteLine("-------------------------------------");

//This setup work (because the tools are given via the chat-options)
Console.WriteLine("WORKING EXAMPLE:");
ChatClientAgent agentSetupThatWork = chatClient.CreateAIAgent(
    options: new ChatClientAgentOptions(instructions: myInstructions)
    {
        ChatOptions = new ChatOptions
        {
            RawRepresentationFactory = rawRepresentationFactory,
            Tools = aiTools
        },
    });

AgentRunResponse response2 = await agentSetupThatWork.RunAsync(myQuestion);
Console.WriteLine(response2);

//Tool
static string WhatIsTheUsersName()
{
    return "John AI";
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETUsage: [Issues, PRs], Target: .NetagentsUsage: [Issues, PRs], Target: Single agent

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions