Skip to content

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

Merged
Evan Mattson (moonbox3) merged 7 commits into
microsoft:mainfrom
hanhan761:fix-5008-anthropic-assistant-prefill
Jun 19, 2026
Merged

Python: re-role trailing assistant message to user for Anthropic compatibility (fixes #5008)#6207
Evan Mattson (moonbox3) merged 7 commits into
microsoft:mainfrom
hanhan761:fix-5008-anthropic-assistant-prefill

Conversation

@hanhan761

Copy link
Copy Markdown
Contributor

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
@moonbox3 Evan Mattson (moonbox3) added the python Usage: [Issues, PRs], Target: Python label May 30, 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.

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

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py4663692%463, 466, 547, 640, 642, 726, 816, 852–853, 931, 933, 963–964, 1009, 1025–1026, 1033–1035, 1039–1041, 1045–1048, 1164, 1174, 1226, 1374–1375, 1392, 1405, 1418, 1443–1444
TOTAL40064448388% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8023 34 💤 0 ❌ 0 🔥 2m 10s ⏱️

@hanhan761

Copy link
Copy Markdown
Contributor 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.

Eduard van Valkenburg (@eavanvalkenburg) This is ready for re-review.

Copy link
Copy Markdown
Member

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.
@moonbox3

Copy link
Copy Markdown
Contributor

Closing. Re-open if/when necessary.

@moonbox3

Copy link
Copy Markdown
Contributor

Ah, I see the core changes were reverted. Apologies.

@moonbox3

Copy link
Copy Markdown
Contributor

hanhan761 please fix the failing code quality check:

FAILED: pyright in packages/anthropic
/home/runner/work/agent-framework/agent-framework/python/packages/anthropic/agen
t_framework_anthropic/_chat_client.py
/home/runner/work/agent-framework/agent-framework/python/packages/anthropic/ag
ent_framework_anthropic/_chat_client.py:724:40 - error: Type of "get" is
partially unknown
Type of "get" is "Overload[(key: Unknown, default: None = None, /) ->
(Unknown | None), (key: Unknown, default: Unknown, /) -> Unknown, (key: Unknown,
default: _T@get, /) -> (Unknown | _T@get)]" (reportUnknownMemberType)
/home/runner/work/agent-framework/agent-framework/python/packages/anthropic/ag
ent_framework_anthropic/_chat_client.py:725:17 - error: Type of "item" is
unknown (reportUnknownVariableType)
2 errors, 0 warnings, 0 informations

Use object-typed content narrowing before checking Anthropic tool-use block types so strict Pyright no longer treats dynamic message content as Unknown.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Jun 19, 2026
Merged via the queue into microsoft:main with commit 2d0555c Jun 19, 2026
37 checks passed
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]: Workflow fails with Anthropic models — assistant message prefill not supported

4 participants