deepeval 3.5.9__py3-none-any.whl → 3.6.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deepeval/_version.py +1 -1
- deepeval/config/settings_manager.py +1 -1
- deepeval/contextvars.py +25 -0
- deepeval/dataset/__init__.py +8 -2
- deepeval/evaluate/execute.py +15 -3
- deepeval/openai_agents/__init__.py +4 -3
- deepeval/openai_agents/agent.py +8 -166
- deepeval/openai_agents/callback_handler.py +69 -61
- deepeval/openai_agents/extractors.py +83 -7
- deepeval/openai_agents/patch.py +255 -61
- deepeval/openai_agents/runner.py +348 -335
- deepeval/tracing/context.py +1 -0
- deepeval/tracing/otel/exporter.py +6 -0
- deepeval/tracing/otel/utils.py +62 -0
- deepeval/tracing/tracing.py +3 -0
- deepeval/tracing/utils.py +54 -0
- deepeval/utils.py +4 -3
- {deepeval-3.5.9.dist-info → deepeval-3.6.1.dist-info}/METADATA +1 -1
- {deepeval-3.5.9.dist-info → deepeval-3.6.1.dist-info}/RECORD +22 -21
- {deepeval-3.5.9.dist-info → deepeval-3.6.1.dist-info}/LICENSE.md +0 -0
- {deepeval-3.5.9.dist-info → deepeval-3.6.1.dist-info}/WHEEL +0 -0
- {deepeval-3.5.9.dist-info → deepeval-3.6.1.dist-info}/entry_points.txt +0 -0
deepeval/openai_agents/runner.py
CHANGED
|
@@ -1,335 +1,348 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import replace
|
|
4
|
-
from typing import List, Any, Union, Optional
|
|
5
|
-
|
|
6
|
-
try:
|
|
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
|
-
from deepeval.tracing.
|
|
37
|
-
|
|
38
|
-
#
|
|
39
|
-
from deepeval.
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
1
|
+
# from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
# from dataclasses import replace
|
|
4
|
+
# from typing import List, Any, Union, Optional
|
|
5
|
+
|
|
6
|
+
# try:
|
|
7
|
+
# from agents import (
|
|
8
|
+
# RunConfig,
|
|
9
|
+
# RunResult,
|
|
10
|
+
# RunResultStreaming,
|
|
11
|
+
# Runner as AgentsRunner,
|
|
12
|
+
# )
|
|
13
|
+
# from agents.agent import Agent
|
|
14
|
+
# from agents.models.interface import ModelProvider
|
|
15
|
+
# from agents.items import TResponseInputItem
|
|
16
|
+
# from agents.lifecycle import RunHooks
|
|
17
|
+
# from agents.memory import Session
|
|
18
|
+
# from agents.run import DEFAULT_MAX_TURNS
|
|
19
|
+
# from agents.run import AgentRunner
|
|
20
|
+
# from agents.run_context import TContext
|
|
21
|
+
# from agents.models.interface import Model
|
|
22
|
+
# from agents.run import SingleStepResult
|
|
23
|
+
|
|
24
|
+
# agents_available = True
|
|
25
|
+
# except:
|
|
26
|
+
# agents_available = False
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# def is_agents_available():
|
|
30
|
+
# if not agents_available:
|
|
31
|
+
# raise ImportError(
|
|
32
|
+
# "agents is required for this integration. Install it via your package manager"
|
|
33
|
+
# )
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# from deepeval.tracing.tracing import Observer
|
|
37
|
+
# from deepeval.tracing.context import current_span_context, current_trace_context
|
|
38
|
+
# from deepeval.tracing.utils import make_json_serializable
|
|
39
|
+
# from deepeval.tracing.types import AgentSpan
|
|
40
|
+
|
|
41
|
+
# # Import observed provider/model helpers from our agent module
|
|
42
|
+
# from deepeval.metrics import BaseMetric
|
|
43
|
+
# from deepeval.openai_agents.agent import _ObservedModel
|
|
44
|
+
|
|
45
|
+
# _PATCHED_DEFAULT_GET_MODEL = False
|
|
46
|
+
# _PATCHED_DEFAULT_RUN_SINGLE_TURN = False
|
|
47
|
+
|
|
48
|
+
# def patch_default_agent_runner_get_model():
|
|
49
|
+
# global _PATCHED_DEFAULT_GET_MODEL
|
|
50
|
+
# if _PATCHED_DEFAULT_GET_MODEL:
|
|
51
|
+
# return
|
|
52
|
+
|
|
53
|
+
# original_get_model_cm = AgentRunner._get_model
|
|
54
|
+
# try:
|
|
55
|
+
# original_get_model = original_get_model_cm.__func__
|
|
56
|
+
# except AttributeError:
|
|
57
|
+
# original_get_model = original_get_model_cm # fallback (non-classmethod edge case)
|
|
58
|
+
|
|
59
|
+
# def patched_get_model(cls, *args, **kwargs) -> Model:
|
|
60
|
+
# model = original_get_model(cls, *args, **kwargs)
|
|
61
|
+
|
|
62
|
+
# agent = kwargs.get("agent") if "agent" in kwargs else (args[0] if args else None)
|
|
63
|
+
# if agent is None:
|
|
64
|
+
# return model
|
|
65
|
+
|
|
66
|
+
# if isinstance(model, _ObservedModel):
|
|
67
|
+
# return model
|
|
68
|
+
|
|
69
|
+
# llm_metrics = getattr(agent, "llm_metrics", None)
|
|
70
|
+
# llm_metric_collection = getattr(agent, "llm_metric_collection", None)
|
|
71
|
+
# confident_prompt = getattr(agent, "confident_prompt", None)
|
|
72
|
+
# return _ObservedModel(
|
|
73
|
+
# inner=model,
|
|
74
|
+
# llm_metric_collection=llm_metric_collection,
|
|
75
|
+
# llm_metrics=llm_metrics,
|
|
76
|
+
# confident_prompt=confident_prompt,
|
|
77
|
+
# )
|
|
78
|
+
|
|
79
|
+
# # Preserve basic metadata and mark as patched
|
|
80
|
+
# patched_get_model.__name__ = original_get_model.__name__
|
|
81
|
+
# patched_get_model.__doc__ = original_get_model.__doc__
|
|
82
|
+
|
|
83
|
+
# AgentRunner._get_model = classmethod(patched_get_model)
|
|
84
|
+
# _PATCHED_DEFAULT_GET_MODEL = True
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# # if agents_available:
|
|
88
|
+
# # patch_default_agent_run_single_turn()
|
|
89
|
+
# # patch_single_turn_streamed()
|
|
90
|
+
# # patch_default_agent_runner_get_model()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# class Runner(AgentsRunner):
|
|
94
|
+
|
|
95
|
+
# @classmethod
|
|
96
|
+
# async def run(
|
|
97
|
+
# cls,
|
|
98
|
+
# starting_agent: Agent[TContext],
|
|
99
|
+
# input: Union[str, list[TResponseInputItem]],
|
|
100
|
+
# *,
|
|
101
|
+
# context: Optional[TContext] = None,
|
|
102
|
+
# max_turns: int = DEFAULT_MAX_TURNS,
|
|
103
|
+
# hooks: Optional[RunHooks[TContext]] = None,
|
|
104
|
+
# run_config: Optional[RunConfig] = None,
|
|
105
|
+
# previous_response_id: Optional[str] = None,
|
|
106
|
+
# conversation_id: Optional[str] = None,
|
|
107
|
+
# session: Optional[Session] = None,
|
|
108
|
+
# metrics: Optional[List[BaseMetric]] = None,
|
|
109
|
+
# metric_collection: Optional[str] = None,
|
|
110
|
+
# name: Optional[str] = None,
|
|
111
|
+
# tags: Optional[List[str]] = None,
|
|
112
|
+
# metadata: Optional[dict] = None,
|
|
113
|
+
# thread_id: Optional[str] = None,
|
|
114
|
+
# user_id: Optional[str] = None,
|
|
115
|
+
# **kwargs, # backwards compatibility
|
|
116
|
+
# ) -> RunResult:
|
|
117
|
+
# is_agents_available()
|
|
118
|
+
# # _patch_default_agent_runner_get_model()
|
|
119
|
+
|
|
120
|
+
# with Observer(
|
|
121
|
+
# span_type="custom",
|
|
122
|
+
# metric_collection=metric_collection,
|
|
123
|
+
# metrics=metrics,
|
|
124
|
+
# func_name="run",
|
|
125
|
+
# function_kwargs={"input": input}, # also set below
|
|
126
|
+
# ) as observer:
|
|
127
|
+
# update_trace_attributes(
|
|
128
|
+
# name=name,
|
|
129
|
+
# tags=tags,
|
|
130
|
+
# metadata=metadata,
|
|
131
|
+
# thread_id=thread_id,
|
|
132
|
+
# user_id=user_id,
|
|
133
|
+
# metric_collection=metric_collection,
|
|
134
|
+
# metrics=metrics,
|
|
135
|
+
# )
|
|
136
|
+
# current_span = current_span_context.get()
|
|
137
|
+
# current_trace = current_trace_context.get()
|
|
138
|
+
# if not current_trace.input:
|
|
139
|
+
# current_trace.input = input
|
|
140
|
+
# if current_span:
|
|
141
|
+
# current_span.input = input
|
|
142
|
+
# res = await super().run(
|
|
143
|
+
# starting_agent,
|
|
144
|
+
# input,
|
|
145
|
+
# context=context,
|
|
146
|
+
# max_turns=max_turns,
|
|
147
|
+
# hooks=hooks,
|
|
148
|
+
# run_config=run_config,
|
|
149
|
+
# previous_response_id=previous_response_id,
|
|
150
|
+
# conversation_id=conversation_id,
|
|
151
|
+
# session=session,
|
|
152
|
+
# **kwargs, # backwards compatibility
|
|
153
|
+
# )
|
|
154
|
+
# current_trace_thread_id = current_trace_context.get().thread_id
|
|
155
|
+
# _output = None
|
|
156
|
+
# if current_trace_thread_id:
|
|
157
|
+
# _output = res.final_output
|
|
158
|
+
# else:
|
|
159
|
+
# _output = str(res)
|
|
160
|
+
# observer.result = _output
|
|
161
|
+
# update_trace_attributes(output=_output)
|
|
162
|
+
# return res
|
|
163
|
+
|
|
164
|
+
# @classmethod
|
|
165
|
+
# def run_sync(
|
|
166
|
+
# cls,
|
|
167
|
+
# starting_agent: Agent[TContext],
|
|
168
|
+
# input: Union[str, list[TResponseInputItem]],
|
|
169
|
+
# *,
|
|
170
|
+
# context: Optional[TContext] = None,
|
|
171
|
+
# max_turns: int = DEFAULT_MAX_TURNS,
|
|
172
|
+
# hooks: Optional[RunHooks[TContext]] = None,
|
|
173
|
+
# run_config: Optional[RunConfig] = None,
|
|
174
|
+
# previous_response_id: Optional[str] = None,
|
|
175
|
+
# conversation_id: Optional[str] = None,
|
|
176
|
+
# session: Optional[Session] = None,
|
|
177
|
+
# metrics: Optional[List[BaseMetric]] = None,
|
|
178
|
+
# metric_collection: Optional[str] = None,
|
|
179
|
+
# name: Optional[str] = None,
|
|
180
|
+
# tags: Optional[List[str]] = None,
|
|
181
|
+
# metadata: Optional[dict] = None,
|
|
182
|
+
# thread_id: Optional[str] = None,
|
|
183
|
+
# user_id: Optional[str] = None,
|
|
184
|
+
# **kwargs,
|
|
185
|
+
# ) -> RunResult:
|
|
186
|
+
# is_agents_available()
|
|
187
|
+
|
|
188
|
+
# with Observer(
|
|
189
|
+
# span_type="custom",
|
|
190
|
+
# metric_collection=metric_collection,
|
|
191
|
+
# metrics=metrics,
|
|
192
|
+
# func_name="run_sync",
|
|
193
|
+
# function_kwargs={"input": input}, # also set below
|
|
194
|
+
# ) as observer:
|
|
195
|
+
# update_trace_attributes(
|
|
196
|
+
# name=name,
|
|
197
|
+
# tags=tags,
|
|
198
|
+
# metadata=metadata,
|
|
199
|
+
# thread_id=thread_id,
|
|
200
|
+
# user_id=user_id,
|
|
201
|
+
# metric_collection=metric_collection,
|
|
202
|
+
# metrics=metrics,
|
|
203
|
+
# )
|
|
204
|
+
|
|
205
|
+
# current_span = current_span_context.get()
|
|
206
|
+
# current_trace = current_trace_context.get()
|
|
207
|
+
# if not current_trace.input:
|
|
208
|
+
# current_trace.input = input
|
|
209
|
+
# if current_span:
|
|
210
|
+
# current_span.input = input
|
|
211
|
+
# res = super().run_sync(
|
|
212
|
+
# starting_agent,
|
|
213
|
+
# input,
|
|
214
|
+
# context=context,
|
|
215
|
+
# max_turns=max_turns,
|
|
216
|
+
# hooks=hooks,
|
|
217
|
+
# run_config=run_config,
|
|
218
|
+
# previous_response_id=previous_response_id,
|
|
219
|
+
# conversation_id=conversation_id,
|
|
220
|
+
# session=session,
|
|
221
|
+
# **kwargs, # backwards compatibility
|
|
222
|
+
# )
|
|
223
|
+
# current_trace_thread_id = current_trace_context.get().thread_id
|
|
224
|
+
# _output = None
|
|
225
|
+
# if current_trace_thread_id:
|
|
226
|
+
# _output = res.final_output
|
|
227
|
+
# else:
|
|
228
|
+
# _output = str(res)
|
|
229
|
+
# update_trace_attributes(output=_output)
|
|
230
|
+
# observer.result = _output
|
|
231
|
+
|
|
232
|
+
# return res
|
|
233
|
+
|
|
234
|
+
# @classmethod
|
|
235
|
+
# def run_streamed(
|
|
236
|
+
# cls,
|
|
237
|
+
# starting_agent: Agent[TContext],
|
|
238
|
+
# input: Union[str, list[TResponseInputItem]],
|
|
239
|
+
# *,
|
|
240
|
+
# context: Optional[TContext] = None,
|
|
241
|
+
# max_turns: int = DEFAULT_MAX_TURNS,
|
|
242
|
+
# hooks: Optional[RunHooks[TContext]] = None,
|
|
243
|
+
# run_config: Optional[RunConfig] = None,
|
|
244
|
+
# previous_response_id: Optional[str] = None,
|
|
245
|
+
# conversation_id: Optional[str] = None,
|
|
246
|
+
# session: Optional[Session] = None,
|
|
247
|
+
# metrics: Optional[List[BaseMetric]] = None,
|
|
248
|
+
# metric_collection: Optional[str] = None,
|
|
249
|
+
# name: Optional[str] = None,
|
|
250
|
+
# tags: Optional[List[str]] = None,
|
|
251
|
+
# metadata: Optional[dict] = None,
|
|
252
|
+
# thread_id: Optional[str] = None,
|
|
253
|
+
# user_id: Optional[str] = None,
|
|
254
|
+
# **kwargs, # backwards compatibility
|
|
255
|
+
# ) -> RunResultStreaming:
|
|
256
|
+
# is_agents_available()
|
|
257
|
+
# # Manually enter observer; we'll exit when streaming finishes
|
|
258
|
+
# observer = Observer(
|
|
259
|
+
# span_type="custom",
|
|
260
|
+
# metric_collection=metric_collection,
|
|
261
|
+
# metrics=metrics,
|
|
262
|
+
# func_name="run_streamed",
|
|
263
|
+
# function_kwargs={"input": input},
|
|
264
|
+
# )
|
|
265
|
+
# observer.__enter__()
|
|
266
|
+
|
|
267
|
+
# update_trace_attributes(
|
|
268
|
+
# name=name,
|
|
269
|
+
# tags=tags,
|
|
270
|
+
# metadata=metadata,
|
|
271
|
+
# thread_id=thread_id,
|
|
272
|
+
# user_id=user_id,
|
|
273
|
+
# metric_collection=metric_collection,
|
|
274
|
+
# metrics=metrics,
|
|
275
|
+
# )
|
|
276
|
+
# current_trace = current_trace_context.get()
|
|
277
|
+
# if not current_trace.input:
|
|
278
|
+
# current_trace.input = input
|
|
279
|
+
|
|
280
|
+
# current_span = current_span_context.get()
|
|
281
|
+
# if current_span:
|
|
282
|
+
# current_span.input = input
|
|
283
|
+
|
|
284
|
+
# res = super().run_streamed(
|
|
285
|
+
# starting_agent,
|
|
286
|
+
# input,
|
|
287
|
+
# context=context,
|
|
288
|
+
# max_turns=max_turns,
|
|
289
|
+
# hooks=hooks,
|
|
290
|
+
# run_config=run_config,
|
|
291
|
+
# previous_response_id=previous_response_id,
|
|
292
|
+
# conversation_id=conversation_id,
|
|
293
|
+
# session=session,
|
|
294
|
+
# **kwargs, # backwards compatibility
|
|
295
|
+
# )
|
|
296
|
+
|
|
297
|
+
# # Runtime-patch stream_events so the observer closes only after streaming completes
|
|
298
|
+
# orig_stream_events = res.stream_events
|
|
299
|
+
|
|
300
|
+
# async def _patched_stream_events(self: RunResultStreaming):
|
|
301
|
+
# try:
|
|
302
|
+
# async for event in orig_stream_events():
|
|
303
|
+
# yield event
|
|
304
|
+
# observer.result = self.final_output
|
|
305
|
+
# update_trace_attributes(output=self.final_output)
|
|
306
|
+
# except Exception as e:
|
|
307
|
+
# observer.__exit__(type(e), e, e.__traceback__)
|
|
308
|
+
# raise
|
|
309
|
+
# finally:
|
|
310
|
+
# observer.__exit__(None, None, None)
|
|
311
|
+
|
|
312
|
+
# from types import MethodType as _MethodType
|
|
313
|
+
|
|
314
|
+
# res.stream_events = _MethodType(_patched_stream_events, res)
|
|
315
|
+
|
|
316
|
+
# return res
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
# def update_trace_attributes(
|
|
320
|
+
# input: Any = None,
|
|
321
|
+
# output: Any = None,
|
|
322
|
+
# name: str = None,
|
|
323
|
+
# tags: List[str] = None,
|
|
324
|
+
# metadata: dict = None,
|
|
325
|
+
# thread_id: str = None,
|
|
326
|
+
# user_id: str = None,
|
|
327
|
+
# metric_collection: str = None,
|
|
328
|
+
# metrics: List[BaseMetric] = None,
|
|
329
|
+
# ):
|
|
330
|
+
# current_trace = current_trace_context.get()
|
|
331
|
+
# if input:
|
|
332
|
+
# current_trace.input = input
|
|
333
|
+
# if output:
|
|
334
|
+
# current_trace.output = output
|
|
335
|
+
# if name:
|
|
336
|
+
# current_trace.name = name
|
|
337
|
+
# if tags:
|
|
338
|
+
# current_trace.tags = tags
|
|
339
|
+
# if metadata:
|
|
340
|
+
# current_trace.metadata = metadata
|
|
341
|
+
# if thread_id:
|
|
342
|
+
# current_trace.thread_id = thread_id
|
|
343
|
+
# if user_id:
|
|
344
|
+
# current_trace.user_id = user_id
|
|
345
|
+
# if metric_collection:
|
|
346
|
+
# current_trace.metric_collection = metric_collection
|
|
347
|
+
# if metrics:
|
|
348
|
+
# current_trace.metrics = metrics
|