Python: re-role trailing assistant message to user for Anthropic compatibility (fixes #5008) - #6207
Conversation
There was a problem hiding this comment.
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
Nonetool arguments by disablingexclude_noneduring 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. |
…ynthetic user turn)
|
please address the comments hanhan761 |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
All Copilot review comments have been addressed and resolved:
Eduard van Valkenburg (@eavanvalkenburg) This is ready for re-review. |
Eduard van Valkenburg (eavanvalkenburg)
left a comment
There was a problem hiding this comment.
There are changes unrelated to the stated goal of this PR in here, touching the core package, please remove
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.
|
Closing. Re-open if/when necessary. |
|
Ah, I see the core changes were reverted. Apologies. |
|
hanhan761 please fix the failing code quality check: FAILED: pyright in packages/anthropic |
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>
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:
Related
Fixes #5008