Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable adding new exception types to RetryChatModel #417

Open
jackmpcollins opened this issue Feb 2, 2025 · 0 comments
Open

Enable adding new exception types to RetryChatModel #417

jackmpcollins opened this issue Feb 2, 2025 · 0 comments

Comments

@jackmpcollins
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant