@@ -561,7 +561,7 @@ async def create_file(
561561 "/vector-stores/{vector_store_id}/files" , responses = vector_store_file_responses
562562)
563563@authorize (Action .MANAGE_VECTOR_STORES )
564- async def add_file_to_vector_store (
564+ async def add_file_to_vector_store ( # pylint: disable=too-many-locals
565565 request : Request ,
566566 vector_store_id : str ,
567567 auth : Annotated [AuthTuple , Depends (get_auth_dependency ())],
@@ -596,8 +596,9 @@ async def add_file_to_vector_store(
596596
597597 # Retry logic for database lock errors
598598 max_retries = 3
599- retry_delay = 0.5 # seconds
599+ retry_delay = 0.3 # seconds
600600 vs_file = None
601+ last_lock_error : Exception | None = None
601602
602603 for attempt in range (max_retries ):
603604 try :
@@ -613,25 +614,30 @@ async def add_file_to_vector_store(
613614 )
614615 is_last_attempt = attempt == max_retries - 1
615616
616- if is_lock_error and not is_last_attempt :
617- logger .warning (
618- "Database locked while adding file to vector store, "
619- "retrying in %s seconds (attempt %d/%d)" ,
620- retry_delay ,
621- attempt + 1 ,
622- max_retries ,
623- )
624- await asyncio .sleep (retry_delay )
625- retry_delay *= 2 # Exponential backoff
626- else :
627- raise # Re-raise if not a lock error or max retries reached
628- if not vs_file :
629- # Use standard error response model for consistency
630- response = InternalServerErrorResponse (
631- response = "Failed to create vector store file" ,
632- cause = "All retry attempts failed to create the vector store file" ,
633- )
634- raise HTTPException (** response .model_dump ())
617+ if is_lock_error :
618+ last_lock_error = retry_error
619+ if not is_last_attempt :
620+ logger .warning (
621+ "Database locked while adding file to vector store, "
622+ "retrying in %s seconds (attempt %d/%d)" ,
623+ retry_delay ,
624+ attempt + 1 ,
625+ max_retries ,
626+ )
627+ await asyncio .sleep (retry_delay )
628+ retry_delay *= 2 # Exponential backoff
629+ continue
630+ break
631+ raise # Re-raise if not a lock error
632+ if vs_file is None :
633+ if last_lock_error is not None :
634+ # Use standard error response model for consistency
635+ response = InternalServerErrorResponse (
636+ response = "Failed to create vector store file" ,
637+ cause = "All retry attempts failed to create the vector store file" ,
638+ )
639+ raise HTTPException (** response .model_dump ()) from last_lock_error
640+ raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR )
635641 logger .info (
636642 "Vector store file created - ID: %s, status: %s, last_error: %s" ,
637643 vs_file .id ,
0 commit comments