magentic icon indicating copy to clipboard operation
magentic copied to clipboard

Enable adding new exception types to RetryChatModel

Open jackmpcollins opened this issue 1 year ago • 1 comments

Allow registering exception handlers on an instance of RetryChatModel to customize how different errors are handled. These should get the error (and also the current Chat?) as input and return a list of messages to append (or a new Chat?).

Maybe implement this by adding error handling to Chat and using Chat within RetryModel.

As part of this, export all relevant magentic exceptions from magentic.exceptions for easy access. And document the exceptions.

API could be something like:

from magentic import OpenaiChatModel
from magentic.chat_model.retry_chat_model import RetryChatModel
# TODO: make exceptions importable from single location
from magentic.exceptions import UnknownToolError

retry_model = RetryChatModel(OpenaiChatModel("gpt-4o-mini"), max_retries=3)
# OR retry_model = RetryChatModel("openai:gpt-4o-mini", max_retries=3)


@retry_model.register_handler(UnknownToolError)
def _(self, error: UnknownToolError) -> Sequence[Message[Any]]:
    return [
        error.output_message,
        ToolResultMessage(
            content=str(error.validation_error),
            tool_call_id=error.tool_call_id,
        ),
    ]

Similar to FastAPI's exception handling: https://fastapi.tiangolo.com/tutorial/handling-errors/#add-custom-headers

jackmpcollins avatar Feb 02 '25 03:02 jackmpcollins

Once #424 surfaces ToolValidationError/ToolStreamError, RetryChatModel.register_handler should let users plug those in, so the same retry flow handles user-function failures as well as completions/parse errors ?

jackmpcollins avatar Apr 27 '25 01:04 jackmpcollins