Skip to content

Python: re-role trailing assistant message to user for Anthropic compatibility (fixes #5008)#6207

Open
hanhan761 wants to merge 5 commits into
microsoft:mainfrom
hanhan761:fix-5008-anthropic-assistant-prefill
Open

Python: re-role trailing assistant message to user for Anthropic compatibility (fixes #5008)#6207
hanhan761 wants to merge 5 commits into
microsoft:mainfrom
hanhan761:fix-5008-anthropic-assistant-prefill

Conversation

@hanhan761
Copy link
Copy Markdown

Summary

When two agents backed by Anthropic are connected via WorkflowBuilder, the first agent's output (assistant-role messages) is passed as context to the second agent. Anthropic's API rejects this with:

\
This model does not support assistant message prefill. The conversation must end with a user message.
\\

Changes

Modified _prepare_messages_for_anthropic\ in the Anthropic chat client:

  • After preparing all messages, check if the last message has role "assistant"
  • If so, re-role it as "user" so the conversation ends with a valid user message
  • Single-agent usage is unaffected (conversation already ends with user message in that flow)

Related

Fixes #5008

Copilot AI review requested due to automatic review settings May 30, 2026 07:39
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

Note

Copilot was unable to run its full agentic suite in this review.

Updates tool invocation argument handling to preserve None values, adds regression coverage, and adjusts Anthropic message preparation to satisfy its “last message must be user” constraint.

Changes:

  • Preserve explicit None tool arguments by disabling exclude_none during Pydantic dumps.
  • Add a regression test for tool invocation with explicit None.
  • Ensure Anthropic message lists end with a user message by re-labeling a trailing assistant message.

Reviewed changes

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

File Description
python/packages/core/tests/core/test_tools.py Adds a regression test around invoke() handling of explicit None arguments.
python/packages/core/agent_framework/_tools.py Changes Pydantic serialization to keep None values when building invocation kwargs.
python/packages/anthropic/agent_framework_anthropic/_chat_client.py Adjusts message preparation to ensure the final message role is user for Anthropic.

Comment thread python/packages/core/agent_framework/_tools.py
Comment thread python/packages/core/agent_framework/_tools.py Outdated
Comment thread python/packages/core/tests/core/test_tools.py Outdated
Comment thread python/packages/anthropic/agent_framework_anthropic/_chat_client.py Outdated
@eavanvalkenburg
Copy link
Copy Markdown
Member

please address the comments @hanhan761

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py4503592%461, 464, 545, 638, 640, 798, 834–835, 913, 915, 945–946, 991, 1007–1008, 1015–1017, 1021–1023, 1027–1030, 1144, 1154, 1206, 1354–1355, 1372, 1385, 1398, 1423–1424
packages/core/agent_framework
   _tools.py10147892%219–220, 395, 397, 410, 435–437, 445, 463, 477, 484, 491, 514, 516, 523, 531, 650, 685–687, 690–692, 694, 700, 752–754, 779, 805, 809, 847–849, 853, 875, 1018–1019, 1023, 1059, 1071, 1078–1081, 1102, 1106, 1110, 1124–1126, 1473, 1559, 1587, 1609, 1613, 1743, 1747, 1793, 1854–1855, 1958, 2011, 2031, 2033, 2089, 2152, 2324–2325, 2345, 2401–2402, 2540–2541, 2608, 2613, 2620
TOTAL37785440788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
7509 34 💤 0 ❌ 0 🔥 1m 58s ⏱️

@hanhan761
Copy link
Copy Markdown
Author

All Copilot review comments have been addressed and resolved:

  1. exclude_none → exclude_unset=True, exclude_none=False — applied to both invoke() and _auto_invoke_function() in _tools.py
  2. Test distinguishes omission from explicit None — test now uses a non-None default and verifies different outputs
  3. Re-role → append synthetic user turn_chat_client.py now appends {"role": "user", "content": "Continue"} instead of rewriting the assistant message. Docstring updated accordingly.

@eavanvalkenburg This is ready for re-review.

Copy link
Copy Markdown
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

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

There are changes unrelated to the stated goal of this PR in here, touching the core package, please remove

Comment thread python/packages/core/agent_framework/_tools.py Outdated
The exclude_none/exclude_unset changes in the core package are out of scope
for this Anthropic-specific fix. This PR now only contains the Anthropic
chat client docstring fix and the synthetic user turn append.
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]: Workflow fails with Anthropic models — assistant message prefill not supported

4 participants