feat: support models requiring strict alternating user-assistant roles#7571
Open
Ricardo-M-L wants to merge 1 commit intomicrosoft:mainfrom
Open
feat: support models requiring strict alternating user-assistant roles#7571Ricardo-M-L wants to merge 1 commit intomicrosoft:mainfrom
Ricardo-M-L wants to merge 1 commit intomicrosoft:mainfrom
Conversation
Add support for model APIs (DeepSeek R1, Mistral) that reject requests with consecutive same-role messages. This implements automatic message merging/injection to ensure strict user-assistant alternation. Changes: - Add requires_alternating_roles field to ModelInfo TypedDict - Add ModelFamily.requires_alternating_roles() static method - Add ensure_alternating_roles() utility function that merges consecutive same-role messages - Update AssistantAgent._get_compatible_context() to enforce alternation - Update SelectorGroupChat._select_speaker() to enforce alternation - Add comprehensive tests (15+ test cases) Fixes microsoft#5965 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
@Ricardo-M-L please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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
requires_alternating_rolesfield inModelInfoand a family-based check inModelFamily.requires_alternating_roles()Changes
autogen-core(models)ModelInfo: Added optionalrequires_alternating_roles: boolfieldModelFamily: Addedrequires_alternating_roles()static method that returnsTrueforR1,MISTRAL,MINISTRAL,CODESRAL,OPEN_CODESRAL_MAMBA, andPIXTRALfamiliesautogen-agentchat(agents & teams)ensure_alternating_roles()utility function inutils/_utils.py:UserMessages into oneAssistantMessages (includingthoughtfields)FunctionExecutionResultMessageas user-role, merging when adjacent to other user-role messagesSystemMessages toUserMessagesAssistantAgent._get_compatible_context(): Now applies alternating role enforcement when the model requires it (via explicit flag or family detection)SelectorGroupChat._select_speaker(): Now applies alternating role enforcement to selector messages before sending to the modelTests (
test_alternating_roles.py)FunctionExecutionResultMessagehandlingModelFamily.requires_alternating_roles()for all familiesModelInfofield presence/absenceFixes #5965
Test plan
ensure_alternating_rolescorrectly merges consecutive same-role messagesAssistantAgentapplies alternation for models withrequires_alternating_roles=TrueSelectorGroupChatapplies alternation for affected model families🤖 Generated with Claude Code