{"record":{"id":"f61b771adad8ebc8","repo":"langchain-ai/langchain","slug":"invalid-placeholder-template-var-name-wrapped","errorCode":null,"errorMessage":"Invalid placeholder template: {var_name_wrapped}. Expected a variable name surrounded by curly braces.","messagePattern":"Invalid placeholder template: (.+?)\\. Expected a variable name surrounded by curly braces\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":1405,"sourceCode":"                    \" Expected either a single string variable name\"\n                    \" or a list of [variable_name: str, is_optional: bool].\"\n                    f\" Got: {template}\"\n                )\n                raise ValueError(msg) from e\n\n            if not isinstance(is_optional, bool):\n                msg = f\"Expected is_optional to be a boolean. Got: {is_optional}\"\n                raise ValueError(msg)  # noqa: TRY004\n\n            if not isinstance(var_name_wrapped, str):\n                msg = f\"Expected variable name to be a string. Got: {var_name_wrapped}\"\n                raise ValueError(msg)  # noqa: TRY004\n            if var_name_wrapped[0] != \"{\" or var_name_wrapped[-1] != \"}\":\n                msg = (\n                    f\"Invalid placeholder template: {var_name_wrapped}.\"\n                    \" Expected a variable name surrounded by curly braces.\"\n                )\n                raise ValueError(msg)\n            var_name = var_name_wrapped[1:-1]\n\n            message = MessagesPlaceholder(variable_name=var_name, optional=is_optional)\n    else:\n        msg = (\n            f\"Unexpected message type: {message_type}. Use one of 'human',\"\n            f\" 'user', 'ai', 'assistant', or 'system'.\"\n        )\n        raise ValueError(msg)\n    return message\n\n\ndef _convert_to_message_template(\n    message: MessageLikeRepresentation,\n    template_format: PromptTemplateFormat = \"f-string\",\n) -> BaseMessage | BaseMessagePromptTemplate | BaseChatPromptTemplate:\n    \"\"\"Instantiate a message from a variety of message formats.\n","sourceCodeStart":1387,"sourceCodeEnd":1423,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L1387-L1423","documentation":"Raised in _create_template_from_message_type when the structured placeholder spec's variable name is a string but does not start with '{' and end with '}'. The list form has the same brace requirement as the plain string form: [\"{name}\", optional].","triggerScenarios":"`(\"placeholder\", [\"history\", True])` — bare name without braces; or [\"{history\", True] with mismatched braces.","commonSituations":"Assuming the list form takes the bare variable name because MessagesPlaceholder(variable_name=...) does; template strings assembled by f-string that drop or double braces.","solutions":["Wrap the name in single braces: [\"{history}\", True].","Remember in f-strings braces must be doubled or built as \"{\" + name + \"}\".","Or use MessagesPlaceholder(variable_name=\"history\", optional=True) directly, which takes the bare name."],"exampleFix":"# before\nChatPromptTemplate.from_messages([(\"placeholder\", [\"history\", True])])\n\n# after\nfrom langchain_core.prompts import MessagesPlaceholder\nChatPromptTemplate.from_messages([MessagesPlaceholder(\"history\", optional=True)])","handlingStrategy":"validation","validationCode":"def valid_braced_name(name):\n    return isinstance(name, str) and len(name) >= 2 and name[0] == \"{\" and name[-1] == \"}\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use MessagesPlaceholder(variable_name=...) which accepts the bare name and avoids brace pitfalls.","In f-strings, build brace-wrapped names as \"{\" + name + \"}\"."],"tags":["prompts","messages-placeholder","from-messages"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}