@@ -138,9 +138,25 @@ def test_guardrail_output_intervention(boto_session, bedrock_guardrail, processi
138
138
response1 = agent ("Say the word." )
139
139
response2 = agent ("Hello!" )
140
140
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
+ )
144
160
145
161
146
162
@pytest .mark .parametrize ("processing_mode" , ["sync" , "async" ])
@@ -164,10 +180,27 @@ def test_guardrail_output_intervention_redact_output(bedrock_guardrail, processi
164
180
165
181
response1 = agent ("Say the word." )
166
182
response2 = agent ("Hello!" )
183
+
167
184
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
+ )
171
204
172
205
173
206
def test_guardrail_input_intervention_properly_redacts_in_session (boto_session , bedrock_guardrail , temp_dir ):
0 commit comments