Skip to content

Return empty results for empty ID iterators in fetch_objects_by_ids - #2297

Open
Shy7777 wants to merge 1 commit into
weaviate:mainfrom
Shy7777:fix/handle-empty-id-iterables
Open

Shy7777 wants to merge 1 commit into
weaviate:mainfrom
Shy7777:fix/handle-empty-id-iterables

Conversation

@Shy7777

@Shy7777 Shy7777 commented Dec 1, 2026

Copy link
Copy Markdown

When a caller filters a stream of IDs down to zero items, fetch_objects_by_ids raises WeaviateInvalidInputError: Filter.any_of must have at least one filter. Passing an empty list to the same method returns an empty result. Both inputs satisfy the advertised Iterable argument.

The query and generate paths now build the ID filters once and return the existing empty response when there are none. A non-empty iterator is still consumed only once.

The regression tests call both APIs on synchronous and asynchronous clients without connecting to a server. All four failed on main and pass with this change. I also checked that a two-ID generator still sends both IDs, in order, in the gRPC request.

Local checks passed: 717 unit tests (one skipped), all 118 mock-server tests, Ruff, and Flake8.

Copilot AI balanced review requested due to automatic review settings December 1, 2026 05:13

@orca-security-eu orca-security-eu Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the change aligning empty-iterator behavior across query and generate APIs.

No open issues 🎉

What changed in this PR

Makes fetch_objects_by_ids return empty results for empty iterators, matching empty-list behavior in both query and generate APIs.

Changes:

  • Builds ID filters once, preserving single-pass consumption of non-empty iterators.
  • Reuses the existing empty-response paths when no filters are produced.
  • Adds offline regression tests for query and generate calls on synchronous and asynchronous clients.
File Description
weaviate/​collections/​queries/​fetch_objects_by_ids/​query/​executor.py Detects empty inputs from the constructed filters before querying.
weaviate/​collections/​queries/​fetch_objects_by_ids/​generate/​executor.py Applies the same empty-iterator handling to generation.
test/​collection/​test_queries.py Adds four empty-iterator regression cases without connecting to a server.

Review effort: Balanced

@weaviate-git-bot

Copy link
Copy Markdown

To avoid any confusion in the future about your contribution to Weaviate, we work with a Contributor License Agreement. If you agree, you can simply add a comment to this PR that you agree with the CLA so that we can merge.

beep boop - the Weaviate bot 👋🤖

PS:
Are you already a member of the Weaviate Forum?

@Mansi2221 Mansi2221 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right fix for the empty-iterator regression. The old if not ids check was ambiguous — it short-circuited for both an empty collection and for ids=None, but when ids was an iterator, if not ids on a generator object is always truthy regardless of whether it would yield anything, so the empty-iterator branch fell through to the error path that constructed Filter.any_of([]).

Materializing the iterator into id_filters once and then branching on not id_filters gives both:

  1. A single-pass consumption of the iterator (correct for generators)
  2. An unambiguous empty-list check

The symmetrical fix in both query/executor.py and generate/executor.py plus the sync+async parametrized tests covers the full API surface. One minor observation: the test uses a real WeaviateClient with a localhost:8080 connection — since the test expects to return an empty result without any gRPC round-trip, the short-circuit should fire before any network I/O. That's visible in the diff. Good.

LGTM.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants