{"record":{"id":"13ed239690c27963","repo":"microsoft/semantic-kernel","slug":"multiple-system-messages-in-chat-history-only-one","errorCode":null,"errorMessage":"Multiple system messages in chat history. Only one system message is expected.","messagePattern":"Multiple system messages in chat history\\. Only one system message is expected\\.","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/google/shared_utils.py","lineNumber":23,"sourceCode":"from typing import Any\n\nfrom semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceType\nfrom semantic_kernel.const import DEFAULT_FULLY_QUALIFIED_NAME_SEPARATOR\nfrom semantic_kernel.contents.chat_history import ChatHistory\nfrom semantic_kernel.contents.utils.author_role import AuthorRole\nfrom semantic_kernel.exceptions.service_exceptions import ServiceInvalidRequestError\n\nlogger: logging.Logger = logging.getLogger(__name__)\n\n\ndef filter_system_message(chat_history: ChatHistory) -> str | None:\n    \"\"\"Filter the first system message from the chat history.\n\n    If there are multiple system messages, raise an error.\n    If there are no system messages, return None.\n    \"\"\"\n    if len([message for message in chat_history if message.role == AuthorRole.SYSTEM]) > 1:\n        raise ServiceInvalidRequestError(\n            \"Multiple system messages in chat history. Only one system message is expected.\"\n        )\n\n    for message in chat_history:\n        if message.role == AuthorRole.SYSTEM:\n            return message.content\n\n    return None\n\n\nFUNCTION_CHOICE_TYPE_TO_GOOGLE_FUNCTION_CALLING_MODE = {\n    FunctionChoiceType.AUTO: \"AUTO\",\n    FunctionChoiceType.NONE: \"NONE\",\n    FunctionChoiceType.REQUIRED: \"ANY\",\n}\n\n# The separator used in the fully qualified name of the function instead of the default \"-\" separator.\n# This is required since Gemini doesn't work well with \"-\" in the function name.","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/google/shared_utils.py#L5-L41","documentation":"Raised by filter_system_message when more than one message with AuthorRole.SYSTEM exists in the ChatHistory. The Gemini API accepts a single system_instruction in the generate_content config; it does not support multiple system messages interleaved in the conversation. filter_system_message extracts the first system message for the config and rejects the presence of additional ones.","triggerScenarios":"Adding two or more messages with role=AuthorRole.SYSTEM to a ChatHistory and then calling a Google AI chat completion method (which calls filter_system_message on the history).","commonSituations":"Prepending a system prompt to history that already contains a system message; accumulating conversation turns across multiple calls without clearing prior system messages; merging two ChatHistory objects that each had a system message.","solutions":["Keep exactly one system message in the ChatHistory; update its content in place instead of adding a second.","Before sending, remove all but the first system message: filter and re-insert a single consolidated system message.","If you need multi-part instructions, concatenate them into a single system message's text."],"exampleFix":"# before\nhistory.add_system_message('You are a helpful assistant.')\nhistory.add_system_message('Always be concise.')  # second system message\n\n# after\nhistory.add_system_message('You are a helpful assistant. Always be concise.')","handlingStrategy":"validation","validationCode":"from semantic_kernel.contents.utils.author_role import AuthorRole\n\ndef validate_single_system_message(chat_history):\n    system_msgs = [m for m in chat_history if m.role == AuthorRole.SYSTEM]\n    if len(system_msgs) > 1:\n        raise ValueError(f'Found {len(system_msgs)} system messages; Google AI allows only one.')","typeGuard":"from semantic_kernel.contents.utils.author_role import AuthorRole\n\ndef has_at_most_one_system_message(chat_history) -> bool:\n    return sum(1 for m in chat_history if m.role == AuthorRole.SYSTEM) <= 1","tryCatchPattern":null,"preventionTips":["Maintain a single system message; update its content in place rather than appending another.","Consolidate multi-part instructions into one system message string.","Before each Google AI request, assert at most one system message exists in history."],"tags":["google-ai","chat-history","system-message","system-instruction","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}