openai/openai-dotnet

[BUG] Web Search - Empty Response

Closed this issue · 2 comments

Describe the bug

No matter what prompt I use, whenever using the web search tool I only get a empty response. Loads of output items, but nothing in them..

Steps to reproduce

// Build Prompt (all user messages joined)
string systemInstructions = string.Join("\n\n", chat.Messages.Where(m => m.Role == "system").Select(m => m.Content));
string userPrompt = "Use the latest web search results to answer: " +string.Join("\n\n", chat.Messages.Where(m => m.Role == "user").Select(m => m.Content));


// Enable web search + reasoning
var options = new ResponseCreationOptions
{
    Instructions = systemInstructions,
    Tools = { ResponseTool.CreateWebSearchTool() },
    ReasoningOptions = new ResponseReasoningOptions
    {
        ReasoningEffortLevel = ResponseReasoningEffortLevel.High
    }
};

if (chat.Metadata != null)
{
    // Add the max tokens if specified in metadata
    if (chat.Metadata.TryGetValue("max_output_tokens", out var maxTokensObj) && maxTokensObj is int maxTokens)
    {
        options.MaxOutputTokenCount = maxTokens;
    }    
}

var inputItems = new[]
{
    ResponseItem.CreateUserMessageItem(
        [ ResponseContentPart.CreateInputTextPart(userPrompt) ]
    )
};

var resp = await _responseClient.CreateResponseAsync(inputItems, options);

var json = resp.Value.GetOutputText();

foreach (ResponseItem item in resp.Value.OutputItems)
{
    if (item is WebSearchCallResponseItem webSearchCall)
    {
        Console.WriteLine($"[Web search invoked]({webSearchCall.Status}) {webSearchCall.Id}");
    }
    else if (item is MessageResponseItem message)
    {
        Console.WriteLine($"[{message.Role}] {message.Content?.FirstOrDefault()?.Text}");
    }
}



// Fallback: read the assistant message if OutputText is empty
if (string.IsNullOrWhiteSpace(json))
{
    foreach (var item in resp.Value.OutputItems)
        if (item is MessageResponseItem m && m.Content?.Count > 0)
            json = m.Content[0].Text ?? json;
}

if (!string.IsNullOrWhiteSpace(json))
{
    yield return new AIChatCompletionResponse(json, numAnswers, null);
}

Code snippets

var sbJson = new StringBuilder();

 await foreach (var update in _responseClient.CreateResponseStreamingAsync(userPrompt, options))
 {
     Debug.WriteLine($"[Response Update] Type: {update.GetType().Name} | Seq: {update.SequenceNumber}");
     Debug.WriteLine(JsonConvert.SerializeObject(update));

     // ✅ Handle JSON delta streaming (since response_format is json_schema)
     if (update is StreamingResponseOutputTextDeltaUpdate jsonDelta)
     {
         sbJson.Append(jsonDelta.Delta);
         // Optionally yield partials to a UI:
         yield return new AIChatCompletionResponse(jsonDelta.Delta ?? string.Empty, numAnswers, null);
     }

     // (optional) Also handle text deltas if you flip to text output later
     if (update is StreamingResponseOutputTextDeltaUpdate textDelta)
     {
         sbJson.Append(textDelta.Delta);
         yield return new AIChatCompletionResponse(textDelta.Delta ?? string.Empty, numAnswers, null);
     }

     if (update is StreamingResponseOutputTextDoneUpdate doneDelta)
     {
         sbJson.Append(doneDelta.Text);
         yield return new AIChatCompletionResponse(doneDelta.Text ?? string.Empty, numAnswers, null);
     }
 }

 // At the end, return the assembled JSON string
 var finalJson = sbJson.ToString();
 if (!string.IsNullOrWhiteSpace(finalJson))
 {
     yield return new AIChatCompletionResponse(finalJson, numAnswers, null);
 }

OS

Windows

.NET version

9

Library version

2.4.0

Hi @OGExclusives -
I'm not able to reproduce the problem. Here is a test that calls the WebSearchTool and gets a response as expected. Does that work for you?

Hi @OGExclusives. Thanks for reaching out and we regret that you're experiencing difficulties. The OpenAI client has no insight nor influence over the how the service processes the request. Beyond verifying that the client parameters are sent correctly to the service, there is little assistance we can provide if you're not seeing the expected result. You would need to engage the OpenAI support team.

Unfortunately, OpenAI does not offer service support from this repository. The most reliable and direct way to get support is by reaching out to the OpenAI Support Team. You can either email support@openai.com or start a new chat session via the Help Center. The OpenAI support team is available 24/7 and is equipped to assist with the full range of requests.

Client test results

Unfortunately, your code snippet is not complete and does not compile as written, so we cannot test your full end-to-end scenario. I crafted a couple of variants of queries and am unable to reproduce what you're seeing. In both cases, I was able to confirm that the client is sending the expected payload to the service, and see the service respond with web searches.

If I explicitly ask for the gpt-5 model to perform web searches, I see them:

     // Tool choice is not supported for gpt-5
     var options = new ResponseCreationOptions
    {
        Instructions = "Use the web serach tool to get information about the story.  Validate assumptions with at least two sources.  You should always present your responses as if you are Terry Crews and super excited about the world!",
        Tools = { ResponseTool.CreateWebSearchTool() }, 
        ReasoningOptions = new ResponseReasoningOptions
        {
            ReasoningEffortLevel = ResponseReasoningEffortLevel.High
        }
    };   

    var inputItems = new[]
    {
    ResponseItem.CreateUserMessageItem(
        [ ResponseContentPart.CreateInputTextPart("Tell me the story of the three little pigs") ]
    )
[Web search invoked](Completed) ws_68c45eb83d3481908ebef073e910cdcb0e2bc2396bedb2e4
[Web search invoked](Completed) ws_68c45ebcd294819080c93a7f56d12baf0e2bc2396bedb2e4
[Web search invoked](Completed) ws_68c45ec35cf88190be2ea19e8e24963f0e2bc2396bedb2e4

// ... << SNIP >> ...

Likewise, if I ask the gpt-4.1 model to explicitly use the web search tool, I see it do so:

    // Reasoning is not supported for gpt-4.1
    var options = new ResponseCreationOptions
    {
        Instructions = "You should always present your responses as if you are Terry Crews and super excited about the world!",
        Tools = { ResponseTool.CreateWebSearchTool() }, 
        ToolChoice = ResponseToolChoice.CreateWebSearchChoice(),
    };   

    var inputItems = new[]
    {
    ResponseItem.CreateUserMessageItem(
        [ ResponseContentPart.CreateInputTextPart("Tell me the story of the three little pigs") ]
    )
[Web search invoked](Completed) ws_68c461736e2c819087e861e62fdeeb5d02aa7e2c48a871e6