Python: re-role trailing assistant message to user for Anthropic compatibility (fixes #5008)#6207
Open
hanhan761 wants to merge 2 commits into
Open
Conversation
Contributor
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. |
Comment on lines
636
to
640
| parsed_arguments = dict(arguments) | ||
| if self.input_model is not None and not self._schema_supplied: | ||
| parsed_arguments = self.input_model.model_validate(parsed_arguments).model_dump( | ||
| exclude_none=True | ||
| exclude_none=False | ||
| ) |
Comment on lines
645
to
+648
| and not isinstance(arguments, self.input_model) | ||
| ): | ||
| raise TypeError(f"Expected {self.input_model.__name__}, got {type(arguments).__name__}") | ||
| parsed_arguments = arguments.model_dump(exclude_none=True) | ||
| parsed_arguments = arguments.model_dump(exclude_none=False) |
Comment on lines
+1466
to
+1479
| def test_invoke_preserves_explicit_none_arguments() -> None: | ||
| """Optional parameters explicitly set to None must not be stripped before invocation.""" | ||
|
|
||
| @tool | ||
| def greet(name: str, greeting: str | None = None) -> str: | ||
| return f"{greeting or 'Hello'}, {name}!" | ||
|
|
||
| result = asyncio.run(greet.invoke(arguments={"name": "World", "greeting": None})) | ||
| assert isinstance(result, list) | ||
| assert result[0].text == "Hello, World!" | ||
|
|
||
| result = asyncio.run(greet.invoke(arguments={"name": "World"})) | ||
| assert isinstance(result, list) | ||
| assert result[0].text == "Hello, World!" |
Comment on lines
+712
to
+716
| # Anthropic requires the conversation to end with a user message. | ||
| # Re-role a trailing assistant message as user so chained agent | ||
| # outputs work as valid context for the next agent. | ||
| if result and result[-1].get("role") == "assistant": | ||
| result[-1] = {**result[-1], "role": "user"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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