Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions backend/src/baserow/core/generative_ai/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ def __init__(self, model_name, *args, **kwargs):

class GenerativeAIPromptError(Exception):
"""Raised when an error occurs while prompting the model."""


def get_user_friendly_error_message(exc: Exception) -> str:
"""
Extract a concise, user-facing message from a provider SDK exception.

Provider SDKs (OpenAI, Anthropic, Mistral) include metadata like
status_code and model_name in their ``__str__`` output. Users only
care about the human-readable body/message part.

:param exc: The exception raised by the provider SDK.
:return: A concise error message suitable for displaying to users.
"""

# OpenAI / Anthropic APIStatusError exposes a `.body` dict or string
# with the actual error message from the provider.
body = getattr(exc, "body", None)
if isinstance(body, dict):
msg = body.get("message") or body.get("error", {}).get("message")
if msg:
return str(msg)
if isinstance(body, str) and body:
return body

# Fallback: use the full string representation.
return str(exc)
Loading
Loading