|
| 1 | +"""Tests for thinking mode behavior in BedrockModel.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from strands.models.bedrock import BedrockModel |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def model_with_thinking(): |
| 10 | + """Create a BedrockModel with thinking enabled.""" |
| 11 | + return BedrockModel( |
| 12 | + model_id="anthropic.claude-sonnet-4-20250514-v1:0", |
| 13 | + additional_request_fields={"thinking": {"type": "enabled", "budget_tokens": 5000}}, |
| 14 | + ) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def model_without_thinking(): |
| 19 | + """Create a BedrockModel without thinking.""" |
| 20 | + return BedrockModel(model_id="anthropic.claude-sonnet-4-20250514-v1:0") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def model_with_thinking_and_other_fields(): |
| 25 | + """Create a BedrockModel with thinking and other additional fields.""" |
| 26 | + return BedrockModel( |
| 27 | + model_id="anthropic.claude-sonnet-4-20250514-v1:0", |
| 28 | + additional_request_fields={ |
| 29 | + "thinking": {"type": "enabled", "budget_tokens": 5000}, |
| 30 | + "some_other_field": "value", |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def test_thinking_removed_when_forcing_tool_any(model_with_thinking): |
| 36 | + """Thinking should be removed when tool_choice forces tool use with 'any'.""" |
| 37 | + tool_choice = {"any": {}} |
| 38 | + result = model_with_thinking._get_additional_request_fields(tool_choice) |
| 39 | + assert result == {} # thinking removed, no other fields |
| 40 | + |
| 41 | + |
| 42 | +def test_thinking_removed_when_forcing_specific_tool(model_with_thinking): |
| 43 | + """Thinking should be removed when tool_choice forces a specific tool.""" |
| 44 | + tool_choice = {"tool": {"name": "structured_output_tool"}} |
| 45 | + result = model_with_thinking._get_additional_request_fields(tool_choice) |
| 46 | + assert result == {} # thinking removed, no other fields |
| 47 | + |
| 48 | + |
| 49 | +def test_thinking_preserved_with_auto_tool_choice(model_with_thinking): |
| 50 | + """Thinking should be preserved when tool_choice is 'auto'.""" |
| 51 | + tool_choice = {"auto": {}} |
| 52 | + result = model_with_thinking._get_additional_request_fields(tool_choice) |
| 53 | + assert result == {"additionalModelRequestFields": {"thinking": {"type": "enabled", "budget_tokens": 5000}}} |
| 54 | + |
| 55 | + |
| 56 | +def test_thinking_preserved_with_none_tool_choice(model_with_thinking): |
| 57 | + """Thinking should be preserved when tool_choice is None.""" |
| 58 | + result = model_with_thinking._get_additional_request_fields(None) |
| 59 | + assert result == {"additionalModelRequestFields": {"thinking": {"type": "enabled", "budget_tokens": 5000}}} |
| 60 | + |
| 61 | + |
| 62 | +def test_other_fields_preserved_when_thinking_removed(model_with_thinking_and_other_fields): |
| 63 | + """Other additional fields should be preserved when thinking is removed.""" |
| 64 | + tool_choice = {"any": {}} |
| 65 | + result = model_with_thinking_and_other_fields._get_additional_request_fields(tool_choice) |
| 66 | + assert result == {"additionalModelRequestFields": {"some_other_field": "value"}} |
| 67 | + |
| 68 | + |
| 69 | +def test_no_fields_when_model_has_no_additional_fields(model_without_thinking): |
| 70 | + """Should return empty dict when model has no additional_request_fields.""" |
| 71 | + tool_choice = {"any": {}} |
| 72 | + result = model_without_thinking._get_additional_request_fields(tool_choice) |
| 73 | + assert result == {} |
| 74 | + |
| 75 | + |
| 76 | +def test_fields_preserved_when_no_thinking_and_forcing_tool(): |
| 77 | + """Additional fields without thinking should be preserved when forcing tool.""" |
| 78 | + model = BedrockModel( |
| 79 | + model_id="anthropic.claude-sonnet-4-20250514-v1:0", |
| 80 | + additional_request_fields={"some_field": "value"}, |
| 81 | + ) |
| 82 | + tool_choice = {"any": {}} |
| 83 | + result = model._get_additional_request_fields(tool_choice) |
| 84 | + assert result == {"additionalModelRequestFields": {"some_field": "value"}} |
0 commit comments