langchain-ai/langchain · error · ValueError

tags must be a list of the same length as prompts

Error message

tags must be a list of the same length as prompts

What it means

Raised by BaseLLM.generate when processing a batch of prompts with per-prompt callbacks. When callbacks is passed as a list of per-prompt callback configs, any tags argument must be a list of exactly the same length as the prompts list. This validation runs only in the per-prompt-callbacks branch (callbacks[0] is itself a list, a BaseCallbackManager, or None).

Source

Thrown at libs/core/langchain_core/language_models/llms.py:944

                **self._get_ls_params_with_defaults(stop=stop, **kwargs),
            }
        if (
            isinstance(callbacks, list)
            and callbacks
            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))
            )

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Make tags a list with exactly len(prompts) entries, e.g. tags=['my-tag'] * len(prompts), when using per-prompt callbacks
  2. Or pass tags=None to omit per-prompt tags entirely
  3. Or switch callbacks to a single shared callback manager/list if you want one tags list applied to all prompts

Example fix

# before
llm.generate(prompts, callbacks=[[cb1], [cb2]], tags=['trace'])
# after
llm.generate(prompts, callbacks=[[cb1], [cb2]], tags=[['trace'], ['trace']])
Defensive patterns

Strategy: validation

Validate before calling

n = len(prompts)
assert isinstance(callbacks, list) and len(callbacks) == n, 'per-prompt callbacks must match prompts'
assert tags is None or (isinstance(tags, list) and len(tags) == n), 'tags must match prompts'

Type guard

def valid_batch_tags(tags: object, n: int) -> bool:
    return tags is None or (isinstance(tags, list) and len(tags) == n and all(isinstance(t, list) for t in tags))

Try / catch

try:
    llm.generate(prompts, callbacks=cbs, tags=tags)
except ValueError as e:
    if 'same length as prompts' in str(e):
        tags = [None] * len(prompts)
        llm.generate(prompts, callbacks=cbs, tags=tags)
    else:
        raise

Prevention

When it happens

Trigger: Calling llm.generate(prompts, callbacks=[[cb1], [cb2]], tags=['a']) with 2 prompts but 1 tag; or passing tags as a flat list of tag strings while callbacks is a list of lists. Only triggers when callbacks is a list whose first element is a list/BaseCallbackManager/None.

Common situations: Migrating from single-prompt invoke to batched generate and reusing a single tags value; passing tags=['my-tag'] (flat, meaning one tag for all) while using per-prompt callbacks; mixing per-prompt and shared callback conventions in tracing pipelines.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/d6250ed74b4c13e3. Report an issue: GitHub.