{"record":{"id":"8136d2cddc7c69fd","repo":"zylon-ai/private-gpt","slug":"cannot-set-both-add-generation-prompt-and-conti","errorCode":null,"errorMessage":"Cannot set both `add_generation_prompt` and `continue_final_message` to True.","messagePattern":"Cannot set both `add_generation_prompt` and `continue_final_message` to True\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"private_gpt/components/llm/tokenizers/mistral.py","lineNumber":223,"sourceCode":"def _prepare_apply_chat_template_tools_and_messages(\n    messages: list[dict[str, Any]],\n    tools: list[dict[str, Any]] | None = None,\n    continue_final_message: bool = False,\n    add_generation_prompt: bool = False,\n) -> tuple[list[dict[str, Any]], list[dict[str, Any]] | None]:\n    \"\"\"Prepare messages and tools for Mistral's chat template format.\n\n    Handles validation and formatting of messages and tools to ensure\n    compatibility with Mistral's requirements.\n    \"\"\"\n    tool_calls_module = _load_mistral_module(\n        \"mistral_common.protocol.instruct.tool_calls\"\n    )\n    Function = tool_calls_module.Function\n    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        )","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/zylon-ai/private-gpt/blob/4a030776a31a901ad80b1bf4d7faa2c1a367efbb/private_gpt/components/llm/tokenizers/mistral.py#L205-L241","documentation":"Before formatting messages for mistral-common, _prepare_apply_chat_template_tools_and_messages validates mutually exclusive flags: add_generation_prompt (start a new assistant turn) and continue_final_message (extend the last assistant turn) cannot both be True, mirroring HF tokenizer semantics, and raises ValueError immediately.","triggerScenarios":"Calling the mistral tokenizer's apply_chat_template (messages, add_generation_prompt=True, continue_final_message=True) — usually a caller blindly forwarding both kwargs or copying defaults from another API.","commonSituations":"Adapters translating OpenAI-style 'continue' requests into chat-template calls; default-argument dicts shared across call sites; refactors that started passing continue_final_message without clearing add_generation_prompt.","solutions":["Set exactly one flag: add_generation_prompt=True for normal generation, or continue_final_message=True to extend the last assistant message.","Audit call sites that forward **kwargs into apply_chat_template and clamp/validate the pair there.","Add an assert/guard upstream so the invalid combination never reaches the tokenizer."],"exampleFix":"# before\nout = tok.apply_chat_template(msgs, add_generation_prompt=True, continue_final_message=True)\n\n# after\nout = tok.apply_chat_template(msgs, add_generation_prompt=True, continue_final_message=False)","handlingStrategy":"validation","validationCode":"def valid_template_flags(add_generation_prompt: bool, continue_final_message: bool) -> bool:\n    return not (add_generation_prompt and continue_final_message)\n\nassert valid_template_flags(agg, cont)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat the two flags as a single enum choice, not independent booleans.","Centralize chat-template calls behind one helper that enforces the invariant.","Add unit tests covering the mutually exclusive flag pairs."],"tags":["mistral","chat-template","api-misuse","validation"],"backgroundTag":null,"analyzedSha":"4a030776a31a901ad80b1bf4d7faa2c1a367efbb","analyzedAt":"2026-08-15T03:51:26.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}