{"record":{"id":"f8603ad1cc541d05","repo":"langchain-ai/langchain","slug":"expected-is-optional-to-be-a-boolean-got-is-opt","errorCode":null,"errorMessage":"Expected is_optional to be a boolean. Got: {is_optional}","messagePattern":"Expected is_optional to be a boolean\\. Got: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":1395,"sourceCode":"                )\n                raise ValueError(msg)\n            var_name = template[1:-1]\n            message = MessagesPlaceholder(variable_name=var_name, optional=True)\n        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        )","sourceCodeStart":1377,"sourceCodeEnd":1413,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L1377-L1413","documentation":"Raised in _create_template_from_message_type when a structured placeholder spec [\"{name}\", is_optional] is given but the second element is not a bool. LangChain explicitly type-checks the optional flag because truthy values like 1 or \"yes\" would silently behave as positional/keyword confusion later.","triggerScenarios":"`(\"placeholder\", [\"{history}\", 1])`, `(\"placeholder\", [\"{history}\", \"true\"])`, or `(\"placeholder\", [\"{history}\", None])` inside from_messages.","commonSituations":"JSON/YAML-config-driven prompt definitions where booleans arrive as strings or 0/1 integers after deserialization; passing a numpy bool or an Optional flag from a config schema.","solutions":["Coerce the flag to a real Python bool: bool(value) or fix the config to emit true/false.","When loading prompts from JSON, normalize with `spec[1] = bool(spec[1])` before calling from_messages.","Prefer the plain string form (\"placeholder\", \"{name}\") if you don't need to control optionality."],"exampleFix":"# before\nChatPromptTemplate.from_messages([(\"placeholder\", [\"{history}\", \"true\"])])\n\n# after\nChatPromptTemplate.from_messages([(\"placeholder\", [\"{history}\", True])])","handlingStrategy":"validation","validationCode":"def normalize_placeholder_spec(spec):\n    name, optional = spec\n    if not isinstance(optional, bool):\n        spec[1] = bool(optional)\n    return spec","typeGuard":"def is_bool_optional(spec) -> bool:\n    return len(spec) == 2 and isinstance(spec[1], bool)","tryCatchPattern":null,"preventionTips":["Normalize booleans after loading prompts from JSON/YAML (\"true\"/1 are not bool).","Use JSON schema validation with type: boolean for config-driven prompts."],"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"}