Skip to content

python : Honor merge_mode when streaming function call arguments in ResponsesHostServe - #10259

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
PratikWayase:fix/ResponsesHostServer-duplicates-function-call
Nov 11, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
PratikWayase:fix/ResponsesHostServer-duplicates-function-call

Conversation

@PratikWayase

Copy link
Copy Markdown
Contributor

Motivation & Context

This fixes the bug reported in #9980. When a model streams a tool call, its arguments
arrive as incremental fragments followed by one final complete snapshot. On the producer
side the fragments are marked Content.merge_mode="append" and the snapshot is marked
merge_mode="replace".

_OutputItemTracker.handle in agent_framework_foundry_hosting/_responses.py ignored
merge_mode and appended every function call arguments update it saw. A streamed call
therefore reached the client with its arguments duplicated, for example
{"location":"Seattle"}{"location":"Seattle"}. That string is not valid JSON, so tool
execution downstream breaks even though the HTTP response reports success with a 200.

Text updates in the same class already handle this correctly. The arguments branch just
never went through that path.

Description & Review Guide

Major changes

  • The function_call and mcp_server_tool_call branches of _OutputItemTracker.handle
    now pass the arguments string through the existing _consume_text_update helper
    instead of appending unconditionally:
    • append: appended as before
    • replace: the buffer is replaced with the snapshot and only the part that was not
      streamed yet is emitted as a delta. If the snapshot matches what was already
      streamed, nothing extra goes out
    • a replace snapshot that diverges from what was streamed raises
      _PublicResponseFailure, which surfaces as a response.failed event, the same
      behavior divergent text already has
    • merge_mode=None (legacy or single update) behaves exactly as before
  • Deltas are only emitted when there is an actual delta, so a call without arguments no
    longer produces an empty response.function_call_arguments.delta event
  • _close() is untouched. It joins _accumulated, which now holds the final snapshot
    exactly once

Impact

  • A completed function_call item now carries its arguments exactly once, as valid JSON
  • There is no change for legacy updates without merge_mode. Only the duplicated emission is removed. Requests without streaming reuse the same tracker, so they are covered as well
  • The MCP branch is the same parity change. Core does not currently tag mcp_server_tool_call updates with merge_mode, so that hunk is defensive for now. Both branches stay symmetric so they cannot drift apart

Reviewer focus

  • The two parallel hunks in _OutputItemTracker.handle, and whether reusing the existing text policy for arguments is the right approach
  • The six new regression tests in TestStreaming in tests/test_responses.py: an equal snapshot is not sent again, a snapshot that extends the fragments streams only the suffix, a stream that contains only a replace snapshot, a divergent snapshot fails the stream with response.failed, a call without arguments emits no empty delta, and the non streaming path

Related Issue

Fixes #9980

AI Assistance

  • No material AI assistance was used
  • This contribution used AI assistance. I reviewed, understood, and verified all submitted content and accept responsibility for it

AI assistance details: this PR description was drafted with AI assistance. I reviewed and verified all of it before submitting.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue
    section documents a trivial change or repository automation exception
  • This is not a breaking change. If it is a breaking change, add the
    breaking change label (or add "[BREAKING]" to the title prefix, before or after any
    language prefix). A workflow keeps the label and title prefix in sync automatically

Copilot AI balanced review requested due to automatic review settings November 11, 2026 06:17
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 11, 2026
@PratikWayase pratik wayase (PratikWayase) changed the title [Python] ==85onor merge_mode when streaming function call arguments in ResponsesHostServe python : onor merge_mode when streaming function call arguments in ResponsesHostServe Nov 11, 2026
@github-actions github-actions Bot changed the title python : onor merge_mode when streaming function call arguments in ResponsesHostServe Python: [Python] ==85onor merge_mode when streaming function call arguments in ResponsesHostServe Nov 11, 2026
@PratikWayase pratik wayase (PratikWayase) changed the title Python: [Python] ==85onor merge_mode when streaming function call arguments in ResponsesHostServe python : Honor merge_mode when streaming function call arguments in ResponsesHostServe Nov 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the change that honors argument merge modes to prevent duplicated streamed JSON.

No open issues 🎉

What changed in this PR

Fixes #9980 by honoring Content.merge_mode when the hosting adapter assembles tool-call arguments, preventing replacement snapshots from duplicating streamed JSON.

Changes:

  • Reuses existing merge handling for function-call and MCP arguments, emitting only nonempty deltas.
  • Adds six regression tests covering snapshot reconciliation, divergent-snapshot failure, empty arguments, and non-streaming responses.
File Description
python/​packages/​foundry_hosting/​tests/​test_responses.py Adds regression coverage for argument snapshot handling.
python/​packages/​foundry_hosting/​agent_framework_foundry_hosting/​_responses.py Applies merge-mode handling to function-call and MCP argument updates.

Review effort: Balanced

Merged via the queue into microsoft:main with commit e77de4a Nov 11, 2026
57 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — 04cbba5d Deployed Nov 11, 2026 by PratikWayase via add_label #27569
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: ResponsesHostServer duplicates function-call arguments by appending replacement updates

3 participants