Skip to content

A programming framework for agentic AI

Generalize Continuous SystemMessage merging via model_info[“multiple_system_messages”] instead of `startswith("gemini-")` (#6345)

EeS1de07ab293f0
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
index 84951e40c..ffe816e59 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
@@ -525,9 +525,9 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):
if self.model_info["json_output"] is False and json_output is True:
raise ValueError("Model does not support JSON output.")
- if create_args.get("model", "unknown").startswith("gemini-"):
- # Gemini models accept only one system message(else, it will read only the last one)
- # So, merge system messages into one
+ if not self.model_info.get("multiple_system_messages", False):
+ # Some models accept only one system message(or, it will read only the last one)
+ # So, merge system messages into one (if multiple and continuous)
system_message_content = ""
_messages: List[LLMMessage] = []
_first_system_message_idx = -1
@@ -540,7 +540,9 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):
elif _last_system_message_idx + 1 != idx:
# That case, system message is not continuous
# Merge system messages only contiues system messages
- raise ValueError("Multiple and Not continuous system messages are not supported")
+ raise ValueError(
+ "Multiple and Not continuous system messages are not supported if model_info['multiple_system_messages'] is False"
+ )
system_message_content += message.content + "\n"
_last_system_message_idx = idx
else:
diff --git a/python/packages/autogen-ext/tests/models/test_openai_model_client.py b/python/packages/autogen-ext/tests/models/test_openai_model_client.py
index 60d5547d0..263f57887 100644
--- a/python/packages/autogen-ext/tests/models/test_openai_model_client.py
+++ b/python/packages/autogen-ext/tests/models/test_openai_model_client.py
@@ -2096,7 +2096,7 @@ async def test_muliple_system_message(model: str, openai_client: OpenAIChatCompl
@pytest.mark.asyncio
-async def test_system_message_merge_for_gemini_models() -> None:
+async def test_system_message_merge_with_continuous_system_messages_models() -> None:
"""Tests that system messages are merged correctly for Gemini models."""
# Create a mock client
mock_client = MagicMock()
@@ -2109,6 +2109,7 @@ async def test_system_message_merge_for_gemini_models() -> None:
"json_output": False,
"family": "unknown",
"structured_output": False,
+ "multiple_system_messages": False,
},
)
@@ -2157,6 +2158,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None:
"json_output": False,
"family": "unknown",
"structured_output": False,
+ "multiple_system_messages": False,
},
)
@@ -2180,7 +2182,7 @@ async def test_system_message_merge_with_non_continuous_messages() -> None:
@pytest.mark.asyncio
-async def test_system_message_not_merged_for_non_gemini_models() -> None:
+async def test_system_message_not_merged_for_multiple_system_messages_true() -> None:
"""Tests that system messages aren't modified for non-Gemini models."""
# Create a mock client
mock_client = MagicMock()
@@ -2193,6 +2195,7 @@ async def test_system_message_not_merged_for_non_gemini_models() -> None:
"json_output": False,
"family": "unknown",
"structured_output": False,
+ "multiple_system_messages": True,
},
)