Skip to content

Commit ff402cf

Browse files
fixed errors
1 parent 69c406f commit ff402cf

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

src/agents/agent.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,15 @@ async def get_system_prompt(self, run_context: RunContextWrapper[TContext]) -> s
414414
return cast(str, self.instructions(run_context, self))
415415

416416
elif self.instructions is not None:
417-
logger.error(f"Instructions must be a string or a callable function, got {type(self.instructions).__name__}")
417+
logger.error(
418+
f"Instructions must be a string or a callable function, "
419+
f"got {type(self.instructions).__name__}"
420+
)
418421

419422
return None
420423

421424
async def get_prompt(
422425
self, run_context: RunContextWrapper[TContext]
423426
) -> ResponsePromptParam | None:
424427
"""Get the prompt for the agent."""
425-
return await PromptUtil.to_model_input(self.prompt, run_context, self)
428+
return await PromptUtil.to_model_input(self.prompt, run_context, self)
Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,111 @@
1-
import pytest
21
from unittest.mock import Mock
3-
# Adjust import based on actual repo structure
4-
from src.agents.agent import Agent, RunContextWrapper
2+
3+
import pytest
4+
5+
from agents.agent import Agent, RunContextWrapper
6+
57

68
class TestInstructionsSignatureValidation:
79
"""Test suite for instructions function signature validation"""
8-
10+
911
@pytest.fixture
1012
def mock_run_context(self):
1113
"""Create a mock RunContextWrapper for testing"""
1214
return Mock(spec=RunContextWrapper)
13-
15+
1416
@pytest.mark.asyncio
1517
async def test_valid_async_signature_passes(self, mock_run_context):
1618
"""Test that async function with correct signature works"""
1719
async def valid_instructions(context, agent):
1820
return "Valid async instructions"
19-
20-
agent = Agent(instructions=valid_instructions)
21+
22+
agent = Agent(name="test_agent", instructions=valid_instructions)
2123
result = await agent.get_system_prompt(mock_run_context)
2224
assert result == "Valid async instructions"
23-
25+
2426
@pytest.mark.asyncio
2527
async def test_valid_sync_signature_passes(self, mock_run_context):
2628
"""Test that sync function with correct signature works"""
2729
def valid_instructions(context, agent):
2830
return "Valid sync instructions"
29-
30-
agent = Agent(instructions=valid_instructions)
31+
32+
agent = Agent(name="test_agent", instructions=valid_instructions)
3133
result = await agent.get_system_prompt(mock_run_context)
3234
assert result == "Valid sync instructions"
33-
35+
3436
@pytest.mark.asyncio
3537
async def test_one_parameter_raises_error(self, mock_run_context):
3638
"""Test that function with only one parameter raises TypeError"""
3739
def invalid_instructions(context):
3840
return "Should fail"
39-
40-
agent = Agent(instructions=invalid_instructions)
41-
41+
42+
agent = Agent(name="test_agent", instructions=invalid_instructions)
43+
4244
with pytest.raises(TypeError) as exc_info:
4345
await agent.get_system_prompt(mock_run_context)
44-
46+
4547
assert "must accept exactly 2 arguments" in str(exc_info.value)
4648
assert "but got 1" in str(exc_info.value)
47-
49+
4850
@pytest.mark.asyncio
4951
async def test_three_parameters_raises_error(self, mock_run_context):
5052
"""Test that function with three parameters raises TypeError"""
5153
def invalid_instructions(context, agent, extra):
5254
return "Should fail"
53-
54-
agent = Agent(instructions=invalid_instructions)
55-
55+
56+
agent = Agent(name="test_agent", instructions=invalid_instructions)
57+
5658
with pytest.raises(TypeError) as exc_info:
5759
await agent.get_system_prompt(mock_run_context)
58-
60+
5961
assert "must accept exactly 2 arguments" in str(exc_info.value)
6062
assert "but got 3" in str(exc_info.value)
61-
63+
6264
@pytest.mark.asyncio
6365
async def test_zero_parameters_raises_error(self, mock_run_context):
6466
"""Test that function with no parameters raises TypeError"""
6567
def invalid_instructions():
6668
return "Should fail"
67-
68-
agent = Agent(instructions=invalid_instructions)
69-
69+
70+
agent = Agent(name="test_agent", instructions=invalid_instructions)
71+
7072
with pytest.raises(TypeError) as exc_info:
7173
await agent.get_system_prompt(mock_run_context)
72-
74+
7375
assert "must accept exactly 2 arguments" in str(exc_info.value)
7476
assert "but got 0" in str(exc_info.value)
75-
77+
7678
@pytest.mark.asyncio
7779
async def test_function_with_args_kwargs_passes(self, mock_run_context):
7880
"""Test that function with *args/**kwargs still works (edge case)"""
7981
def flexible_instructions(context, agent, *args, **kwargs):
8082
return "Flexible instructions"
81-
82-
agent = Agent(instructions=flexible_instructions)
83+
84+
agent = Agent(name="test_agent", instructions=flexible_instructions)
8385
# This should potentially pass as it can accept the 2 required args
8486
# Adjust this test based on your desired behavior
8587
result = await agent.get_system_prompt(mock_run_context)
8688
assert result == "Flexible instructions"
87-
89+
8890
@pytest.mark.asyncio
8991
async def test_string_instructions_still_work(self, mock_run_context):
9092
"""Test that string instructions continue to work"""
91-
agent = Agent(instructions="Static string instructions")
93+
agent = Agent(name="test_agent", instructions="Static string instructions")
9294
result = await agent.get_system_prompt(mock_run_context)
9395
assert result == "Static string instructions"
94-
96+
9597
@pytest.mark.asyncio
9698
async def test_none_instructions_return_none(self, mock_run_context):
9799
"""Test that None instructions return None"""
98-
agent = Agent(instructions=None)
100+
agent = Agent(name="test_agent", instructions=None)
99101
result = await agent.get_system_prompt(mock_run_context)
100102
assert result is None
101-
103+
102104
@pytest.mark.asyncio
103105
async def test_non_callable_instructions_log_error(self, mock_run_context, caplog):
104106
"""Test that non-callable instructions log an error"""
105-
agent = Agent(instructions=123) # Invalid type
107+
agent = Agent(name="test_agent", instructions=123) # Invalid type
106108
result = await agent.get_system_prompt(mock_run_context)
107109
assert result is None
108110
# Check that error was logged (adjust based on actual logging setup)
109-
# assert "Instructions must be a string or a function" in caplog.text
111+
# assert "Instructions must be a string or a function" in caplog.text

0 commit comments

Comments
 (0)