Skip to content

Commit 727f84f

Browse files
committed
Fix Python Voyage AI error handling to use SDK exception types
- Remove accidentally added .python-version file - Update get_embedding() to catch specific voyageai.error exceptions: - AuthenticationError (401) -> VoyageAuthError - InvalidRequestError (400) -> VoyageAPIError - RateLimitError (429) -> VoyageAPIError - ServiceUnavailableError (503) -> VoyageAPIError - VoyageError (other) -> VoyageAPIError - Import VoyageAuthError and VoyageAPIError in tests for future use This fixes the issue where authentication errors were not being properly detected because the code was relying on string matching instead of catching the SDK's specific exception types.
1 parent 695161c commit 727f84f

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

mflix/server/python-fastapi/.python-version

Lines changed: 0 additions & 1 deletion
This file was deleted.

mflix/server/python-fastapi/src/routers/movies.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
from bson.errors import InvalidId
1212
import voyageai
13+
import voyageai.error as voyage_error
1314
import os
1415

1516

@@ -1380,17 +1381,22 @@ def get_embedding(data, input_type = "document", client=None):
13801381
data, model = model, output_dimension = outputDimension, input_type = input_type
13811382
).embeddings
13821383
return embeddings[0]
1384+
except voyage_error.AuthenticationError as e:
1385+
# Handle authentication errors (401) from Voyage AI SDK
1386+
raise VoyageAuthError("Invalid Voyage AI API key. Please check your VOYAGE_API_KEY in the .env file")
1387+
except voyage_error.InvalidRequestError as e:
1388+
# Handle invalid request errors (400) - often due to malformed API key
1389+
raise VoyageAPIError(f"Invalid request to Voyage AI API: {str(e)}", 400)
1390+
except voyage_error.RateLimitError as e:
1391+
# Handle rate limiting errors (429)
1392+
raise VoyageAPIError(f"Voyage AI API rate limit exceeded: {str(e)}", 429)
1393+
except voyage_error.ServiceUnavailableError as e:
1394+
# Handle service unavailable errors (502, 503, 504)
1395+
raise VoyageAPIError(f"Voyage AI service unavailable: {str(e)}", 503)
1396+
except voyage_error.VoyageError as e:
1397+
# Handle any other Voyage AI SDK errors
1398+
raise VoyageAPIError(f"Voyage AI API error: {str(e)}", getattr(e, 'http_status', 500) or 500)
13831399
except Exception as e:
1384-
error_message = str(e).lower()
1385-
1386-
# Check for authentication errors
1387-
if "401" in error_message or "unauthorized" in error_message or "invalid api key" in error_message:
1388-
raise VoyageAuthError("Invalid Voyage AI API key. Please check your VOYAGE_API_KEY in the .env file")
1389-
1390-
# Check for other API errors
1391-
if "api" in error_message or "voyage" in error_message:
1392-
raise VoyageAPIError(f"Voyage AI API error: {str(e)}", 503)
1393-
1394-
# Re-raise other exceptions
1400+
# Handle unexpected errors
13951401
raise VoyageAPIError(f"Failed to generate embedding: {str(e)}", 500)
13961402

mflix/server/python-fastapi/tests/test_movie_routes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from fastapi import HTTPException
1313

1414
from src.models.models import CreateMovieRequest, UpdateMovieRequest
15+
from src.utils.exceptions import VoyageAuthError, VoyageAPIError
1516

1617

1718
# Test constants

0 commit comments

Comments
 (0)