Skip to content

Commit 4972a4e

Browse files
committed
Fix: Move voyageai.Client() creation inside try/except block
The voyageai.Client() constructor can raise AuthenticationError if the API key is empty or invalid. Previously, this was happening outside the try/except block that catches Voyage AI exceptions, causing the error to be caught by the generic Exception handler and returned as a 500 error instead of a 401. This fix moves the client creation inside the try block so that AuthenticationError from the constructor is properly caught and converted to a VoyageAuthError with a 401 status code.
1 parent 727f84f commit 4972a4e

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

  • mflix/server/python-fastapi/src/routers

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,9 @@ async def vector_search_movies(
332332
)
333333

334334
try:
335-
# Initialize the client here to avoid import-time errors
336-
vo = voyageai.Client()
337-
338335
# The vector search index was already created at startup time
339-
# Generate embedding for the search query
340-
query_embedding = get_embedding(q, input_type="query", client=vo)
336+
# Generate embedding for the search query (client is created inside get_embedding)
337+
query_embedding = get_embedding(q, input_type="query")
341338

342339
# Get the embedded movies collection
343340
embedded_movies_collection = get_collection("embedded_movies")
@@ -1373,10 +1370,10 @@ def get_embedding(data, input_type = "document", client=None):
13731370
VoyageAuthError: If the API key is invalid (401)
13741371
VoyageAPIError: For other API errors
13751372
"""
1376-
if client is None:
1377-
client = voyageai.Client()
1378-
13791373
try:
1374+
if client is None:
1375+
client = voyageai.Client()
1376+
13801377
embeddings = client.embed(
13811378
data, model = model, output_dimension = outputDimension, input_type = input_type
13821379
).embeddings

0 commit comments

Comments
 (0)