Skip to content

Conversation

@moonbox3
Copy link
Contributor

Motivation and Context

When using a thread with WorkflowAgent, the second call to run_stream fails with an OpenAI error:

Error: service failed to complete the prompt: Error code: 400 - {'error': {'message': "An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_F09je20iUue6DlFRDLLh3dGK", 'type': 'invalid_request_error', 'param': 'messages.[3].role', 'code': None}}

The root cause is in WorkflowAgent.merge_updates(). FunctionResultContent updates lack a response_id, so they were being added to global_dangling and appended at the end of messages instead of immediately after their corresponding FunctionCallContent.

Incorrect ordering (before fix):

- User Question
- FunctionCallContent
- Assistant Answer
- FunctionResultContent  <-- Wrong position

Correct ordering (after fix):

- User Question
- FunctionCallContent
- FunctionResultContent
- Assistant Answer

Description

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@moonbox3 moonbox3 self-assigned this Jan 12, 2026
Copilot AI review requested due to automatic review settings January 12, 2026 03:31
@moonbox3 moonbox3 added python workflows Related to Workflows in agent-framework labels Jan 12, 2026
@moonbox3 moonbox3 changed the title Python: fix(core): simplify FunctionResultContent ordering in WorkflowAgent.merge_updates Python: fix(core): correct FunctionResultContent ordering in WorkflowAgent.merge_updates Jan 12, 2026
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Jan 12, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _agent.py2834384%61, 69–75, 103–104, 260, 316, 330, 343, 392–395, 401, 407, 411–412, 415–421, 425–426, 495, 502, 508–509, 515, 527, 559, 566, 587, 596, 600, 611
TOTAL17658289683% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2620 154 💤 0 ❌ 0 🔥 1m 0s ⏱️

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 WorkflowAgent.merge_updates() where FunctionResultContent updates without a response_id were incorrectly placed at the end of messages, causing OpenAI API validation errors.

Changes:

  • Modified the merge logic to map FunctionResultContent to their corresponding FunctionCallContent via call_id matching
  • Added comprehensive unit tests covering single and multiple function call/result scenarios, plus edge cases

Reviewed changes

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

File Description
python/packages/core/agent_framework/_workflows/_agent.py Implemented two-pass algorithm: first pass builds call_id→response_id mapping from FunctionCallContent, second pass associates FunctionResultContent with matching calls
python/packages/core/tests/workflow/test_workflow_agent.py Added three comprehensive tests validating correct ordering, multiple function calls, and orphan result handling

@moonbox3 moonbox3 enabled auto-merge January 12, 2026 06:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python workflows Related to Workflows in agent-framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Python: WorkflowAgent Thread FunctionResultContent out of order

3 participants