{"record":{"id":"827451cfd272b533","repo":"langchain-ai/langchain","slug":"invalid-variable-name-var-r-in-f-string-template-827451","errorCode":null,"errorMessage":"Invalid variable name {var!r} in f-string template. Variable names cannot be all digits as they are interpreted as positional arguments.","messagePattern":"Invalid variable name (.+?) in f-string template\\. Variable names cannot be all digits as they are interpreted as positional arguments\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/string.py","lineNumber":248,"sourceCode":"def validate_f_string_template(template: str) -> list[str]:\n    \"\"\"Validate an f-string template and return its input variables.\"\"\"\n    input_variables = set()\n    for var, format_spec in _parse_f_string_fields(template):\n        if \".\" in var or \"[\" in var or \"]\" in var:\n            msg = (\n                f\"Invalid variable name {var!r} in f-string template. \"\n                f\"Variable names cannot contain attribute \"\n                f\"access (.) or indexing ([]).\"\n            )\n            raise ValueError(msg)\n\n        if var.isdigit():\n            msg = (\n                f\"Invalid variable name {var!r} in f-string template. \"\n                f\"Variable names cannot be all digits as they are interpreted \"\n                f\"as positional arguments.\"\n            )\n            raise ValueError(msg)\n\n        if format_spec and (\"{\" in format_spec or \"}\" in format_spec):\n            msg = (\n                \"Invalid format specifier in f-string template. \"\n                \"Nested replacement fields are not allowed.\"\n            )\n            raise ValueError(msg)\n\n        input_variables.add(var)\n\n    return sorted(input_variables)\n\n\ndef check_valid_template(\n    template: str, template_format: str, input_variables: list[str]\n) -> None:\n    \"\"\"Check that template string is valid.\n","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/string.py#L230-L266","documentation":"Raised by `validate_f_string_template` when an f-string template's variable name consists only of digits, e.g. `{0}` or `{1}`. In Python's format protocol such fields are positional arguments, but LangChain invokes formatting with keyword arguments derived from `input_variables`, so purely numeric names are unusable and rejected with `ValueError`.","triggerScenarios":"A template like `\"{0} said: {1}\"` passed to `PromptTemplate.from_template(...)` (default f-string format), or to `validate_f_string_template` / `get_template_variables(..., \"f-string\")`. The `var.isdigit()` check fires for each parsed field.","commonSituations":"Translating `str.format`-style code or `.format(*args)` call sites into prompt templates; auto-generated templates that number placeholders; templates converted from CSV/JSON resources that use positional markers.","solutions":["Rename the placeholders to descriptive identifiers: `\"{speaker} said: {utterance}\"`, and pass those names as format kwargs","If the values genuinely come from a list, unpack them into named locals first (`speaker, utterance = rows[0]`) and format with names","If positional formatting must be preserved, format the string yourself before constructing the template"],"exampleFix":"# before\nprompt = PromptTemplate.from_template(\"{0}: {1}\")\n\n# after\nprompt = PromptTemplate.from_template(\"{speaker}: {utterance}\")\nprompt.format(speaker=\"Ada\", utterance=\"hello\")","handlingStrategy":"validation","validationCode":"import re\n\ndef has_no_numeric_vars(template: str) -> bool:\n    return not any(v.isdigit() for v in re.findall(r\"\\{(\\w+)\\}\", template))\n\nassert has_no_numeric_vars(template)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always name placeholders descriptively in prompt templates","Auto-generate templates with named fields, never positional indices","Convert `{0}`-style strings to `{name}` before passing to from_template"],"tags":["prompts","f-string","template-validation","valueerror","naming"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}