weaviate-python-client icon indicating copy to clipboard operation
weaviate-python-client copied to clipboard

fix(util): guard _datetime_from_weaviate_str against empty string

Open devteamaegis opened this issue 6 months ago • 2 comments

What's broken

_datetime_from_weaviate_str in weaviate/util.py evaluates string[-1] on line 754 without checking whether string is empty. Python's negative indexing on an empty string raises IndexError. The protobuf wire-format default for a string field is "", so any gRPC query response containing an object with an unset date property crashes the client during deserialization in base_executor.py.

Traceback: IndexError: string index out of range at weaviate/util.py line 754.

Why it happens

The function assumes the input is a non-empty datetime string and immediately indexes from the end, which is invalid for the empty-string protobuf default.

Fix

Added a two-line guard at the top of _datetime_from_weaviate_str: if string is empty, return datetime.min. This matches the existing behaviour for year-zero dates and avoids any semantic change for valid inputs.

Test

Added ("", datetime.min) as a parametrized case in the existing test_datetime_from_weaviate_str test in test/test_util.py. All 40 tests pass.

Fixes #2052

devteamaegis avatar Jun 01 '26 19:06 devteamaegis

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?

weaviate-git-bot avatar Jun 01 '26 21:06 weaviate-git-bot

Good call @dirkkul — changed it to return None for an unset/empty datetime instead of datetime.min. That matches the "unset property should be None" expectation, and it flows through safely: both callers in base_executor.py propagate the value as a property (Any / Optional[List[Any]]), neither does date math on it. Updated the return type to Optional[datetime.datetime] and the test case to expect None. Left the year-0 → datetime.min path untouched since that's separate existing behavior with its own warning.

devteamaegis avatar Jul 26 '26 16:07 devteamaegis