|
| 1 | +"""Tests for outputSchema filtering in model providers.""" |
| 2 | + |
| 3 | +from strands.models.model import Model |
| 4 | +from strands.types.tools import ToolSpec |
| 5 | + |
| 6 | + |
| 7 | +class MockModel(Model): |
| 8 | + """Mock model implementation for testing.""" |
| 9 | + |
| 10 | + def __init__(self, supports_tool_output_schema: bool = False): |
| 11 | + self.config = {"supports_tool_output_schema": supports_tool_output_schema} |
| 12 | + |
| 13 | + def update_config(self, **model_config): |
| 14 | + self.config.update(model_config) |
| 15 | + |
| 16 | + def get_config(self): |
| 17 | + return self.config |
| 18 | + |
| 19 | + def structured_output(self, output_model, prompt, system_prompt=None, **kwargs): |
| 20 | + pass |
| 21 | + |
| 22 | + def stream(self, messages, tool_specs=None, system_prompt=None, **kwargs): |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +def test_filter_tool_specs_removes_output_schema_by_default(): |
| 27 | + """Test that outputSchema is removed when not explicitly supported.""" |
| 28 | + model = MockModel(supports_tool_output_schema=False) |
| 29 | + |
| 30 | + tool_specs = [ |
| 31 | + ToolSpec( |
| 32 | + name="test_tool", |
| 33 | + description="A test tool", |
| 34 | + inputSchema={"json": {"type": "object"}}, |
| 35 | + outputSchema={"json": {"type": "string"}}, |
| 36 | + ) |
| 37 | + ] |
| 38 | + |
| 39 | + filtered = model.filter_tool_specs(tool_specs) |
| 40 | + |
| 41 | + assert len(filtered) == 1 |
| 42 | + assert "name" in filtered[0] |
| 43 | + assert "description" in filtered[0] |
| 44 | + assert "inputSchema" in filtered[0] |
| 45 | + assert "outputSchema" not in filtered[0] |
| 46 | + |
| 47 | + |
| 48 | +def test_filter_tool_specs_preserves_output_schema_when_supported(): |
| 49 | + """Test that outputSchema is preserved when explicitly supported.""" |
| 50 | + model = MockModel(supports_tool_output_schema=True) |
| 51 | + |
| 52 | + tool_specs = [ |
| 53 | + ToolSpec( |
| 54 | + name="test_tool", |
| 55 | + description="A test tool", |
| 56 | + inputSchema={"json": {"type": "object"}}, |
| 57 | + outputSchema={"json": {"type": "string"}}, |
| 58 | + ) |
| 59 | + ] |
| 60 | + |
| 61 | + filtered = model.filter_tool_specs(tool_specs) |
| 62 | + |
| 63 | + assert len(filtered) == 1 |
| 64 | + assert filtered[0] == tool_specs[0] |
| 65 | + assert "outputSchema" in filtered[0] |
| 66 | + |
| 67 | + |
| 68 | +def test_filter_tool_specs_handles_missing_output_schema(): |
| 69 | + """Test that tools without outputSchema work correctly.""" |
| 70 | + model = MockModel(supports_tool_output_schema=False) |
| 71 | + |
| 72 | + tool_specs = [ToolSpec(name="test_tool", description="A test tool", inputSchema={"json": {"type": "object"}})] |
| 73 | + |
| 74 | + filtered = model.filter_tool_specs(tool_specs) |
| 75 | + |
| 76 | + assert len(filtered) == 1 |
| 77 | + assert "name" in filtered[0] |
| 78 | + assert "description" in filtered[0] |
| 79 | + assert "inputSchema" in filtered[0] |
| 80 | + assert "outputSchema" not in filtered[0] |
| 81 | + |
| 82 | + |
| 83 | +def test_filter_tool_specs_handles_none(): |
| 84 | + """Test that None tool_specs returns None.""" |
| 85 | + model = MockModel() |
| 86 | + |
| 87 | + filtered = model.filter_tool_specs(None) |
| 88 | + |
| 89 | + assert filtered is None |
| 90 | + |
| 91 | + |
| 92 | +def test_filter_tool_specs_handles_empty_list(): |
| 93 | + """Test that empty list returns empty list.""" |
| 94 | + model = MockModel() |
| 95 | + |
| 96 | + filtered = model.filter_tool_specs([]) |
| 97 | + |
| 98 | + assert filtered == [] |
| 99 | + |
| 100 | + |
| 101 | +def test_filter_tool_specs_multiple_tools(): |
| 102 | + """Test filtering multiple tools with mixed outputSchema presence.""" |
| 103 | + model = MockModel(supports_tool_output_schema=False) |
| 104 | + |
| 105 | + tool_specs = [ |
| 106 | + ToolSpec( |
| 107 | + name="tool1", |
| 108 | + description="First tool", |
| 109 | + inputSchema={"json": {"type": "object"}}, |
| 110 | + outputSchema={"json": {"type": "string"}}, |
| 111 | + ), |
| 112 | + ToolSpec(name="tool2", description="Second tool", inputSchema={"json": {"type": "object"}}), |
| 113 | + ToolSpec( |
| 114 | + name="tool3", |
| 115 | + description="Third tool", |
| 116 | + inputSchema={"json": {"type": "object"}}, |
| 117 | + outputSchema={"json": {"type": "array"}}, |
| 118 | + ), |
| 119 | + ] |
| 120 | + |
| 121 | + filtered = model.filter_tool_specs(tool_specs) |
| 122 | + |
| 123 | + assert len(filtered) == 3 |
| 124 | + for spec in filtered: |
| 125 | + assert "outputSchema" not in spec |
| 126 | + assert "name" in spec |
| 127 | + assert "description" in spec |
| 128 | + assert "inputSchema" in spec |
| 129 | + |
| 130 | + |
| 131 | +def test_update_config_changes_output_schema_support(): |
| 132 | + """Test that updating config can change outputSchema support.""" |
| 133 | + model = MockModel(supports_tool_output_schema=False) |
| 134 | + |
| 135 | + tool_specs = [ |
| 136 | + ToolSpec( |
| 137 | + name="test_tool", |
| 138 | + description="A test tool", |
| 139 | + inputSchema={"json": {"type": "object"}}, |
| 140 | + outputSchema={"json": {"type": "string"}}, |
| 141 | + ) |
| 142 | + ] |
| 143 | + |
| 144 | + # Initially, outputSchema should be filtered out |
| 145 | + filtered = model.filter_tool_specs(tool_specs) |
| 146 | + assert "outputSchema" not in filtered[0] |
| 147 | + |
| 148 | + # Update config to support outputSchema |
| 149 | + model.update_config(supports_tool_output_schema=True) |
| 150 | + |
| 151 | + # Now outputSchema should be preserved |
| 152 | + filtered = model.filter_tool_specs(tool_specs) |
| 153 | + assert "outputSchema" in filtered[0] |
0 commit comments