{"record":{"id":"c57344a2ee095146","repo":"deepset-ai/haystack","slug":"the-length-of-the-metadata-list-must-match-the-num","errorCode":null,"errorMessage":"The length of the metadata list must match the number of sources.","messagePattern":"The length of the metadata list must match the number of sources\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"haystack/components/converters/utils.py","lineNumber":80,"sourceCode":"\n    Given all the possible value of the meta input for a converter (None, dictionary or list of dicts),\n    makes sure to return a list of dictionaries of the correct length for the converter to use.\n\n    :param meta: the meta input of the converter, as-is\n    :param sources_count: the number of sources the converter received\n    :returns: a list of dictionaries of the make length as the sources list\n\n    Each source always gets its own independent dictionary. When ``meta`` is ``None`` or a single\n    dictionary, a separate copy is returned for every source so that mutating one source's metadata\n    downstream does not leak into the others.\n    \"\"\"\n    if meta is None:\n        return [{} for _ in range(sources_count)]\n    if isinstance(meta, dict):\n        return [deepcopy(meta) for _ in range(sources_count)]\n    if isinstance(meta, list):\n        if sources_count != len(meta):\n            raise ValueError(\"The length of the metadata list must match the number of sources.\")\n        return meta\n    raise ValueError(\"meta must be either None, a dictionary or a list of dictionaries.\")\n","sourceCodeStart":62,"sourceCodeEnd":83,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/converters/utils.py#L62-L83","documentation":"normalize_metadata aligns per-source metadata with the number of sources. When meta is a list, its length must equal sources_count; otherwise ValueError is raised. This ensures each source gets exactly one metadata dict in converter run() calls.","triggerScenarios":"Calling a converter's run(sources=[...], meta=[{...},...]) where len(meta) != len(sources) — e.g. one metadata dict for three files, or extra dicts left over from a previous batch.","commonSituations":"Reusing a cached meta list across batches of different size; building meta in a loop that skips failed sources; hardcoded meta for a fixed file count then changing the file list.","solutions":["Make the meta list exactly the same length as sources (one dict per source).","Pass a single dict instead — it is automatically deep-copied for every source.","Pass meta=None to get an empty dict per source."],"exampleFix":"# before\nconverter.run(sources=[a, b, c], meta=[{\"author\": \"x\"}])  # 1 vs 3\n# after\nconverter.run(sources=[a, b, c], meta=[{\"author\": \"x\"}, {\"author\": \"x\"}, {\"author\": \"x\"}])\n# or simply:\nconverter.run(sources=[a, b, c], meta={\"author\": \"x\"})","handlingStrategy":"validation","validationCode":"def check_meta(meta, sources):\n    if isinstance(meta, list) and len(meta) != len(sources):\n        raise ValueError(f\"meta length {len(meta)} != sources length {len(sources)}\")\n\ncheck_meta(meta, sources)\nconverter.run(sources=sources, meta=meta)","typeGuard":null,"tryCatchPattern":"try:\n    result = converter.run(sources=sources, meta=meta)\nexcept ValueError as e:\n    if \"length of the metadata list\" in str(e):\n        result = converter.run(sources=sources, meta=meta[:len(sources)] if len(meta) > len(sources) else meta)\n    else:\n        raise","preventionTips":["Prefer a single shared dict over a list when all sources need identical metadata","Build meta with zip(sources, meta_dicts) so lengths are tied together structurally","Assert len(meta) == len(sources) right before the call in batch pipelines"],"tags":["python","valueerror","metadata","length-mismatch"],"backgroundTag":"list-length-mismatch","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}