Skip to content

fix(routing): 404 on batch document lookup doesn't identify which doc_id failed, forcing binary search on callers #277

Closed

Description

@piyushmakhija28

Summary

When one of N document IDs in a route request is not found, route_query() raises document_not_found() with a generic 404 message that does not include the failing doc_id. With up to 25 document IDs in a batch request, the caller cannot identify which one is missing without performing a binary search.

Root Cause

backend/app/api/routing.py around line 96:

docs = await asyncio.gather(*[_fetch_doc(doc_id, ...) for doc_id in document_ids])

for doc in docs:
    if doc is None:
        raise document_not_found()   # ← which doc_id? No information provided

The doc_id corresponding to the None result is lost because the index into docs is not tracked alongside the fetch result.

Failure Scenario

  1. Caller submits 25 document_ids to batch-route a query.
  2. One of them — say document_ids[17] — was deleted 3 days ago.
  3. The response is 404 {"code": "DOCUMENT_NOT_FOUND", "message": "No document with the given id exists."}.
  4. Caller cannot tell which of 25 IDs failed.
  5. Caller must make 25 individual /documents/{id} requests to binary-search for the missing one.

Proposed Fix

Track doc_id alongside the fetch result:

async def _fetch_doc_tagged(doc_id: str, ...) -> tuple[str, DocumentRecord | None]:
    return doc_id, await _fetch_doc(doc_id, ...)

tagged_results = await asyncio.gather(
    *[_fetch_doc_tagged(doc_id, ...) for doc_id in document_ids],
    return_exceptions=True,
)

for doc_id, doc in tagged_results:
    if doc is None:
        raise HTTPException(
            status_code=404,
            detail={
                "code": "DOCUMENT_NOT_FOUND",
                "document_id": doc_id,    # ← now caller knows
                "message": f"Document '{doc_id}' not found.",
            }
        )

Activity

  1. piyushmakhija28 commented on Jun 21, 2026

    @piyushmakhija28
    ContributorAuthor

    Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcode-reviewFound via automated code reviewpriority: mediumFunctional error with workaround

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions