Skip to content

Commit f8e9b12

Browse files
Fixing dockey/doc_id mismatch when no metadata is found (#1288)
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 8685d9f commit f8e9b12

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/paperqa/clients/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,13 @@ async def upgrade_doc_to_doc_details(self, doc: Doc, **kwargs) -> DocDetails:
251251
return provided_doc_details + doc_details
252252

253253
# if we can't get metadata, just return the doc, but don't overwrite any fields
254-
orig_fields = doc.model_dump() | {"fields_to_overwrite_from_metadata": set()}
254+
overwrite_fields: set[str] = set()
255+
if doc.dockey == doc.content_hash:
256+
# This allows DocDetails validator on fields_to_overwrite_from_metadata
257+
# to sync dockey with doc_id. Otherwise, dockey remains the raw
258+
# content_hash and won't match the computed doc_id.
259+
overwrite_fields.add("doc_id")
260+
orig_fields = doc.model_dump() | {
261+
"fields_to_overwrite_from_metadata": overwrite_fields
262+
}
255263
return DocDetails(**(orig_fields | provided_fields))

tests/test_agents.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ async def test_get_directory_index(
103103
results = await index.query(query="who is Frederick Bates?", min_score=5)
104104
assert results
105105
target_doc_path = (paper_dir / "bates.txt").absolute()
106-
assert results[0].docs.keys() == {md5sum(target_doc_path)}, (
106+
assert results[0].docs.keys() == {
107+
compute_unique_doc_id(None, md5sum(target_doc_path))
108+
}, (
107109
f"Expected to find {target_doc_path.name!r}, got citations"
108110
f" {[d.formatted_citation for d in results[0].docs.values()]}."
109111
)

0 commit comments

Comments
 (0)