forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_agent_tracing.py
322 lines (255 loc) · 8.67 KB
/
test_agent_tracing.py
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
321
322
from __future__ import annotations
import asyncio
import pytest
from agents import Agent, RunConfig, Runner, trace
from .fake_model import FakeModel
from .test_responses import get_text_message
from .testing_processor import fetch_ordered_spans, fetch_traces
@pytest.mark.asyncio
async def test_single_run_is_single_trace():
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
await Runner.run(agent, input="first_test")
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 1, (
f"Got {len(spans)}, but expected 1: the agent span. data:"
f"{[span.span_data for span in spans]}"
)
@pytest.mark.asyncio
async def test_multiple_runs_are_multiple_traces():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
]
)
agent = Agent(
name="test_agent_1",
model=model,
)
await Runner.run(agent, input="first_test")
await Runner.run(agent, input="second_test")
traces = fetch_traces()
assert len(traces) == 2, f"Expected 2 traces, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 2, f"Got {len(spans)}, but expected 2: agent span per run"
@pytest.mark.asyncio
async def test_wrapped_trace_is_single_trace():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
[get_text_message("third_test")],
]
)
with trace(workflow_name="test_workflow"):
agent = Agent(
name="test_agent_1",
model=model,
)
await Runner.run(agent, input="first_test")
await Runner.run(agent, input="second_test")
await Runner.run(agent, input="third_test")
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 3, f"Got {len(spans)}, but expected 3: the agent span per run"
@pytest.mark.asyncio
async def test_parent_disabled_trace_disabled_agent_trace():
with trace(workflow_name="test_workflow", disabled=True):
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
await Runner.run(agent, input="first_test")
traces = fetch_traces()
assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 0, (
f"Expected no spans, got {len(spans)}, with {[x.span_data for x in spans]}"
)
@pytest.mark.asyncio
async def test_manual_disabling_works():
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
await Runner.run(agent, input="first_test", run_config=RunConfig(tracing_disabled=True))
traces = fetch_traces()
assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 0, f"Got {len(spans)}, but expected no spans"
@pytest.mark.asyncio
async def test_trace_config_works():
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
await Runner.run(
agent,
input="first_test",
run_config=RunConfig(workflow_name="Foo bar", group_id="123", trace_id="456"),
)
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
export = traces[0].export()
assert export is not None, "Trace export should not be None"
assert export["workflow_name"] == "Foo bar"
assert export["group_id"] == "123"
assert export["id"] == "456"
@pytest.mark.asyncio
async def test_not_starting_streaming_creates_trace():
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
result = Runner.run_streamed(agent, input="first_test")
# Purposely don't await the stream
while True:
if result.is_complete:
break
await asyncio.sleep(0.1)
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
spans = fetch_ordered_spans()
assert len(spans) == 1, f"Got {len(spans)}, but expected 1: the agent span"
# Await the stream to avoid warnings about it not being awaited
async for _ in result.stream_events():
pass
@pytest.mark.asyncio
async def test_streaming_single_run_is_single_trace():
agent = Agent(
name="test_agent",
model=FakeModel(
initial_output=[get_text_message("first_test")],
),
)
x = Runner.run_streamed(agent, input="first_test")
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
@pytest.mark.asyncio
async def test_multiple_streamed_runs_are_multiple_traces():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
]
)
agent = Agent(
name="test_agent_1",
model=model,
)
x = Runner.run_streamed(agent, input="first_test")
async for _ in x.stream_events():
pass
x = Runner.run_streamed(agent, input="second_test")
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 2, f"Expected 2 traces, got {len(traces)}"
@pytest.mark.asyncio
async def test_wrapped_streaming_trace_is_single_trace():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
[get_text_message("third_test")],
]
)
with trace(workflow_name="test_workflow"):
agent = Agent(
name="test_agent_1",
model=model,
)
x = Runner.run_streamed(agent, input="first_test")
async for _ in x.stream_events():
pass
x = Runner.run_streamed(agent, input="second_test")
async for _ in x.stream_events():
pass
x = Runner.run_streamed(agent, input="third_test")
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
@pytest.mark.asyncio
async def test_wrapped_mixed_trace_is_single_trace():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
[get_text_message("third_test")],
]
)
with trace(workflow_name="test_workflow"):
agent = Agent(
name="test_agent_1",
model=model,
)
x = Runner.run_streamed(agent, input="first_test")
async for _ in x.stream_events():
pass
await Runner.run(agent, input="second_test")
x = Runner.run_streamed(agent, input="third_test")
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 1, f"Expected 1 trace, got {len(traces)}"
@pytest.mark.asyncio
async def test_parent_disabled_trace_disables_streaming_agent_trace():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
]
)
with trace(workflow_name="test_workflow", disabled=True):
agent = Agent(
name="test_agent",
model=model,
)
x = Runner.run_streamed(agent, input="first_test")
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}"
@pytest.mark.asyncio
async def test_manual_streaming_disabling_works():
model = FakeModel()
model.add_multiple_turn_outputs(
[
[get_text_message("first_test")],
[get_text_message("second_test")],
]
)
agent = Agent(
name="test_agent",
model=model,
)
x = Runner.run_streamed(agent, input="first_test", run_config=RunConfig(tracing_disabled=True))
async for _ in x.stream_events():
pass
traces = fetch_traces()
assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}"