Skip to content

Commit 1feb548

Browse files
fix(memory): type EventMetadataFilter.operator as str
`EventMetadataFilter.build_expression` stores `operator.value` (the operator's string, e.g. "EQUALS_TO") in the returned dict, but the TypedDict annotated `operator` as the `OperatorType` enum. Every constructed filter therefore mis-typed the field, contradicting the runtime value that is serialized to the AgentCore service. Annotate `operator` as `str`, matching the sibling `MemoryMetadataFilter` which already documents and types this the same way. Add the first unit tests for `EventMetadataFilter` (the type previously had none), covering the field annotation and `build_expression` for both a value operator and an existence operator. Fixes #240
1 parent ba755aa commit 1feb548

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/bedrock_agentcore/memory/models/filters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class EventMetadataFilter(TypedDict):
8686
"""
8787

8888
left: LeftExpression
89-
operator: OperatorType
89+
# Stored as the operator's string value (e.g. "EQUALS_TO"), not the enum itself,
90+
# since this dict is serialized directly to the AgentCore service.
91+
operator: str
9092
right: Optional[RightExpression]
9193

9294
def build_expression(

tests/bedrock_agentcore/memory/models/test_filters.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
11
"""Unit tests for memory record metadata filter models."""
22

33
from datetime import datetime
4+
from typing import get_type_hints
45

56
import pytest
67

78
from bedrock_agentcore.memory.models import (
9+
EventMetadataFilter,
810
IndexedKey,
11+
LeftExpression,
912
MemoryMetadataFilter,
1013
MemoryRecordLeftExpression,
1114
MemoryRecordOperatorType,
1215
MemoryRecordRightExpression,
1316
MetadataValueType,
17+
OperatorType,
18+
RightExpression,
1419
)
1520

1621

22+
class TestEventMetadataFilter:
23+
"""Test cases for EventMetadataFilter."""
24+
25+
def test_operator_is_annotated_as_str(self):
26+
"""The stored `operator` is the enum's string value, so the field is typed `str`.
27+
28+
Regression for #240: `build_expression` stores `operator.value` (a string),
29+
but the field was annotated `OperatorType` (the enum), which mis-typed every
30+
constructed filter. This mirrors the already-correct `MemoryMetadataFilter`.
31+
"""
32+
assert get_type_hints(EventMetadataFilter)["operator"] is str
33+
34+
def test_build_expression_equals_string(self):
35+
"""build_expression stores the operator's string value, not the enum."""
36+
result = EventMetadataFilter.build_expression(
37+
LeftExpression.build("location"),
38+
OperatorType.EQUALS_TO,
39+
RightExpression.build("NYC"),
40+
)
41+
42+
assert result == {
43+
"left": {"metadataKey": "location"},
44+
"operator": "EQUALS_TO",
45+
"right": {"metadataValue": {"stringValue": "NYC"}},
46+
}
47+
48+
def test_build_expression_exists_no_right_operand(self):
49+
"""An EXISTS filter omits the right operand."""
50+
result = EventMetadataFilter.build_expression(
51+
LeftExpression.build("location"),
52+
OperatorType.EXISTS,
53+
)
54+
55+
assert result == {
56+
"left": {"metadataKey": "location"},
57+
"operator": "EXISTS",
58+
}
59+
assert "right" not in result
60+
61+
1762
class TestMemoryRecordLeftExpression:
1863
"""Test cases for MemoryRecordLeftExpression."""
1964

0 commit comments

Comments
 (0)