{"record":{"id":"882e67fc1b55f97b","repo":"langchain-ai/langchain","slug":"expected-variable-name-to-be-a-string-got-var-n","errorCode":null,"errorMessage":"Expected variable name to be a string. Got: {var_name_wrapped}","messagePattern":"Expected variable name to be a string\\. Got: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":1399,"sourceCode":"        else:\n            try:\n                var_name_wrapped, is_optional = template\n            except ValueError as e:\n                msg = (\n                    \"Unexpected arguments for placeholder message type.\"\n                    \" 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","sourceCodeStart":1381,"sourceCodeEnd":1417,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L1381-L1417","documentation":"Raised in _create_template_from_message_type when the structured placeholder spec's first element (the variable name) is not a str, e.g. (\"placeholder\", [123, True]). The name must be a string like \"{history}\" so braces can be stripped.","triggerScenarios":"`(\"placeholder\", [42, True])`, or a config pipeline that yields the variable name as a Symbol/enum object instead of a string.","commonSituations":"Programmatically generated message specs where identifiers come from an enum, int ID, or object attribute that wasn't converted to str.","solutions":["Convert the first element to a string wrapped in braces: f\"{{{var_name}}}\".","Validate spec shape before calling from_messages when specs come from user/config input."],"exampleFix":"# before\nvar_id = 7\nChatPromptTemplate.from_messages([(\"placeholder\", [var_id, True])])\n\n# after\nvar_id = 7\nChatPromptTemplate.from_messages([(\"placeholder\", [f\"{{{var_id}}}\", True])])","handlingStrategy":"validation","validationCode":"def valid_placeholder_name(spec):\n    return isinstance(spec[0], str)","typeGuard":"def is_str_var_name(spec) -> bool:\n    return len(spec) == 2 and isinstance(spec[0], str)","tryCatchPattern":null,"preventionTips":["Convert enum/int identifiers to f\"{{{name}}}\" strings before building specs.","Keep message specs as plain Python literals in config, not object references."],"tags":["prompts","messages-placeholder","type-validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}