Skip to content

.Net: Bugfix OpenAI Assistant Client When Annotation comes in an empty text chunk#6723

Merged
stephentoub merged 1 commit intodotnet:mainfrom
rogerbarreto:openai-assistants-filesearchtool-bugfix
Aug 15, 2025
Merged

.Net: Bugfix OpenAI Assistant Client When Annotation comes in an empty text chunk#6723
stephentoub merged 1 commit intodotnet:mainfrom
rogerbarreto:openai-assistants-filesearchtool-bugfix

Conversation

@rogerbarreto
Copy link
Copy Markdown
Contributor

@rogerbarreto rogerbarreto commented Aug 15, 2025

Motivation and Context

This PR fixes a bug where the annotation information can come in the streaming from a delta chunk that doesn't share a Text chunk, to accommodate those scenarios an empty text chunk is created with the annotation attached to it.

Reproduction / Test Code below works after the change.

var configuration = new ConfigurationBuilder()
    .AddUserSecrets<Program>()
    .Build();

var apiKey = configuration["OpenAI:ApiKey"]!;

var fileClient = new OpenAIFileClient(apiKey);
OpenAIFile openAIFileInfo = await fileClient.UploadFileAsync("employees.pdf", FileUploadPurpose.Assistants);

// Upload a file to the specified provider.
var fileId = openAIFileInfo.Id;

// Create a vector store for the uploaded file to enable file search capabilities.
var vectorStoreClient = new VectorStoreClient(apiKey);
VectorStoreCreationOptions vectorOptions = new();
vectorOptions.FileIds.Add(fileId);

var vectorStore = await vectorStoreClient.CreateVectorStoreAsync(waitUntilCompleted: true, vectorOptions);

// Create a file search tool that can access the vector store.
var fileSearchTool = new HostedFileSearchTool()
{
    Inputs = [new HostedVectorStoreContent(vectorStore.Value!.Id)],
};

ChatOptions chatOptions = new() { Tools = [fileSearchTool] };

var assistantClient = new AssistantClient(apiKey);
Assistant assistant = await assistantClient.CreateAssistantAsync("gpt-4o-mini", new AssistantCreationOptions()
{
    Name = "FileSearchAssistant",
    Instructions = "You are a helpful assistant that can search through uploaded documents to answer questions. Use the file search tool to find relevant information from the uploaded files.",
});

using var chatClient = new AssistantClient(apiKey).AsIChatClient(assistant.Id);

const string Prompt = "Who is the youngest employee?";

await foreach (var update in chatClient.GetStreamingResponseAsync(Prompt, chatOptions))
{
    Console.Write(update);
}

await assistantClient.DeleteAssistantAsync(assistant.Id);
Microsoft Reviewers: Open in CodeFlow

Copilot AI review requested due to automatic review settings August 15, 2025 14:36
@rogerbarreto rogerbarreto requested a review from a team as a code owner August 15, 2025 14:36
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Aug 15, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in the OpenAI Assistant Client where annotation information can arrive in a streaming delta chunk without accompanying text content. The fix creates an empty text chunk to hold the annotation when no text content is present.

  • Adds a check for empty text content in delta chunks
  • Creates an empty TextContent object when needed to attach annotations
  • Ensures annotations are properly processed even when they arrive without text

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Copy Markdown
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@stephentoub stephentoub merged commit 2130f36 into dotnet:main Aug 15, 2025
7 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants