|
2 | 2 | from pydantic import BaseModel
|
3 | 3 |
|
4 | 4 | from agents import Agent, AgentOutputSchema, Handoff, RunContextWrapper, handoff
|
| 5 | +from agents.lifecycle import AgentHooksBase |
| 6 | +from agents.model_settings import ModelSettings |
5 | 7 | from agents.run import AgentRunner
|
6 | 8 |
|
7 | 9 |
|
@@ -167,3 +169,58 @@ async def test_agent_final_output():
|
167 | 169 | assert schema.is_strict_json_schema() is True
|
168 | 170 | assert schema.json_schema() is not None
|
169 | 171 | assert not schema.is_plain_text()
|
| 172 | + |
| 173 | + |
| 174 | +class TestAgentValidation: |
| 175 | + """Essential validation tests for Agent __post_init__""" |
| 176 | + |
| 177 | + def test_name_validation_critical_cases(self): |
| 178 | + """Test name validation - the original issue that started this PR""" |
| 179 | + # This was the original failing case that caused JSON serialization errors |
| 180 | + with pytest.raises(TypeError, match="Agent name must be a string, got int"): |
| 181 | + Agent(name=1) |
| 182 | + |
| 183 | + with pytest.raises(TypeError, match="Agent name must be a string, got NoneType"): |
| 184 | + Agent(name=None) |
| 185 | + |
| 186 | + def test_tool_use_behavior_dict_validation(self): |
| 187 | + """Test tool_use_behavior accepts StopAtTools dict - fixes existing test failures""" |
| 188 | + # This test ensures the existing failing tests now pass |
| 189 | + Agent(name="test", tool_use_behavior={"stop_at_tool_names": ["tool1"]}) |
| 190 | + |
| 191 | + # Invalid cases that should fail |
| 192 | + with pytest.raises(TypeError, match="Agent tool_use_behavior must be"): |
| 193 | + Agent(name="test", tool_use_behavior=123) |
| 194 | + |
| 195 | + def test_hooks_validation_python39_compatibility(self): |
| 196 | + """Test hooks validation works with Python 3.9 - fixes generic type issues""" |
| 197 | + |
| 198 | + class MockHooks(AgentHooksBase): |
| 199 | + pass |
| 200 | + |
| 201 | + # Valid case |
| 202 | + Agent(name="test", hooks=MockHooks()) |
| 203 | + |
| 204 | + # Invalid case |
| 205 | + with pytest.raises(TypeError, match="Agent hooks must be an AgentHooks instance"): |
| 206 | + Agent(name="test", hooks="invalid") |
| 207 | + |
| 208 | + def test_list_field_validation(self): |
| 209 | + """Test critical list fields that commonly get wrong types""" |
| 210 | + # These are the most common mistakes users make |
| 211 | + with pytest.raises(TypeError, match="Agent tools must be a list"): |
| 212 | + Agent(name="test", tools="not_a_list") |
| 213 | + |
| 214 | + with pytest.raises(TypeError, match="Agent handoffs must be a list"): |
| 215 | + Agent(name="test", handoffs="not_a_list") |
| 216 | + |
| 217 | + def test_model_settings_validation(self): |
| 218 | + """Test model_settings validation - prevents runtime errors""" |
| 219 | + # Valid case |
| 220 | + Agent(name="test", model_settings=ModelSettings()) |
| 221 | + |
| 222 | + # Invalid case that could cause runtime issues |
| 223 | + with pytest.raises( |
| 224 | + TypeError, match="Agent model_settings must be a ModelSettings instance" |
| 225 | + ): |
| 226 | + Agent(name="test", model_settings={}) |
0 commit comments