langchain-ai/langchain · error · ValueError
metadata must be a list of the same length as prompts
Error message
metadata must be a list of the same length as prompts
What it means
Raised by BaseLLM.generate during batch generation with per-prompt callbacks. If metadata is provided alongside a per-prompt callbacks list, metadata must be a list of dicts with one dict per prompt. A single dict or a mismatched-length list is rejected.
Source
Thrown at libs/core/langchain_core/language_models/llms.py:949
and (
isinstance(callbacks[0], (list, BaseCallbackManager))
or callbacks[0] is None
)
):
# We've received a list of callbacks args to apply to each input
if len(callbacks) != len(prompts):
msg = "callbacks must be the same length as prompts"
raise ValueError(msg)
if tags is not None and not (
isinstance(tags, list) and len(tags) == len(prompts)
):
msg = "tags must be a list of the same length as prompts"
raise ValueError(msg)
if metadata is not None and not (
isinstance(metadata, list) and len(metadata) == len(prompts)
):
msg = "metadata must be a list of the same length as prompts"
raise ValueError(msg)
if run_name is not None and not (
isinstance(run_name, list) and len(run_name) == len(prompts)
):
msg = "run_name must be a list of the same length as prompts"
raise ValueError(msg)
tags_list = cast("list[list[str] | None]", tags or ([None] * len(prompts)))
metadata_list = cast(
"list[builtins.dict[str, Any] | None]",
metadata or ([{}] * len(prompts)),
)
run_name_list = run_name or cast(
"list[str | None]", ([None] * len(prompts))
)
params = self._dict_for_compat()
params["stop"] = stop
callback_managers = [
CallbackManager.configure(
callback,View on GitHub (pinned to e32fa9a52e)
Solutions
- Pass metadata as a list of dicts of length len(prompts): metadata=[md] * len(prompts)
- Or omit metadata (pass None) if per-prompt metadata is not needed
- Verify len(callbacks) == len(prompts) first, since the metadata check only runs in that branch
Example fix
# before
llm.generate(prompts, callbacks=per_prompt_cbs, metadata={'run': 'x'})
# after
llm.generate(prompts, callbacks=per_prompt_cbs, metadata=[{'run': 'x'}] * len(prompts)) Defensive patterns
Strategy: validation
Validate before calling
n = len(prompts)
if metadata is not None:
assert isinstance(metadata, list) and len(metadata) == n, f'metadata must be a list of {n} dicts' Type guard
def valid_batch_metadata(md: object, n: int) -> bool:
return md is None or (isinstance(md, list) and len(md) == n and all(isinstance(d, dict) for d in md)) Try / catch
try:
llm.generate(prompts, callbacks=cbs, metadata=metadata)
except ValueError as e:
if 'metadata must be a list' in str(e) and isinstance(metadata, dict):
llm.generate(prompts, callbacks=cbs, metadata=[metadata] * len(prompts))
else:
raise Prevention
- Never pass a bare dict as metadata to a batched call with per-prompt callbacks
- Use [md] * len(prompts) to fan out shared metadata
- Log len() of every per-prompt argument before dispatch in batch pipelines
When it happens
Trigger: Calling llm.generate(prompts, callbacks=[[cb], [cb]], metadata={'user': 'a'}) — a single dict instead of a list of dicts; or metadata=[{'user': 'a'}] with 2+ prompts. Only checked when callbacks[0] is a list/BaseCallbackManager/None.
Common situations: Copying a metadata dict used with single-prompt invoke into a batched generate call; LangSmith tracing setups that attach metadata per run and forget to fan it out per prompt.
Related errors
- tags must be a list of the same length as prompts
- run_name must be a list of the same length as prompts
- Number of manually provided run_id's does not match batch le
- Argument 'prompts' is expected to be of type list[str], rece
- invalid IP address
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/87e2cc8691a2284f.
Report an issue: GitHub.