BasePromptTemplate._validate_input uses isinstance(inner_input, dict) instead of collections.abc.Mapping, causing standard library mappings that are not dict subclasses to be mishandled. Single-variable templates silently render the mapping's repr into the prompt, while multi-variable templates raise a misleading TypeError. All template formats and both sync/async paths are affected. Fix is to check Mapping and convert to dict.
Origin Analysis
In langchain_core.prompts.base.BasePromptTemplate._validate_input, the type check for mapping inputs uses isinstance(inner_input, dict). This excludes valid Mappings like ChainMap, UserDict, and MappingProxyType. For single-variable templates, the input falls into the bare-value branch and is converted to string via str(), producing a wrong prompt. For multi-variable templates, the validation raises TypeError because the input is not recognized as a mapping.
Run the following Python code:
from collections import ChainMap, UserDict
from types import MappingProxyType
from langchain_core.prompts import PromptTemplate
template = PromptTemplate.from_template('Hello {name}')
for cls in (dict, ChainMap, UserDict, MappingProxyType):
payload = cls({'name': 'Alice'})
try:
print(cls.__name__, '->', template.format(**payload))
except TypeError as exc:
print(cls.__name__, '-> TypeError:', exc)
for cls in (dict, ChainMap, UserDict):
payload = cls({'name': 'Alice'})
try:
print(cls.__name__, '->', template.invoke(payload).text)
except TypeError as exc:
print(cls.__name__, '-> TypeError:', exc)
Observe that ChainMap, UserDict, MappingProxyType either raise TypeError for multi-variable templates or produce silent misrendering for single-variable templates.
Edge Case Audit
Converting arbitrary Mapping objects to dict forces eager evaluation and may break lazy or read-only mappings (e.g., MappingProxyType, custom lazy maps). Large mappings may cause memory spikes. If the original mapping is mutated concurrently, the converted dict may capture an inconsistent snapshot. Users who relied on passing a custom Mapping that preserved identity or special behavior will see changed semantics. Rollback: if this causes issues, revert to checking `dict` only and document the limitation, or add an opt-in flag for strict mapping handling.