langchain-ai/langchain · error · ValueError
config must be a list of the same length as inputs, but got
Error message
config must be a list of the same length as inputs, but got {len(config)} configs for {length} inputs What it means
When a `config` for a batch operation is given as a sequence, `get_config_list` requires `len(config) == length`, where `length` is the number of inputs being processed. A mismatch means some inputs would have no config (or configs would be silently dropped), so `ValueError` is raised reporting both counts.
Source
Thrown at libs/core/langchain_core/runnables/config.py:337
config: The config or list of configs.
length: The length of the list.
Returns:
The list of configs.
Raises:
ValueError: If the length of the list is not equal to the length of the inputs.
"""
if length < 0:
msg = f"length must be >= 0, but got {length}"
raise ValueError(msg)
if isinstance(config, Sequence) and len(config) != length:
msg = (
f"config must be a list of the same length as inputs, "
f"but got {len(config)} configs for {length} inputs"
)
raise ValueError(msg)
if isinstance(config, Sequence):
return list(map(ensure_config, config))
if length > 1 and isinstance(config, dict) and config.get("run_id") is not None:
warnings.warn(
"Provided run_id be used only for the first element of the batch.",
category=RuntimeWarning,
stacklevel=3,
)
subsequent = cast(
"RunnableConfig", {k: v for k, v in config.items() if k != "run_id"}
)
return [
ensure_config(subsequent) if i else ensure_config(config)
for i in range(length)
]
return [ensure_config(config) for i in range(length)]
View on GitHub (pinned to e32fa9a52e)
Solutions
- Make the config list match the inputs one-to-one: build it with a list comprehension over the inputs.
- If all inputs share one config, pass a single dict instead of a list: `.batch(inputs, config=shared_cfg)`.
- When sharding inputs, shard the configs identically: `.batch(inputs[i:j], config=configs[i:j])`.
Example fix
# before results = chain.batch(inputs, config=[cfg1, cfg2]) # len(inputs) == 3 # after results = chain.batch(inputs, config=[make_cfg(i) for i in range(len(inputs))])
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(config, list):
assert len(config) == len(inputs), f"{len(config)} configs vs {len(inputs)} inputs"
chain.batch(inputs, config=config) Try / catch
try:
chain.batch(inputs, config=configs)
except ValueError as e:
if "same length as inputs" in str(e):
configs = [configs[i % len(configs)] for i in range(len(inputs))]
chain.batch(inputs, config=configs)
else:
raise Prevention
- Build config lists with a comprehension over inputs so lengths always match.
- Pass a single shared dict when all items use the same config.
- Shard configs together with inputs when parallelizing.
When it happens
Trigger: Calling `.batch(inputs, config=[cfg1, cfg2])` with 3 inputs, or `.batch([x], config=[cfg1, cfg2])`; also custom code that zips inputs with a differently-sized config list, or slices inputs (`inputs[1:]`) while passing the full config list.
Common situations: Reusing a config list built for a previous batch size; parallel workers processing shards of inputs with the unsharded config list; passing `config=` per-item dicts when a single shared `RunnableConfig` dict was intended (and vice versa when a list was intended).
Related errors
- length must be >= 0, but got {length}
- Unknown alternative: {which}
- If 'exception_key' is specified then inputs must be dictiona
- RunnableBranch requires at least two branches
- RunnableBranch branches must be tuples or lists of length 2,
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/28cc72b57df10256.
Report an issue: GitHub.