diff --git a/agenta-backend/agenta_backend/services/llm_apps_service.py b/agenta-backend/agenta_backend/services/llm_apps_service.py index e9c597e0a3..d67ca4321e 100644 --- a/agenta-backend/agenta_backend/services/llm_apps_service.py +++ b/agenta-backend/agenta_backend/services/llm_apps_service.py @@ -91,13 +91,53 @@ async def invoke_app( result=Result(type="text", value=app_output["message"], error=None) ) - except httpx.HTTPError as e: - logger.error(f"Error occurred during request: {e}") + except httpx.HTTPStatusError as e: + # Parse error details from the API response + error_message = "Error in invoking the LLM App:" + try: + error_body = e.response.json() + if "message" in error_body: + error_message = error_body["message"] + elif ( + "error" in error_body + ): # Some APIs return error information under an 'error' key + error_message = error_body["error"] + except ValueError: + # Fallback if the error response is not JSON or doesn't have the expected structure + logger.error(f"Failed to parse error response: {e}") + + logger.error(f"Error occurred during request: {error_message}") + return InvokationResult( + result=Result( + type="error", + error=Error( + message=error_message, + stacktrace=str(e), + ), + ) + ) + + except httpx.RequestError as e: + # Handle other request errors (e.g., network issues) + logger.error(f"Request error: {e}") + return InvokationResult( + result=Result( + type="error", + error=Error( + message="Network error while invoking the LLM App", + stacktrace=str(e), + ), + ) + ) + + except Exception as e: + # Catch-all for any other unexpected errors + logger.error(f"Unexpected error: {e}") return InvokationResult( result=Result( type="error", error=Error( - message="An error occurred while invoking the LLM App", + message="Unexpected error while invoking the LLM App", stacktrace=str(e), ), )