Skip to content

Conversation

@emilmuller
Copy link

Motivation and Context

  1. Why is this change required?
    AG-UI agents fail on the second turn after executing multiple tool calls, because the message history sent back to the provider violates the OpenAI tools protocol. The model rejects the request with a 400 error.

  2. What problem does it solve?
    When AG-UI streams tool calls, it emits several assistant messages with ToolCalls, followed later by several tool messages with results. When replayed in this order, the provider complains that assistant messages containing tool_calls are not immediately followed by tool responses. This PR ensures the backend normalizes the ordering so that each assistant tool call is directly followed by its corresponding tool result.

  3. What scenario does it contribute to?
    Enables multi-turn conversations involving multiple tool calls, which is a core scenario for agents. Without this fix, any multi-tool-turn conversation breaks on the next user turn.

Linked issue:
Fixes #2699

Description

This change introduces a preprocessing step inside MapAGUI that reorders incoming AGUIMessage instances before they are mapped into ChatMessages.

Copilot AI review requested due to automatic review settings December 8, 2025 14:55
@github-actions github-actions bot changed the title Fix AG-UI tool message ordering for multi-turn tool calls (#2699) .NET: Fix AG-UI tool message ordering for multi-turn tool calls (#2699) Dec 8, 2025
@emilmuller
Copy link
Author

@microsoft-github-policy-service agree company="DNV AS"

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 fixes a critical bug in AG-UI agents where multi-turn conversations with multiple tool calls would fail on the second turn with a 400 error from the OpenAI provider. The issue was caused by incorrect message ordering in the conversation history that violated the OpenAI tools protocol.

Key Changes

  • Introduces a FixToolMessageOrdering method that reorders AGUI messages before mapping to ChatMessages
  • Ensures each assistant message with tool calls is immediately followed by its corresponding tool result messages
  • Handles edge cases including orphaned tool messages and tool calls without matching results

emilmuller and others added 3 commits December 8, 2025 19:12
…pointRouteBuilderExtensions.cs

Co-authored-by: Copilot <[email protected]>
Added XML documentation and changed FixToolMessageOrdering to internal for broader usage. Introduced extensive unit tests to validate message reordering behavior in various scenarios. Updated MapAGUIAgent to use FixToolMessageOrdering for consistent production logic. Added InternalsVisibleTo attribute for test assembly access and included a copyright notice in AssemblyInfo.cs.
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.

.NET: AG-UI: Multi-turn tool calls replay produces invalid OpenAI tool_call history

3 participants