{"record":{"id":"b8d1e78bf1aab3a4","repo":"microsoft/semantic-kernel","slug":"system-messages-are-not-supported-in-gemma","errorCode":null,"errorMessage":"System messages are not supported in Gemma","messagePattern":"System messages are not supported in Gemma","errorType":"exception","errorClass":"ServiceInvalidRequestError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/ai/onnx/utils.py","lineNumber":167,"sourceCode":"    return phi4mm_input\n\n\ndef gemma_template(history: ChatHistory) -> str:\n    \"\"\"Generates a formatted string for the Gemma model based on the provided chat history.\n\n    Args:\n        history (ChatHistory): An object containing the chat history with messages.\n\n    Returns:\n        str: A formatted string representing the chat history for the Gemma model.\n\n    Raises:\n        ServiceInvalidRequestError: If a system message is encountered in the chat history.\n    \"\"\"\n    gemma_input = \"<bos>\"\n    for message in history.messages:\n        if message.role == AuthorRole.SYSTEM:\n            raise ServiceInvalidRequestError(\"System messages are not supported in Gemma\")\n        if message.role == AuthorRole.USER:\n            gemma_input += f\"<start_of_turn>user\\n{message.content}<end_of_turn>\\n\"\n        if message.role == AuthorRole.ASSISTANT:\n            gemma_input += f\"<start_of_turn>model\\n{message.content}<end_of_turn>\\n\"\n    gemma_input += \"<start_of_turn>model\\n\"\n    return gemma_input\n\n\ndef llama_template(history: ChatHistory) -> str:\n    \"\"\"Generates a formatted string from a given chat history for use with the LLaMA model.\n\n    Args:\n        history (ChatHistory): An object containing the chat history, which includes a list of messages.\n\n    Returns:\n        str: A formatted string where each message is wrapped with specific header and end tags,\n             and the final string ends with an assistant header tag.\n    \"\"\"","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/onnx/utils.py#L149-L185","documentation":"Google's Gemma models do not support system-role messages in their prompt format. The gemma_template() function explicitly checks every message's role and raises ServiceInvalidRequestError (an HTTP-400-equivalent client error) if any message has AuthorRole.SYSTEM. This is a hard API contract from the model, not a library limitation.","triggerScenarios":"Calling an ONNX Gemma chat completion with a ChatHistory that contains at least one message where role == AuthorRole.SYSTEM — typically from adding a system prompt via history.add_system_message() or a kernel function that auto-injects one.","commonSituations":"Using the same ChatHistory builder across models (a system prompt that works for GPT/Phi fails for Gemma); templated pipelines that always prepend a system instruction; copying examples designed for OpenAI models into a Gemma workflow.","solutions":["Remove or convert the system message to a user message before calling the Gemma service: merge the system instruction into the first user turn.","Filter system messages from ChatHistory right before the call: history.messages = [m for m in history.messages if m.role != AuthorRole.SYSTEM].","Use a different template/model (e.g. ONNXTemplate.PHI3) if you need native system-role support.","Guard at the application layer with a template-aware check so system messages are never sent to Gemma."],"exampleFix":"// before\nhistory.add_system_message(\"You are a helpful assistant.\")\nresult = gemma_service.get_chat_message_contents(history=history)\n// after\nsystem_msg = next((m for m in history.messages if m.role == AuthorRole.SYSTEM), None)\nif system_msg:\n    history.remove_message(system_msg)\n    history.messages.insert(0, ChatMessageContent(role=AuthorRole.USER, content=system_msg.content))\nresult = gemma_service.get_chat_message_contents(history=history)","handlingStrategy":"validation","validationCode":"from semantic_kernel.contents import AuthorRole\n\ndef strip_system_messages_for_gemma(history):\n    \"\"\"Remove system messages before sending to Gemma — merge into first user turn.\"\"\"\n    system_msgs = [m for m in history.messages if m.role == AuthorRole.SYSTEM]\n    if system_msgs:\n        history.messages = [m for m in history.messages if m.role != AuthorRole.SYSTEM]\n        merged = ' '.join(m.content for m in system_msgs)\n        if history.messages and history.messages[0].role == AuthorRole.USER:\n            history.messages[0].content = merged + '\\n' + history.messages[0].content\n    return history","typeGuard":"def is_gemma_compatible(history) -> bool:\n    return all(m.role != AuthorRole.SYSTEM for m in history.messages)","tryCatchPattern":"from semantic_kernel.exceptions import ServiceInvalidRequestError\n\ntry:\n    result = gemma_service.get_chat_message_contents(history=history)\nexcept ServiceInvalidRequestError as e:\n    if 'System messages' in str(e):\n        history = strip_system_messages_for_gemma(history)\n        result = gemma_service.get_chat_message_contents(history=history)","preventionTips":["Never add system-role messages to a ChatHistory destined for a Gemma model.","Build a model-aware pipeline that checks the target template before constructing history.","If you need system instructions for Gemma, embed them in the first user message."],"tags":["onnx","gemma","system-message","chat-history","semantic-kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}