Skip to content

[BUG] Web Search - Empty Response #683

@OGExclusives

Description

@OGExclusives

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

Metadata

Metadata

Assignees

Labels

area: responsesThis item is related to Responsesissue-addressedWorkflow: The OpenAI maintainers believe the issue to be addressed and ready to close.questionCategory: The issue is seeking information about the library or its usage.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions