8000 chore(tests): Add unit tests for user agent changes by clareliguori · Pull Request #125 · strands-agents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

chore(tests): Add unit tests for user agent changes #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 97 additions & 8 deletions tests/strands/models/test_bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import boto3
import pytest
from botocore.config import Config as BotocoreConfig
from botocore.exceptions import ClientError, EventStreamError

import strands
Expand Down Expand Up @@ -116,6 +117,54 @@ def test__init__with_region_and_session_raises_value_error():
_ = BedrockModel(region_name="us-east-1", boto_session=boto3.Session(region_name="us-east-1"))


def test__init__default_user_agent(bedrock_client):
"""Set user agent when no boto_client_config is provided."""
with unittest.mock.patch("strands.models.bedrock.boto3.Session") as mock_session_cls:
mock_session = mock_session_cls.return_value
_ = BedrockModel()

# Verify the client was created with the correct config
mock_session.client.assert_called_once()
args, kwargs = mock_session.client.call_args
assert kwargs["service_name"] == "bedrock-runtime"
assert isinstance(kwargs["config"], BotocoreConfig)
assert kwargs["config"].user_agent_extra == "strands-agents"


def test__init__with_custom_boto_client_config_no_user_agent(bedrock_client):
"""Set user agent when boto_client_config is provided without user_agent_extra."""
custom_config = BotocoreConfig(read_timeout=900)

with unittest.mock.patch("strands.models.bedrock.boto3.Session") as mock_session_cls:
mock_session = mock_session_cls.return_value
_ = BedrockModel(boto_client_config=custom_config)

# Verify the client was created with the correct config
mock_session.client.assert_called_once()
args, kwargs = mock_session.client.call_args
assert kwargs["service_name"] == "bedrock-runtime"
assert isinstance(kwargs["config"], BotocoreConfig)
assert kwargs["config"].user_agent_extra == "strands-agents"
assert kwargs["config"].read_timeout == 900


def test__init__with_custom_boto_client_config_with_user_agent(bedrock_client):
"""Append to existing user agent when boto_client_config is provided with user_agent_extra."""
custom_config = BotocoreConfig(user_agent_extra="existing-agent", read_timeout=900)

with unittest.mock.patch("strands.models.bedrock.boto3.Session") as mock_session_cls:
mock_session = mock_session_cls.return_value
_ = BedrockModel(boto_client_config=custom_config)

# Verify the client was created with the correct config
mock_session.client.assert_called_once()
args, kwargs = mock_session.client.call_args
assert kwargs["service_name"] == "bedrock-runtime"
assert isinstance(kwargs["config"], BotocoreConfig)
assert kwargs["config"].user_agent_extra == "existing-agent strands-agents"
assert kwargs["config"].read_timeout == 900


def test__init__model_config(bedrock_client):
_ = bedrock_client

Expand Down Expand Up @@ -381,7 +430,15 @@ def test_converse_input_guardrails(bedrock_client, model, messages, tool_spec, m
"guardrail": {
"inputAssessment": {
"3e59qlue4hag": {
"wordPolicy": {"customWords": [{"match": "CACTUS", "action": "BLOCKED", "detected": True}]}
"wordPolicy": {
"customWords": [
{
"match": "CACTUS",
"action": "BLOCKED",
"detected": True,
}
]
}
}
}
}
Expand All @@ -406,7 +463,10 @@ def test_converse_input_guardrails(bedrock_client, model, messages, tool_spec, m
chunks = model.converse(messages, [tool_spec])

tru_chunks = list(chunks)
exp_chunks = [{"redactContent": {"redactUserContentMessage": "[User input redacted.]"}}, metadata_event]
exp_chunks = [
{"redactContent": {"redactUserContentMessage": "[User input redacted.]"}},
metadata_event,
]

assert tru_chunks == exp_chunks
bedrock_client.converse_stream.assert_called_once_with(**request)
Expand All @@ -424,7 +484,13 @@ def test_converse_output_guardrails(bedrock_client, model, messages, tool_spec,
"3e59qlue4hag": [
{
"wordPolicy": {
"customWords": [{"match": "CACTUS", "action": "BLOCKED", "detected": True}]
"customWords": [
{
"match": "CACTUS",
"action": "BLOCKED",
"detected": True,
}
]
},
}
]
Expand All @@ -451,7 +517,10 @@ def test_converse_output_guardrails(bedrock_client, model, messages, tool_spec,
chunks = model.converse(messages, [tool_spec])

tru_chunks = list(chunks)
exp_chunks = [{"redactContent": {"redactAssistantContentMessage": "[Assistant output redacted.]"}}, metadata_event]
exp_chunks = [
{"redactContent": {"redactAssistantContentMessage": "[Assistant output redacted.]"}},
metadata_event,
]

assert tru_chunks == exp_chunks
bedrock_client.converse_stream.assert_called_once_with(**request)
Expand All @@ -471,7 +540,13 @@ def test_converse_output_guardrails_redacts_input_and_output(
"3e59qlue4hag": [
{
"wordPolicy": {
"customWords": [{"match": "CACTUS", "action": "BLOCKED", "detected": True}]
"customWords": [
{
"match": "CACTUS",
"action": "BLOCKED",
"detected": True,
}
]
},
}
]
Expand Down Expand Up @@ -521,7 +596,13 @@ def test_converse_output_no_blocked_guardrails_doesnt_redact(
"3e59qlue4hag": [
{
"wordPolicy": {
"customWords": [{"match": "CACTUS", "action": "NONE", "detected": True}]
"customWords": [
{
"match": "CACTUS",
"action": "NONE",
"detected": True,
}
]
},
}
]
Expand Down Expand Up @@ -567,7 +648,13 @@ def test_converse_output_no_guardrail_redact(
"3e59qlue4hag": [
{
"wordPolicy": {
"customWords": [{"match": "CACTUS", "action": "BLOCKED", "detected": True}]
"customWords": [
{
"match": "CACTUS",
"action": "BLOCKED",
"detected": True,
}
]
},
}
]
Expand All @@ -591,7 +678,9 @@ def test_converse_output_no_guardrail_redact(
}

model.update_config(
additional_request_fields=additional_request_fields, guardrail_redact_output=False, guardrail_redact_input=False
additional_request_fields=additional_request_fields,
guardrail_redact_output=False,
guardrail_redact_input=False,
)
chunks = model.converse(messages, [tool_spec])

Expand Down
Loading
0