Skip to content

Commit 1de07ab

Browse files
SongChiYoungekzhu
andauthored
Generalize Continuous SystemMessage merging via model_info[“multiple_system_messages”] instead of startswith("gemini-") (#6345)
The current implementation of consecutive `SystemMessage` merging applies only to models where `model_info.family` starts with `"gemini-"`. Since PR #6327 introduced the `multiple_system_messages` field in `model_info`, we can now generalize this logic by checking whether the field is explicitly set to `False`. This change replaces the hardcoded family check with a conditional that merges consecutive `SystemMessage` blocks whenever `multiple_system_messages` is set to `False`. Test cases that previously depended on the `"gemini"` model family have been updated to reflect this configuration flag, and renamed accordingly for clarity. In addition, for consistency across conditional logic, a follow-up PR is planned to refactor the Claude-specific transformation condition (currently implemented via `create_args.get("model", "unknown").startswith("claude-")`) to instead use the existing `is_claude()`. Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
1 parent 99aac24 commit 1de07ab

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ def _process_create_args(
525525
if self.model_info["json_output"] is False and json_output is True:
526526
raise ValueError("Model does not support JSON output.")
527527

528-
if create_args.get("model", "unknown").startswith("gemini-"):
529-
# Gemini models accept only one system message(else, it will read only the last one)
530-
# So, merge system messages into one
528+
if not self.model_info.get("multiple_system_messages", False):
529+
# Some models accept only one system message(or, it will read only the last one)
530+
# So, merge system messages into one (if multiple and continuous)
531531
system_message_content = ""
532532
_messages: List[LLMMessage] = []
533533
_first_system_message_idx = -1
@@ -540,7 +540,9 @@ def _process_create_args(
540540
elif _last_system_message_idx + 1 != idx:
541541
# That case, system message is not continuous
542542
# Merge system messages only contiues system messages
543-
raise ValueError("Multiple and Not continuous system messages are not supported")
543+
raise ValueError(
544+
"Multiple and Not continuous system messages are not supported if model_info['multiple_system_messages'] is False"
545+
)
544546
system_message_content += message.content + "\n"
545547
_last_system_message_idx = idx
546548
else:

python/packages/autogen-ext/tests/models/test_openai_model_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,7 +2096,7 @@ async def test_muliple_system_message(model: str, openai_client: OpenAIChatCompl
20962096

20972097

20982098
@pytest.mark.asyncio
2099-
async def test_system_message_merge_for_gemini_models() -> None:
2099+
async def test_system_message_merge_with_continuous_system_messages_models() -> None:
21002100
"""Tests that system messages are merged correctly for Gemini models."""
21012101
# Create a mock client
21022102
mock_client = MagicMock()
@@ -2109,6 +2109,7 @@ async def test_system_message_merge_for_gemini_models() -> None:
21092109
"json_output": False,
21102110
"family": "unknown",
21112111
"structured_output": False,
2112+
"multiple_system_messages": False,
21122113
},
21132114
)
21142115

@@ -2157,6 +2158,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None:
21572158
"json_output": False,
21582159
"family": "unknown",
21592160
"structured_output": False,
2161+
"multiple_system_messages": False,
21602162
},
21612163
)
21622164

@@ -2180,7 +2182,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None:
21802182

21812183

21822184
@pytest.mark.asyncio
2183-
async def test_system_message_not_merged_for_non_gemini_models() -> None:
2185+
async def test_system_message_not_merged_for_multiple_system_messages_true() -> None:
21842186
"""Tests that system messages aren't modified for non-Gemini models."""
21852187
# Create a mock client
21862188
mock_client = MagicMock()
@@ -2193,6 +2195,7 @@ async def test_system_message_not_merged_for_non_gemini_models() -> None:
21932195
"json_output": False,
21942196
"family": "unknown",
21952197
"structured_output": False,
2198+
"multiple_system_messages": True,
21962199
},
21972200
)
21982201

0 commit comments

Comments
 (0)