{"record":{"id":"60b455c50091ae20","repo":"langchain-ai/langchain","slug":"invalid-image-template-tmpl","errorCode":null,"errorMessage":"Invalid image template: {tmpl}","messagePattern":"Invalid image template: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/prompts/chat.py","lineNumber":514,"sourceCode":"                            template_format=template_format,\n                        )\n                    elif isinstance(img_template, dict):\n                        img_template = dict(img_template)\n                        for key in [\"url\", \"path\", \"detail\"]:\n                            if key in img_template:\n                                input_variables.extend(\n                                    get_template_variables(\n                                        img_template[key], template_format\n                                    )\n                                )\n                        img_template_obj = ImagePromptTemplate(\n                            input_variables=input_variables,\n                            template=img_template,\n                            template_format=template_format,\n                        )\n                    else:\n                        msg = f\"Invalid image template: {tmpl}\"  # type: ignore[unreachable]\n                        raise ValueError(msg)\n                    prompt.append(img_template_obj)\n                elif isinstance(tmpl, dict):\n                    if template_format == \"jinja2\":\n                        msg = (\n                            \"jinja2 is unsafe and is not supported for templates \"\n                            \"expressed as dicts. Please use 'f-string' or 'mustache' \"\n                            \"format.\"\n                        )\n                        raise ValueError(msg)\n                    data_template_obj = DictPromptTemplate(\n                        template=cast(\"dict[str, Any]\", tmpl),\n                        template_format=template_format,\n                    )\n                    prompt.append(data_template_obj)\n                else:\n                    msg = f\"Invalid template: {tmpl}\"  # type: ignore[unreachable]\n                    raise ValueError(msg)\n            return cls(prompt=prompt, **kwargs)","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/prompts/chat.py#L496-L532","documentation":"In the image-template branch of from_messages/from_template, an image entry whose value is neither str nor dict is rejected with ValueError('Invalid image template'). The branch's type analysis marks this code unreachable (# type: ignore[unreachable]) because the outer isinstance(tmpl, tuple) dispatch should constrain the shape, so hitting it means the dispatch contract was violated (e.g. via unusual subclasses or mutated input).","triggerScenarios":"Supplying an image message element whose payload is an int, None, bytes, or an object that str/dict checks fail on, typically through dynamically assembled template lists that bypass static typing.","commonSituations":"Programmatic prompt builders that splice user/config data into message tuples without validating shape; serialization round-trips that turn a dict into something else.","solutions":["Make every image entry either a format string or a dict with keys among 'url', 'path', 'detail'","Validate assembled template lists before passing them to from_messages: assert each payload is str or dict"],"exampleFix":"# before\n(\"human\", [{\"type\": \"image_url\", \"image_url\": image_bytes}])  # ValueError\n\n# after\n(\"human\", [{\"type\": \"image_url\", \"image_url\": {\"url\": build_url(image_bytes)}}])","handlingStrategy":"type-guard","validationCode":"def valid_image_entry(entry) -> bool:\n    t = entry.get(\"image_url\") if isinstance(entry, dict) else entry\n    return isinstance(t, (str, dict))","typeGuard":"def is_image_template(t) -> bool:\n    return isinstance(t, (str, dict))","tryCatchPattern":null,"preventionTips":["Validate programmatically assembled message tuples against the (str | dict) image contract before from_messages"],"tags":["prompts","chat","multimodal","images","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}