AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

PromptTemplate._Validate_input Rejects Non-Dict Mapping Inputs (ChainMap, UserDict, MappingProxyType) And Silently Misrenders Single-Variable Templates

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.

highConfidence 95%Langchain_coreVector StoresMemoryAgentsAffected V1.5.6

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.

Fixing Code Block

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.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state