Skip to content

Using HostedWebSearchTool causes OpenAI's IChatClient to throws an exception when request sent #6274

@artl93

Description

@artl93

Description

Hi, folks, I'm getting the following error when trying to use HostedWebSearchTool.
This is my min repro from my solution. I'm using the latest dev builds.

While it works fine using a responses client, it should not throw in this case (after chatting with @stephentoub )

Reproduction Steps

Sample code:

using Microsoft.Extensions.AI;
using System.ClientModel;


var key = Environment.GetEnvironmentVariable("OPENAI_API_KEY") 
          ?? throw new InvalidOperationException("Missing environment variable: OPENAI_API_KEY.");
var openAIClient = new OpenAI.OpenAIClient(new ApiKeyCredential(key));

var chatClient = openAIClient.GetChatClient("gpt-4o-mini").AsIChatClient();


ChatOptions chatoptions = new (){
    Tools = [ new HostedWebSearchTool()],
};

while (true)
{

    var response = await chatClient.GetResponseAsync(Console.ReadLine()!, chatoptions, cancellationToken: default);
    if (response is not null)
    {
        Console.WriteLine(response);
    }
    Console.WriteLine();
}

Expected behavior

The call to get response async should succeed even if the tool is inaccessible.

Actual behavior

Exception has occurred: CLR/System.ClientModel.ClientResultException
An exception of type 'System.ClientModel.ClientResultException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'HTTP 400 (invalid_request_error: )
Parameter: tool_choice

Invalid value for 'tool_choice': 'tool_choice' is only allowed when 'tools' are specified.'
   at OpenAI.ClientPipelineExtensions.<ProcessMessageAsync>d__0.MoveNext()
   at System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable`1.ConfiguredValueTaskAwaiter.GetResult()
   at OpenAI.Chat.ChatClient.<CompleteChatAsync>d__16.MoveNext()
   at OpenAI.Chat.ChatClient.<CompleteChatAsync>d__6.MoveNext()
   at Microsoft.Extensions.AI.OpenAIChatClient.<GetResponseAsync>d__7.MoveNext()
   at Program.<<Main>$>d__0.MoveNext() in /Users/artleonard/src/minrepro/Program.cs:line 19

Regression?

No. New scenario.

The theory is that it's using tool choice is using auto.

Known Workarounds

Use the Responses API.

Configuration

  • .NET 9
  • "Azure.AI.OpenAI" Version="2.2.0-beta.4"
  • "Microsoft.Extensions.AI" Version="9.5.0-preview.1.25207.6"
  • "Microsoft.Extensions.AI.OpenAI" Version="9.5.0-preview.1.25207.6"

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-aiMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions