forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_run_hooks.py
More file actions
320 lines (256 loc) · 10.7 KB
/
test_run_hooks.py
File metadata and controls
320 lines (256 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from collections import defaultdict
from typing import Any, Optional, cast
import pytest
from agents.agent import Agent
from agents.items import ItemHelpers, ModelResponse, TResponseInputItem
from agents.lifecycle import AgentHooks, RunHooks
from agents.models.interface import Model
from agents.run import Runner
from agents.run_context import AgentHookContext, RunContextWrapper, TContext
from agents.tool import Tool
from agents.tool_context import ToolContext
from tests.test_agent_llm_hooks import AgentHooksForTests
from .fake_model import FakeModel
from .test_responses import (
get_function_tool,
get_text_message,
)
class RunHooksForTests(RunHooks):
def __init__(self):
self.events: dict[str, int] = defaultdict(int)
self.tool_context_ids: list[str] = []
def reset(self):
self.events.clear()
self.tool_context_ids.clear()
async def on_agent_start(
self, context: AgentHookContext[TContext], agent: Agent[TContext]
) -> None:
self.events["on_agent_start"] += 1
async def on_agent_end(
self, context: RunContextWrapper[TContext], agent: Agent[TContext], output: Any
) -> None:
self.events["on_agent_end"] += 1
async def on_handoff(
self,
context: RunContextWrapper[TContext],
from_agent: Agent[TContext],
to_agent: Agent[TContext],
) -> None:
self.events["on_handoff"] += 1
async def on_tool_start(
self, context: RunContextWrapper[TContext], agent: Agent[TContext], tool: Tool
) -> None:
self.events["on_tool_start"] += 1
async def on_tool_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
tool: Tool,
result: str,
) -> None:
self.events["on_tool_end"] += 1
if isinstance(context, ToolContext):
self.tool_context_ids.append(context.tool_call_id)
async def on_llm_start(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
system_prompt: Optional[str],
input_items: list[TResponseInputItem],
) -> None:
self.events["on_llm_start"] += 1
async def on_llm_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
response: ModelResponse,
) -> None:
self.events["on_llm_end"] += 1
# Example test using the above hooks
@pytest.mark.asyncio
async def test_async_run_hooks_with_llm():
hooks = RunHooksForTests()
model = FakeModel()
agent = Agent(name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[])
# Simulate a single LLM call producing an output:
model.set_next_output([get_text_message("hello")])
await Runner.run(agent, input="hello", hooks=hooks)
# Expect one on_agent_start, one on_llm_start, one on_llm_end, and one on_agent_end
assert hooks.events == {
"on_agent_start": 1,
"on_llm_start": 1,
"on_llm_end": 1,
"on_agent_end": 1,
}
# test_sync_run_hook_with_llm()
def test_sync_run_hook_with_llm():
hooks = RunHooksForTests()
model = FakeModel()
agent = Agent(name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[])
# Simulate a single LLM call producing an output:
model.set_next_output([get_text_message("hello")])
Runner.run_sync(agent, input="hello", hooks=hooks)
# Expect one on_agent_start, one on_llm_start, one on_llm_end, and one on_agent_end
assert hooks.events == {
"on_agent_start": 1,
"on_llm_start": 1,
"on_llm_end": 1,
"on_agent_end": 1,
}
# test_streamed_run_hooks_with_llm():
@pytest.mark.asyncio
async def test_streamed_run_hooks_with_llm():
hooks = RunHooksForTests()
model = FakeModel()
agent = Agent(name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[])
# Simulate a single LLM call producing an output:
model.set_next_output([get_text_message("hello")])
stream = Runner.run_streamed(agent, input="hello", hooks=hooks)
async for event in stream.stream_events():
if event.type == "raw_response_event":
continue
if event.type == "agent_updated_stream_event":
print(f"[EVENT] agent_updated → {event.new_agent.name}")
elif event.type == "run_item_stream_event":
item = event.item
if item.type == "tool_call_item":
print("[EVENT] tool_call_item")
elif item.type == "tool_call_output_item":
print(f"[EVENT] tool_call_output_item → {item.output}")
elif item.type == "message_output_item":
text = ItemHelpers.text_message_output(item)
print(f"[EVENT] message_output_item → {text}")
# Expect one on_agent_start, one on_llm_start, one on_llm_end, and one on_agent_end
assert hooks.events == {
"on_agent_start": 1,
"on_llm_start": 1,
"on_llm_end": 1,
"on_agent_end": 1,
}
# test_async_run_hooks_with_agent_hooks_with_llm
@pytest.mark.asyncio
async def test_async_run_hooks_with_agent_hooks_with_llm():
hooks = RunHooksForTests()
agent_hooks = AgentHooksForTests()
model = FakeModel()
agent = Agent(
name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[], hooks=agent_hooks
)
# Simulate a single LLM call producing an output:
model.set_next_output([get_text_message("hello")])
await Runner.run(agent, input="hello", hooks=hooks)
# Expect one on_agent_start, one on_llm_start, one on_llm_end, and one on_agent_end
assert hooks.events == {
"on_agent_start": 1,
"on_llm_start": 1,
"on_llm_end": 1,
"on_agent_end": 1,
}
# Expect one on_start, one on_llm_start, one on_llm_end, and one on_end
assert agent_hooks.events == {"on_start": 1, "on_llm_start": 1, "on_llm_end": 1, "on_end": 1}
@pytest.mark.asyncio
async def test_run_hooks_llm_error_non_streaming(monkeypatch):
hooks = RunHooksForTests()
model = FakeModel()
agent = Agent(name="A", model=model, tools=[get_function_tool("f", "res")], handoffs=[])
async def boom(*args, **kwargs):
raise RuntimeError("boom")
monkeypatch.setattr(FakeModel, "get_response", boom, raising=True)
with pytest.raises(RuntimeError, match="boom"):
await Runner.run(agent, input="hello", hooks=hooks)
# Current behavior is that hooks will not fire on LLM failure
assert hooks.events["on_agent_start"] == 1
assert hooks.events["on_llm_start"] == 1
assert hooks.events["on_llm_end"] == 0
assert hooks.events["on_agent_end"] == 0
class DummyAgentHooks(AgentHooks):
"""Agent-scoped hooks used to verify runtime validation."""
@pytest.mark.asyncio
async def test_runner_run_rejects_agent_hooks():
model = FakeModel()
agent = Agent(name="A", model=model)
hooks = cast(RunHooks, DummyAgentHooks())
with pytest.raises(TypeError, match="Run hooks must be instances of RunHooks"):
await Runner.run(agent, input="hello", hooks=hooks)
def test_runner_run_streamed_rejects_agent_hooks():
model = FakeModel()
agent = Agent(name="A", model=model)
hooks = cast(RunHooks, DummyAgentHooks())
with pytest.raises(TypeError, match="Run hooks must be instances of RunHooks"):
Runner.run_streamed(agent, input="hello", hooks=hooks)
class BoomModel(Model):
async def get_response(self, *a, **k):
raise AssertionError("get_response should not be called in streaming test")
async def stream_response(self, *a, **k):
yield {"foo": "bar"}
raise RuntimeError("stream blew up")
@pytest.mark.asyncio
async def test_streamed_run_hooks_llm_error(monkeypatch):
"""
Verify that when the streaming path raises, we still emit on_llm_start
but do NOT emit on_llm_end (current behavior), and the exception propagates.
"""
hooks = RunHooksForTests()
agent = Agent(name="A", model=BoomModel(), tools=[get_function_tool("f", "res")], handoffs=[])
stream = Runner.run_streamed(agent, input="hello", hooks=hooks)
# Consuming the stream should surface the exception
with pytest.raises(RuntimeError, match="stream blew up"):
async for _ in stream.stream_events():
pass
# Current behavior: success-only on_llm_end; ensure starts fired but ends did not.
assert hooks.events["on_agent_start"] == 1
assert hooks.events["on_llm_start"] == 1
assert hooks.events["on_llm_end"] == 0
assert hooks.events["on_agent_end"] == 0
class RunHooksWithTurnInput(RunHooks):
"""Run hooks that capture turn_input from on_agent_start."""
def __init__(self):
self.captured_turn_inputs: list[list[Any]] = []
async def on_agent_start(
self, context: AgentHookContext[TContext], agent: Agent[TContext]
) -> None:
self.captured_turn_inputs.append(list(context.turn_input))
@pytest.mark.asyncio
async def test_run_hooks_receives_turn_input_string():
"""Test that on_agent_start receives turn_input when input is a string."""
hooks = RunHooksWithTurnInput()
model = FakeModel()
agent = Agent(name="test", model=model)
model.set_next_output([get_text_message("response")])
await Runner.run(agent, input="hello world", hooks=hooks)
assert len(hooks.captured_turn_inputs) == 1
turn_input = hooks.captured_turn_inputs[0]
assert len(turn_input) == 1
assert turn_input[0]["content"] == "hello world"
assert turn_input[0]["role"] == "user"
@pytest.mark.asyncio
async def test_run_hooks_receives_turn_input_list():
"""Test that on_agent_start receives turn_input when input is a list."""
hooks = RunHooksWithTurnInput()
model = FakeModel()
agent = Agent(name="test", model=model)
input_items: list[Any] = [
{"role": "user", "content": "first message"},
{"role": "user", "content": "second message"},
]
model.set_next_output([get_text_message("response")])
await Runner.run(agent, input=input_items, hooks=hooks)
assert len(hooks.captured_turn_inputs) == 1
turn_input = hooks.captured_turn_inputs[0]
assert len(turn_input) == 2
assert turn_input[0]["content"] == "first message"
assert turn_input[1]["content"] == "second message"
@pytest.mark.asyncio
async def test_run_hooks_receives_turn_input_streamed():
"""Test that on_agent_start receives turn_input in streamed mode."""
hooks = RunHooksWithTurnInput()
model = FakeModel()
agent = Agent(name="test", model=model)
model.set_next_output([get_text_message("response")])
result = Runner.run_streamed(agent, input="streamed input", hooks=hooks)
async for _ in result.stream_events():
pass
assert len(hooks.captured_turn_inputs) == 1
turn_input = hooks.captured_turn_inputs[0]
assert len(turn_input) == 1
assert turn_input[0]["content"] == "streamed input"