-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathrunner.py
More file actions
68 lines (54 loc) · 2.53 KB
/
runner.py
File metadata and controls
68 lines (54 loc) · 2.53 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
from functools import wraps
import sentry_sdk
from sentry_sdk.integrations import DidNotEnable
from ..spans import agent_workflow_span, end_invoke_agent_span
from ..utils import _capture_exception, _record_exception_on_span
try:
from agents.exceptions import AgentsException
except ImportError:
raise DidNotEnable("OpenAI Agents not installed")
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Any, Callable
def _create_run_wrapper(original_func):
# type: (Callable[..., Any]) -> Callable[..., Any]
"""
Wraps the agents.Runner.run methods to create a root span for the agent workflow runs.
Note agents.Runner.run_sync() is a wrapper around agents.Runner.run(),
so it does not need to be wrapped separately.
"""
@wraps(original_func)
async def wrapper(*args, **kwargs):
# type: (*Any, **Any) -> Any
# Isolate each workflow so that when agents are run in asyncio tasks they
# don't touch each other's scopes
with sentry_sdk.isolation_scope():
# Clone agent because agent invocation spans are attached per run.
agent = args[0].clone()
with agent_workflow_span(agent):
args = (agent, *args[1:])
try:
run_result = await original_func(*args, **kwargs)
except AgentsException as exc:
_capture_exception(exc)
context_wrapper = getattr(exc.run_data, "context_wrapper", None)
if context_wrapper is not None:
invoke_agent_span = getattr(
context_wrapper, "_sentry_agent_span", None
)
if (
invoke_agent_span is not None
and invoke_agent_span.timestamp is None
):
_record_exception_on_span(invoke_agent_span, exc)
end_invoke_agent_span(context_wrapper, agent)
raise exc from None
except Exception as exc:
# Invoke agent span is not finished in this case.
# This is much less likely to occur than other cases because
# AgentRunner.run() is "just" a while loop around _run_single_turn.
_capture_exception(exc)
raise exc from None
end_invoke_agent_span(run_result.context_wrapper, agent)
return run_result
return wrapper