Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions test/test_serialise.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from typing import Annotated

import pytest
Expand Down Expand Up @@ -87,6 +88,173 @@ class TestModel(BaseModel):
},
},
),
(
(
(
Filter.by_property("prop_str").equal("a")
| Filter.by_property("prop_str").equal("b")
)
& Filter.by_property("prop_int").greater_than("c")
),
{
"field": {
"combine": "and",
"filters": [
{
"combine": "or",
"filters": [
{
"operator": "Equal",
"target": "prop_str",
"value": "a",
},
{
"operator": "Equal",
"target": "prop_str",
"value": "b",
},
],
},
{
"operator": "GreaterThan",
"target": "prop_int",
"value": "c",
},
],
},
},
),
(
(
(
(
Filter.by_property("prop_str_1").equal("a")
| Filter.by_property("prop_str_1").equal("b")
)
& (
Filter.by_property("prop_str_2").equal("c")
| Filter.by_property("prop_str_2").equal("d")
)
)
& (
(
Filter.by_property("prop_int").greater_than(1)
| Filter.by_property("prop_int").less_than(-1)
)
& (
(
Filter.by_property("prop_str_3").like("*hello*")
& Filter.by_property("prop_str_3").like("*world*")
)
| (
Filter.by_creation_time().less_than(
datetime.fromtimestamp(1757669274, tz=timezone.utc)
)
| Filter.by_update_time().greater_than(
datetime.fromtimestamp(1757669274, tz=timezone.utc)
)
)
)
)
),
{
"field": {
"combine": "and",
"filters": [
{
"combine": "and",
"filters": [
{
"combine": "or",
"filters": [
{
"operator": "Equal",
"target": "prop_str_1",
"value": "a",
},
{
"operator": "Equal",
"target": "prop_str_1",
"value": "b",
},
],
},
{
"combine": "or",
"filters": [
{
"operator": "Equal",
"target": "prop_str_2",
"value": "c",
},
{
"operator": "Equal",
"target": "prop_str_2",
"value": "d",
},
],
},
],
},
{
"combine": "and",
"filters": [
{
"combine": "or",
"filters": [
{
"operator": "GreaterThan",
"target": "prop_int",
"value": 1,
},
{
"operator": "LessThan",
"target": "prop_int",
"value": -1,
},
],
},
{
"combine": "or",
"filters": [
{
"combine": "and",
"filters": [
{
"operator": "Like",
"target": "prop_str_3",
"value": "*hello*",
},
{
"operator": "Like",
"target": "prop_str_3",
"value": "*world*",
},
],
},
{
"combine": "or",
"filters": [
{
"operator": "LessThan",
"target": "_creationTimeUnix",
"value": "2025-09-12T09:27:54Z",
},
{
"operator": "GreaterThan",
"target": "_lastUpdateTimeUnix",
"value": "2025-09-12T09:27:54Z",
},
],
},
],
},
],
},
],
},
},
),
],
)
def test_serialise_filter(field, expect):
Expand Down
29 changes: 17 additions & 12 deletions weaviate_agents/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
_FilterAnd,
_FilterOr,
_Filters,
_FilterValue,
)
from weaviate.collections.classes.grpc import (
HybridVectorType,
Expand Down Expand Up @@ -38,23 +37,29 @@ class _FilterAndOrSerialise(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

combine: Literal["and", "or"]
filters: list[_Filters]
filters: list[Union[_Filters, _FilterAndOrSerialise]]


@PlainSerializer
def serialise_filter(
filter_and_or_value: Union[_FilterAnd, _FilterOr, _FilterValue],
) -> Union[_FilterValue, _FilterAndOrSerialise]:
if isinstance(filter_and_or_value, _FilterValue):
filter_and_or_value: _Filters,
) -> Union[_Filters, _FilterAndOrSerialise]:
return _serialise_filter_level(filter_and_or_value)


def _serialise_filter_level(
filter_and_or_value: _Filters,
) -> Union[_Filters, _FilterAndOrSerialise]:
if not isinstance(filter_and_or_value, (_FilterAnd, _FilterOr)):
return filter_and_or_value

if isinstance(filter_and_or_value, _FilterAnd):
combine: Literal["and", "or"] = "and"
elif isinstance(filter_and_or_value, _FilterOr):
combine = "or"
else:
raise TypeError(f"Unknown filter type {type(filter_and_or_value)}")
return _FilterAndOrSerialise(combine=combine, filters=filter_and_or_value.filters)
combine: Literal["and", "or"] = (
"and" if isinstance(filter_and_or_value, _FilterAnd) else "or"
)
return _FilterAndOrSerialise(
combine=combine,
filters=[_serialise_filter_level(f) for f in filter_and_or_value.filters],
)


# A set of models to help serialise HybridVectorType, which is a union of
Expand Down