|
85 | 85 | list[_SingleAfterModelCallback], |
86 | 86 | ] |
87 | 87 |
|
| 88 | +_SingleOnModelErrorCallback: TypeAlias = Callable[ |
| 89 | + [CallbackContext, LlmRequest, Exception], |
| 90 | + Union[Awaitable[Optional[LlmResponse]], Optional[LlmResponse]], |
| 91 | +] |
| 92 | + |
| 93 | +OnModelErrorCallback: TypeAlias = Union[ |
| 94 | + _SingleOnModelErrorCallback, |
| 95 | + list[_SingleOnModelErrorCallback], |
| 96 | +] |
| 97 | + |
88 | 98 | _SingleBeforeToolCallback: TypeAlias = Callable[ |
89 | 99 | [BaseTool, dict[str, Any], ToolContext], |
90 | 100 | Union[Awaitable[Optional[dict]], Optional[dict]], |
@@ -364,6 +374,21 @@ class LlmAgent(BaseAgent): |
364 | 374 | The content to return to the user. When present, the actual model response |
365 | 375 | will be ignored and the provided content will be returned to user. |
366 | 376 | """ |
| 377 | + on_model_error_callback: Optional[OnModelErrorCallback] = None |
| 378 | + """Callback or list of callbacks to be called when a model call encounters an error. |
| 379 | +
|
| 380 | + When a list of callbacks is provided, the callbacks will be called in the |
| 381 | + order they are listed until a callback does not return None. |
| 382 | +
|
| 383 | + Args: |
| 384 | + callback_context: CallbackContext, |
| 385 | + llm_request: LlmRequest, The raw model request. |
| 386 | + error: The error from the model call. |
| 387 | +
|
| 388 | + Returns: |
| 389 | + The content to return to the user. When present, the error will be |
| 390 | + ignored and the provided content will be returned to user. |
| 391 | + """ |
367 | 392 | before_tool_callback: Optional[BeforeToolCallback] = None |
368 | 393 | """Callback or list of callbacks to be called before calling the tool. |
369 | 394 |
|
@@ -587,6 +612,20 @@ def canonical_after_model_callbacks(self) -> list[_SingleAfterModelCallback]: |
587 | 612 | return self.after_model_callback |
588 | 613 | return [self.after_model_callback] |
589 | 614 |
|
| 615 | + @property |
| 616 | + def canonical_on_model_error_callbacks( |
| 617 | + self, |
| 618 | + ) -> list[_SingleOnModelErrorCallback]: |
| 619 | + """The resolved self.on_model_error_callback field as a list of _SingleOnModelErrorCallback. |
| 620 | +
|
| 621 | + This method is only for use by Agent Development Kit. |
| 622 | + """ |
| 623 | + if not self.on_model_error_callback: |
| 624 | + return [] |
| 625 | + if isinstance(self.on_model_error_callback, list): |
| 626 | + return self.on_model_error_callback |
| 627 | + return [self.on_model_error_callback] |
| 628 | + |
590 | 629 | @property |
591 | 630 | def canonical_before_tool_callbacks( |
592 | 631 | self, |
|
0 commit comments