{"record":{"id":"07206f673d635b11","repo":"zylon-ai/private-gpt","slug":"cannot-set-continue-final-message-to-true-when-t","errorCode":null,"errorMessage":"Cannot set `continue_final_message` to True when the last message is not from the assistant.","messagePattern":"Cannot set `continue_final_message` to True when the last message is not from the assistant\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/llm/tokenizers/mistral.py","lineNumber":238,"sourceCode":"    Tool = tool_calls_module.Tool\n\n    if add_generation_prompt and continue_final_message:\n        raise ValueError(\n            \"Cannot set both `add_generation_prompt` and \"\n            \"`continue_final_message` to True.\"\n        )\n\n    last_message = messages[-1]\n\n    if add_generation_prompt and last_message[\"role\"] == \"assistant\":\n        raise ValueError(\n            \"Cannot set `add_generation_prompt` to True when \"\n            \"the last message is from the assistant. Consider \"\n            \"using `continue_final_message` instead.\"\n        )\n\n    if continue_final_message and last_message[\"role\"] != \"assistant\":\n        raise ValueError(\n            \"Cannot set `continue_final_message` to True when \"\n            \"the last message is not from the assistant.\"\n        )\n\n    # Mistral-common requires AssistantMessage content to be string\n    # https://github.com/mistralai/mistral-common/blob/f4a06998b75ed78bbf5aaf569590b772ea26c9f6/src/mistral_common/protocol/instruct/messages.py#L80\n    for message in messages:\n        # Remove reasoning as unsupported by Mistral\n        _ = message.pop(\"thinking\", None)\n\n        # Convert assistant message content to string if needed\n        if message.get(\"role\") == \"assistant\":\n            content = message.get(\"content\")\n            if isinstance(content, list):\n                content = \"\\n\".join(chunk.get(\"text\") or \"\" for chunk in content)\n                message[\"content\"] = content\n\n    # Mistral requires \"parameters\" and \"description\" to be present even if empty","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/llm/tokenizers/mistral.py#L220-L256","documentation":"The inverse rule of the prefill validation: continue_final_message=True requires the last message to be an assistant message, because it extends that message in place. A trailing user/tool message with this flag is contradictory and raises ValueError in _prepare_apply_chat_template_tools_and_messages.","triggerScenarios":"apply_chat_template(messages, continue_final_message=True) where messages[-1]['role'] != 'assistant' (e.g. a normal user-turn conversation).","commonSituations":"Toggling continue_final_message based on a config flag while conversations still end with user messages; copy-pasting prefill code into a normal chat-completion path; client code defaulting the flag to True.","solutions":["Only set continue_final_message=True when the conversation actually ends with an assistant message you want extended.","For normal generation over a user turn, use add_generation_prompt=True.","Derive the flag from the last message role at the call site instead of hard-coding it."],"exampleFix":"# before\nout = tok.apply_chat_template(msgs, continue_final_message=True)  # msgs[-1] is user\n\n# after\nflag = msgs[-1]['role'] == 'assistant'\nout = tok.apply_chat_template(msgs, add_generation_prompt=not flag, continue_final_message=flag)","handlingStrategy":"validation","validationCode":"last = messages[-1]['role']\nassert not (continue_final_message and last != 'assistant'), 'continue_final_message needs trailing assistant message'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive continue_final_message from the last message's role instead of config flags.","Keep prefill logic in one code path with this validation.","Test both orderings (user-then-assistant, assistant-only) in CI."],"tags":["mistral","chat-template","prefill","validation"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}