Skip to content

Python: fix AG-UI tool history replay#5856

Open
he-yufeng wants to merge 3 commits into
microsoft:mainfrom
he-yufeng:fix/agui-flush-pending-tool-history
Open

Python: fix AG-UI tool history replay#5856
he-yufeng wants to merge 3 commits into
microsoft:mainfrom
he-yufeng:fix/agui-flush-pending-tool-history

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Summary

  • flush pending AG-UI tool calls before a new assistant message starts
  • flush any remaining pending tool calls when replay history ends
  • cover consecutive assistant tool-call replay and history-ending tool calls

Fixes #5855.

To verify

  • uv run pytest packages\ag-ui\tests\ag_ui\test_message_adapters.py -q --basetemp .tmp\pytest -p no:cacheprovider
  • uv run ruff check packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • uv run ruff format --check packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • uv run python -m py_compile packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 14, 2026 16:03
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

Fixes AG-UI history replay producing invalid assistant/tool sequences by ensuring any pending assistant tool calls are always followed by tool-result messages, even when the next message is another assistant message or when history replay ends.

Changes:

  • Added a flush_pending_tool_calls() helper in _sanitize_tool_history() and invoked it before new assistant messages and at history end.
  • Reused the flush helper for the existing “user follow-up while tools pending” case to consolidate logic.
  • Added regression tests covering consecutive assistant tool-call replay and end-of-history flushing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/ag-ui/agent_framework_ag_ui/_message_adapters.py Adds centralized pending-tool-call flushing to inject synthetic tool results before assistant messages and at history end.
python/packages/ag-ui/tests/ag_ui/test_message_adapters.py Adds tests for consecutive assistant tool calls and history-ending pending tool calls.

return

logger.info(f"{reason} with {len(pending_tool_call_ids)} pending tool calls - injecting synthetic results")
for pending_call_id in pending_tool_call_ids:
@he-yufeng he-yufeng force-pushed the fix/agui-flush-pending-tool-history branch from 3ccd534 to 2de5dda Compare May 14, 2026 18:40
@he-yufeng
Copy link
Copy Markdown
Contributor Author

Rebased onto current main after the branch went behind.

Re-ran the focused checks locally:

  • uv run pytest packages\ag-ui\tests\ag_ui\test_message_adapters.py -q --basetemp .tmp\pytest -p no:cacheprovider (110 passed)
  • uv run ruff check packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • uv run ruff format --check packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • python -m py_compile packages\ag-ui\agent_framework_ag_ui\_message_adapters.py packages\ag-ui\tests\ag_ui\test_message_adapters.py
  • git diff --check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: AG-UI history replay can send invalid assistant/tool sequence to OpenAI (tool_calls without matching tool messages)

3 participants