{"record":{"id":"8a506ce91f498b87","repo":"mlflow/mlflow","slug":"template-variables-mismatch-in-prompt-name-mi","errorCode":null,"errorMessage":"Template variables mismatch in prompt '{name}'. Missing: {missing}.","messagePattern":"Template variables mismatch in prompt '(.+?)'\\. Missing: (.+?)\\.","errorType":"exception","errorClass":"MlflowException","httpStatus":null,"severity":"error","filePath":"mlflow/genai/optimize/optimizers/metaprompt_optimizer.py","lineNumber":428,"sourceCode":"\n    def _validate_template_variables(\n        self, original_prompts: dict[str, str], new_prompts: dict[str, str]\n    ) -> bool:\n        \"\"\"Validate that all template variables are preserved in new prompts.\n\n        Extra variables introduced by the LLM are automatically stripped from the\n        generated prompt so that optimisation can succeed even when the model\n        erroneously adds new ``{{variable}}`` patterns.\n        \"\"\"\n        original_vars = self._extract_template_variables(original_prompts)\n        new_vars = self._extract_template_variables(new_prompts)\n\n        for name in original_prompts:\n            missing = original_vars[name] - new_vars[name]\n            extra = new_vars[name] - original_vars[name]\n\n            if missing:\n                raise MlflowException(\n                    f\"Template variables mismatch in prompt '{name}'. Missing: {missing}.\"\n                )\n\n            if extra:\n                _logger.warning(f\"Stripping extra template variables {extra} from prompt '{name}'.\")\n                text = new_prompts[name]\n                for var in extra:\n                    text = text.replace(\"{{\" + var + \"}}\", \"\")\n                new_prompts[name] = text\n\n        return True\n\n    def _build_zero_shot_meta_prompt(\n        self,\n        current_prompts: dict[str, str],\n        template_variables: dict[str, set[str]],\n    ) -> str:\n        # Format the current prompts for each module","sourceCodeStart":410,"sourceCodeEnd":446,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/genai/optimize/optimizers/metaprompt_optimizer.py#L410-L446","documentation":"_validate_template_variables compares f-string style template variables (via string.Formatter) between original and improved prompts. If the improved prompt dropped a variable that the original required, this error names the prompt and the missing variables. It prevents silently breaking prompts whose variables are filled programmatically at inference time.","triggerScenarios":"optimize() produced an improved prompt that no longer contains a variable present in the original, e.g. original 'Answer {question} about {context}' improved to 'Answer {question}'.","commonSituations":"The reflection model paraphrases and removes placeholders; developers then hit KeyError at runtime when formatting the optimized prompt with their data.","solutions":["Adjust guidelines to instruct the reflection model to preserve all template variables","Re-add the missing variables manually to the improved prompt before use","Catch MlflowException and fall back to the original prompt text","Review the improved prompt in the error message and edit it to include the missing {vars}"],"exampleFix":"// before\nprompts = optimizer.optimize(eval_fn, data, {'q': 'Answer {question} in {language}'})\n// after\ntry:\n    prompts = optimizer.optimize(eval_fn, data, {'q': 'Answer {question} in {language}'})\nexcept MlflowException as e:\n    if 'Template variables mismatch' in str(e):\n        prompts = {'q': 'Answer {question} in {language}'}  # keep original\n    else:\n        raise","handlingStrategy":"try-catch","validationCode":"import string\nvars = {n: {v for _, v, _, _ in string.Formatter().parse(t) if v} for n, t in target_prompts.items()}","typeGuard":"def preserved_vars(orig: str, new: str) -> bool:\n    import string\n    ex = lambda s: {f for _, f, _, _ in string.Formatter().parse(s) if f}\n    return ex(orig) <= ex(new)","tryCatchPattern":"try:\n    prompts = optimizer.optimize(eval_fn, train_data, target_prompts)\nexcept MlflowException as e:\n    if 'Template variables mismatch' in str(e):\n        prompts = dict(target_prompts)\n    else:\n        raise","preventionTips":["Include variable-preservation rules in the optimizer guidelines","Validate improved prompts with string.Formatter before deploying","Keep originals handy for fallback"],"tags":["python","validation","templates","prompt-engineering"],"backgroundTag":"template-variable-mismatch","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}