Skip to content

Update OpenAIResponsesChatClient to handle streaming code interpreter content#7267

Open
stephentoub wants to merge 2 commits intodotnet:mainfrom
stephentoub:fixcodeint
Open

Update OpenAIResponsesChatClient to handle streaming code interpreter content#7267
stephentoub wants to merge 2 commits intodotnet:mainfrom
stephentoub:fixcodeint

Conversation

@stephentoub
Copy link
Member

@stephentoub stephentoub commented Feb 5, 2026

Right now it outputs it but in a bulk fashion only at the end of the response item. This makes it yield the deltas instead.

Microsoft Reviewers: Open in CodeFlow

… content

Right now it outputs it but in a bulk fashion only at the end of the response item. This makes it yield the deltas instead.
@stephentoub stephentoub requested a review from a team as a code owner February 5, 2026 13:16
Copilot AI review requested due to automatic review settings February 5, 2026 13:16
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Feb 5, 2026
Copy link
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 updates the OpenAI Responses chat client to handle streaming code interpreter content by yielding deltas incrementally rather than emitting the code in bulk only at the end of the response. This enables real-time streaming of code generation, improving the user experience.

Changes:

  • Added streaming support for code interpreter delta updates, creating CodeInterpreterToolCallContent for each delta as it arrives
  • Extracted the Python media type string into a reusable constant (PythonMediaType)
  • Added comprehensive test coverage to verify delta streaming behavior and proper coalescing of deltas into final responses

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs Added handling for StreamingResponseCodeInterpreterCallCodeDeltaUpdate to yield deltas incrementally; inlined code interpreter content creation logic (removing helper method); updated OutputItemDone handler to only yield result content (assuming deltas handled the call content)
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIClientExtensions.cs Extracted Python media type string into a constant PythonMediaType
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIAssistantsChatClient.cs Updated to use the new PythonMediaType constant
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIResponseClientTests.cs Updated existing tests with exact code assertions; added new comprehensive test CodeInterpreterTool_Streaming_YieldsCodeDeltas to verify streaming delta behavior and proper coalescing

Comment on lines 235 to 246
message.Contents.Add(new CodeInterpreterToolResultContent
{
CallId = cicri.Id,
Outputs = cicri.Outputs is { Count: > 0 } outputs ? outputs.Select<CodeInterpreterCallOutput, AIContent?>(o =>
o switch
{
CodeInterpreterCallImageOutput cicio => new UriContent(cicio.ImageUri, OpenAIClientExtensions.ImageUriToMediaType(cicio.ImageUri)) { RawRepresentation = cicio },
CodeInterpreterCallLogsOutput ciclo => new TextContent(ciclo.Logs) { RawRepresentation = ciclo },
_ => null,
}).OfType<AIContent>().ToList() : null,
RawRepresentation = cicri,
});
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The logic for creating CodeInterpreterToolResultContent with output mapping is duplicated between the non-streaming path (lines 235-246) and the streaming path (lines 471-482). Consider extracting this into a helper method to reduce duplication and improve maintainability. This would ensure that any future changes to the output mapping logic only need to be made in one place.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant