55import boto3
66import pytest
77
8- from strands import Agent , tool
8+ from strands import Agent
99from strands .models .bedrock import BedrockModel
1010from strands .session .file_session_manager import FileSessionManager
1111
@@ -187,7 +187,7 @@ def test_guardrail_output_intervention_redact_output(bedrock_guardrail, processi
187187 In async streaming: The buffering is non-blocking.
188188 Tokens are streamed while Guardrails processes the buffered content in the background.
189189 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.
190+ As a result, we cannot guarantee that the REDACT_MESSAGE is in the response
191191 """
192192 if processing_mode == "sync" :
193193 assert REDACT_MESSAGE in str (response1 )
@@ -203,79 +203,6 @@ def test_guardrail_output_intervention_redact_output(bedrock_guardrail, processi
203203 )
204204
205205
206- @pytest .mark .parametrize ("processing_mode" , ["sync" , "async" ])
207- def test_guardrail_intervention_properly_redacts_tool_result (bedrock_guardrail , processing_mode ):
208- INPUT_REDACT_MESSAGE = "Input redacted."
209- OUTPUT_REDACT_MESSAGE = "Output redacted."
210- bedrock_model = BedrockModel (
211- guardrail_id = bedrock_guardrail ,
212- guardrail_version = "DRAFT" ,
213- guardrail_stream_processing_mode = processing_mode ,
214- guardrail_redact_output = True ,
215- guardrail_redact_input_message = INPUT_REDACT_MESSAGE ,
216- guardrail_redact_output_message = OUTPUT_REDACT_MESSAGE ,
217- region_name = "us-east-1" ,
218- )
219-
220- @tool
221- def list_users () -> str :
222- "List my users"
223- return """[{"name": "Jerry Merry"}, {"name": "Mr. CACTUS"}]"""
224-
225- agent = Agent (
226- model = bedrock_model ,
227- system_prompt = "You are a helpful assistant." ,
228- callback_handler = None ,
229- load_tools_from_directory = False ,
230- tools = [list_users ],
231- )
232-
233- response1 = agent ("List my users." )
234- response2 = agent ("Thank you!" )
235-
236- """ Message sequence:
237- 0 (user): request1
238- 1 (assistant): reasoning + tool call
239- 2 (user): tool result
240- 3 (assistant): response1 -> output guardrail intervenes
241- 4 (user): request2
242- 5 (assistant): response2
243-
244- Guardrail intervened on output in message 3 will cause
245- the redaction of the preceding input (message 2) and message 3.
246- """
247-
248- assert response1 .stop_reason == "guardrail_intervened"
249-
250- if processing_mode == "sync" :
251- """ In sync mode the guardrail processing is blocking.
252- The response is already blocked and redacted. """
253-
254- assert OUTPUT_REDACT_MESSAGE in str (response1 )
255- assert OUTPUT_REDACT_MESSAGE not in str (response2 )
256-
257- """
258- In async streaming, the buffering is non-blocking,
259- so the response may be returned before Guardrails has finished processing.
260-
261- However, in both sync and async, with guardrail_redact_output=True:
262-
263- 1. the content should be properly redacted in memory, so that
264- response2 is not blocked by guardrails;
265- """
266- assert response2 .stop_reason != "guardrail_intervened"
267-
268- """
269- 2. the tool result block should be redacted properly, so that the
270- conversation is not corrupted.
271- """
272-
273- tool_call = [b for b in agent .messages [1 ]["content" ] if "toolUse" in b ][0 ]["toolUse" ]
274- tool_result = [b for b in agent .messages [2 ]["content" ] if "toolResult" in b ][0 ]["toolResult" ]
275- assert tool_result ["toolUseId" ] == tool_call ["toolUseId" ]
276- assert tool_result ["content" ][0 ]["text" ] == INPUT_REDACT_MESSAGE
277-
278-
279206def test_guardrail_input_intervention_properly_redacts_in_session (boto_session , bedrock_guardrail , temp_dir ):
280207 bedrock_model = BedrockModel (
281208 guardrail_id = bedrock_guardrail ,
0 commit comments