-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathrun_error_handlers.py
More file actions
113 lines (80 loc) · 3.24 KB
/
run_error_handlers.py
File metadata and controls
113 lines (80 loc) · 3.24 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
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, Generic
from typing_extensions import TypedDict
from .agent import Agent
from .exceptions import MaxTurnsExceeded
from .items import ModelResponse, RunItem, TResponseInputItem
from .run_context import RunContextWrapper, TContext
from .util._types import MaybeAwaitable
@dataclass
class RunErrorData:
"""Snapshot of run data passed to error handlers."""
input: str | list[TResponseInputItem]
new_items: list[RunItem]
history: list[TResponseInputItem]
output: list[TResponseInputItem]
raw_responses: list[ModelResponse]
last_agent: Agent[Any]
@dataclass
class RunErrorHandlerInput(Generic[TContext]):
error: MaxTurnsExceeded
context: RunContextWrapper[TContext]
run_data: RunErrorData
@dataclass
class RunErrorHandlerResult:
"""Result returned by an error handler."""
final_output: Any
include_in_history: bool = True
# Handlers may return RunErrorHandlerResult, a dict with final_output, or a raw final output value.
RunErrorHandler = Callable[
[RunErrorHandlerInput[TContext]],
MaybeAwaitable[RunErrorHandlerResult | dict[str, Any] | Any | None],
]
@dataclass
class ToolNotFoundErrorHandlerInput(Generic[TContext]):
"""Input passed to the ``tool_not_found`` error handler.
The handler is invoked when the model calls a tool that is not registered on the current
agent. Returning :class:`ToolNotFoundAction` tells the runner to inject a synthetic tool
output with ``error_message`` so the model can self-correct on the next turn. Returning
``None`` re-raises the original :class:`ModelBehaviorError`.
"""
tool_name: str
"""Name of the tool the model tried to call."""
available_tools: list[str]
"""Names of tools actually registered on the agent (function + custom + handoffs)."""
agent: Agent[Any]
"""The agent that received the bogus tool call."""
context: RunContextWrapper[TContext]
"""The run context wrapper."""
run_data: RunErrorData
"""Snapshot of run data at the moment the error occurred."""
@dataclass
class ToolNotFoundAction:
"""Instructs the runner to recover from a tool-not-found error.
The runner appends a synthetic ``function_call_output`` item containing ``error_message`` to
the conversation, then continues the turn. The model will see the error on its next step and
can retry with a valid tool name.
Note: recovery is bounded by the run's ``max_turns`` setting. A model that repeatedly
hallucinates tool calls will eventually hit that limit and raise ``MaxTurnsExceeded``.
"""
error_message: str
ToolNotFoundErrorHandler = Callable[
[ToolNotFoundErrorHandlerInput[TContext]],
MaybeAwaitable["ToolNotFoundAction | None"],
]
class RunErrorHandlers(TypedDict, Generic[TContext], total=False):
"""Error handlers keyed by error kind."""
max_turns: RunErrorHandler[TContext]
tool_not_found: ToolNotFoundErrorHandler[TContext]
__all__ = [
"RunErrorData",
"RunErrorHandler",
"RunErrorHandlerInput",
"RunErrorHandlerResult",
"RunErrorHandlers",
"ToolNotFoundAction",
"ToolNotFoundErrorHandler",
"ToolNotFoundErrorHandlerInput",
]