Skip to content

Commit 6d12fee

Browse files
authored
fix: incorrect handling of empty Tags object (#23)
* fix: incorrect handling of empty Tags object * fix: remove redundant bool cast
1 parent 0f90a81 commit 6d12fee

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/f5_ai_gateway_sdk/result.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def validate_allowed(self, processor_name: str, annotate: bool, modify: bool):
8181
self.modified_prompt = None
8282
self.modified_response = None
8383

84-
if self.tags is not None and not annotate:
84+
if self.tags and not annotate:
8585
logging.warning(
8686
"%s tried to annotate request with tags when parameters.annotate was set to false, tags will be dropped",
8787
processor_name,

tests/test_result.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from f5_ai_gateway_sdk.request_input import RequestInput
1111
from f5_ai_gateway_sdk.response_output import ResponseOutput
1212
from f5_ai_gateway_sdk.result import Result
13+
from f5_ai_gateway_sdk.tags import Tags
1314

1415

1516
def test_prompt_not_allowed_with_response():
@@ -22,3 +23,62 @@ def test_prompt_not_allowed_with_response():
2223
modified_prompt=RequestInput(messages=[]),
2324
modified_response=ResponseOutput(choices=[]),
2425
)
26+
27+
28+
@pytest.mark.parametrize(
29+
"name,annotate,modify,result,expected_log",
30+
[
31+
(
32+
"Annotate not allowed",
33+
False,
34+
False,
35+
Result(tags=Tags({"test": ["value"]})),
36+
"test_processor tried to annotate request with tags when parameters.annotate was set to false, tags will be dropped",
37+
),
38+
("Treat empty Tags as no annotate", False, False, Result(tags=Tags()), ""),
39+
(
40+
"Modify not allowed for prompt",
41+
True,
42+
False,
43+
Result(modified_prompt=RequestInput(messages=[])),
44+
"test_processor tried to modify request when parameters.modify was set to false, modification will be dropped",
45+
),
46+
(
47+
"Modify not allowed for response",
48+
True,
49+
False,
50+
Result(modified_response=ResponseOutput(choices=[])),
51+
"test_processor tried to modify request when parameters.modify was set to false, modification will be dropped",
52+
),
53+
(
54+
"Modify allowed",
55+
False,
56+
True,
57+
Result(modified_response=ResponseOutput(choices=[])),
58+
"",
59+
),
60+
(
61+
"Annotate allowed",
62+
True,
63+
True,
64+
Result(tags=Tags(), modified_prompt=RequestInput(messages=[])),
65+
"",
66+
),
67+
],
68+
ids=lambda name: name,
69+
)
70+
def test_validate_not_allowed_parameters(
71+
caplog, name, annotate: bool, modify: bool, result: Result, expected_log
72+
):
73+
"""Test validate_allowed drops modifications or tags which have not been approved."""
74+
result.validate_allowed("test_processor", annotate, modify)
75+
76+
if expected_log:
77+
assert expected_log in caplog.text
78+
else:
79+
assert len(caplog.records) == 0
80+
if not annotate:
81+
assert not bool(result.tags)
82+
if not modify:
83+
assert result.modified_prompt is None
84+
assert result.modified_response is None

0 commit comments

Comments
 (0)