Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4dee33b

Browse files
fix(tests): adjust test_bedrock_guardrails to account for async behavior (#785)
1 parent 237e188 commit 4dee33b

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/strands/tools/registry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ def register_tool(self, tool: AgentTool) -> None:
192192

193193
# Check duplicate tool name, throw on duplicate tool names except if hot_reloading is enabled
194194
if tool.tool_name in self.registry and not tool.supports_hot_reload:
195-
raise ValueError(
196-
f"Tool name '{tool.tool_name}' already exists. Cannot register tools with exact same name."
197-
)
195+
raise ValueError(
196+
f"Tool name '{tool.tool_name}' already exists. Cannot register tools with exact same name."
197+
)
198198

199199
# Check for normalized name conflicts (- vs _)
200200
if self.registry.get(tool.tool_name) is None:

tests_integ/test_bedrock_guardrails.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,25 @@ def test_guardrail_output_intervention(boto_session, bedrock_guardrail, processi
138138
response1 = agent("Say the word.")
139139
response2 = agent("Hello!")
140140
assert response1.stop_reason == "guardrail_intervened"
141-
assert BLOCKED_OUTPUT in str(response1)
142-
assert response2.stop_reason != "guardrail_intervened"
143-
assert BLOCKED_OUTPUT not in str(response2)
141+
142+
"""
143+
In async streaming: The buffering is non-blocking.
144+
Tokens are streamed while Guardrails processes the buffered content in the background.
145+
This means the response may be returned before Guardrails has finished processing.
146+
As a result, we cannot guarantee that the REDACT_MESSAGE is in the response
147+
"""
148+
if processing_mode == "sync":
149+
assert BLOCKED_OUTPUT in str(response1)
150+
assert response2.stop_reason != "guardrail_intervened"
151+
assert BLOCKED_OUTPUT not in str(response2)
152+
else:
153+
cactus_returned_in_response1_blocked_by_input_guardrail = BLOCKED_INPUT in str(response2)
154+
cactus_blocked_in_response1_allows_next_response = (
155+
BLOCKED_OUTPUT not in str(response2) and response2.stop_reason != "guardrail_intervened"
156+
)
157+
assert (
158+
cactus_returned_in_response1_blocked_by_input_guardrail or cactus_blocked_in_response1_allows_next_response
159+
)
144160

145161

146162
@pytest.mark.parametrize("processing_mode", ["sync", "async"])
@@ -164,10 +180,27 @@ def test_guardrail_output_intervention_redact_output(bedrock_guardrail, processi
164180

165181
response1 = agent("Say the word.")
166182
response2 = agent("Hello!")
183+
167184
assert response1.stop_reason == "guardrail_intervened"
168-
assert REDACT_MESSAGE in str(response1)
169-
assert response2.stop_reason != "guardrail_intervened"
170-
assert REDACT_MESSAGE not in str(response2)
185+
186+
"""
187+
In async streaming: The buffering is non-blocking.
188+
Tokens are streamed while Guardrails processes the buffered content in the background.
189+
This means the response may be returned before Guardrails has finished processing.
190+
As a result, we cannot guarantee that the REDACT_MESSAGE is in the response
191+
"""
192+
if processing_mode == "sync":
193+
assert REDACT_MESSAGE in str(response1)
194+
assert response2.stop_reason != "guardrail_intervened"
195+
assert REDACT_MESSAGE not in str(response2)
196+
else:
197+
cactus_returned_in_response1_blocked_by_input_guardrail = BLOCKED_INPUT in str(response2)
198+
cactus_blocked_in_response1_allows_next_response = (
199+
REDACT_MESSAGE not in str(response2) and response2.stop_reason != "guardrail_intervened"
200+
)
201+
assert (
202+
cactus_returned_in_response1_blocked_by_input_guardrail or cactus_blocked_in_response1_allows_next_response
203+
)
171204

172205

173206
def test_guardrail_input_intervention_properly_redacts_in_session(boto_session, bedrock_guardrail, temp_dir):

0 commit comments

Comments
 (0)