{"record":{"id":"b8713c0f5458eeba","repo":"deepset-ai/haystack","slug":"expected-1-parent-document-with-id-parent-id-fo","errorCode":null,"errorMessage":"Expected 1 parent document with id {parent_id}, found {len(parent_docs)}","messagePattern":"Expected 1 parent document with id (.+?), found (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/retrievers/auto_merging_retriever.py","lineNumber":131,"sourceCode":"    @component.output_types(documents=list[Document])\n    def run(self, documents: list[Document]) -> dict[str, list[Document]]:\n        \"\"\"\n        Run the AutoMergingRetriever.\n\n        Recursively groups documents by their parents and merges them if they meet the threshold,\n        continuing up the hierarchy until no more merges are possible.\n\n        :param documents: List of leaf documents that were matched by a retriever\n        :returns:\n            List of documents (could be a mix of different hierarchy levels)\n        \"\"\"\n\n        AutoMergingRetriever._check_valid_documents(documents)\n\n        def _get_parent_doc(parent_id: str) -> Document:\n            parent_docs = self.document_store.filter_documents({\"field\": \"id\", \"operator\": \"==\", \"value\": parent_id})\n            if len(parent_docs) != 1:\n                raise ValueError(f\"Expected 1 parent document with id {parent_id}, found {len(parent_docs)}\")\n\n            parent_doc = parent_docs[0]\n            if not parent_doc.meta.get(\"__children_ids\"):\n                raise ValueError(f\"Parent document with id {parent_id} does not have any children.\")\n\n            return parent_doc\n\n        def _try_merge_level(docs_to_merge: list[Document], docs_to_return: list[Document]) -> list[Document]:\n            parent_doc_id_to_child_docs: dict[str, list[Document]] = defaultdict(list)  # to group documents by parent\n\n            for doc in docs_to_merge:\n                if doc.meta.get(\"__parent_id\"):  # only docs that have parents\n                    parent_doc_id_to_child_docs[doc.meta[\"__parent_id\"]].append(doc)\n                else:\n                    docs_to_return.append(doc)  # keep docs that have no parents\n\n            # Process each parent group\n            merged_docs = []","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/retrievers/auto_merging_retriever.py#L113-L149","documentation":"When merging leaf documents, the retriever looks up each parent document by exact id via document_store.filter_documents. It requires exactly one match; zero matches (missing parent) or multiple matches (store inconsistency) raise this ValueError.","triggerScenarios":"run()/sync merging where a child's '__parent_id' has no corresponding document in the store, or the store contains duplicate ids for the parent.","commonSituations":"Deleting parent documents while keeping leaves, partial re-indexing, or a custom document store whose filter returns unexpected results.","solutions":["Ensure all parent documents referenced by '__parent_id' exist in the document_store","Re-run the hierarchical indexing pipeline to restore missing parents","Check for duplicate document ids in the store"],"exampleFix":"// before\nstore.delete_documents([parent_doc])  # children now orphaned\nretriever.run(documents=leaves)\n// after\n# keep parents in the store when children remain\nretriever.run(documents=leaves)","handlingStrategy":"validation","validationCode":"for doc in leaves:\n    pid = doc.meta.get(\"__parent_id\")\n    found = store.filter_documents({\"field\": \"id\", \"operator\": \"==\", \"value\": pid})\n    if len(found) != 1:\n        raise ValueError(f\"Parent {pid} missing or duplicated in store\")","typeGuard":null,"tryCatchPattern":"try:\n    result = retriever.run(documents=leaves)\nexcept ValueError as e:\n    logger.warning(\"Parent lookup failed: %s — returning leaf documents\", e)\n    result = {\"documents\": leaves}","preventionTips":["Never delete parents while children remain","Keep parent+child writes atomic in one batch","Monitor for orphaned children after re-indexing"],"tags":["retriever","document-store","parent-lookup","data-integrity"],"backgroundTag":"parent-document-not-found","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}