fix: preserve chunk order when assembling multi-chunk transcriptions (#27417)
When an audio file is split into multiple chunks for transcription, transcribe() collected the per-chunk results with asyncio.as_completed(), which yields results in completion order rather than submission order. Whenever a later chunk finished transcribing before an earlier one, the assembled transcript was scrambled, for example the second half of a recording appearing before the first, and the stored file content plus everything downstream (file preview, full-context retrieval) read out of chronological order. This change awaits the chunk tasks with asyncio.gather() instead, which runs them just as concurrently but returns the results in the order the tasks were created, i.e. chunk_paths order. The existing error handling and chunk cleanup are unchanged: an HTTPException from a chunk is re-raised as is and any other error is wrapped in a 500. Fixes #27143
This commit is contained in:
1 parent
30be10f968
commit
99da2324e3
1 file changed
+9
-11
@@ -1091,19 +1091,17 @@ async def transcribe(request: Request, file_path: str, metadata: Optional[dict]
|
|||||||
detail=ERROR_MESSAGES.DEFAULT(e, 'Error processing audio file'),
|
detail=ERROR_MESSAGES.DEFAULT(e, 'Error processing audio file'),
|
||||||
)
|
)
|
||||||
|
|
||||||
results = []
|
|
||||||
try:
|
try:
|
||||||
tasks = [transcription_handler(request, chunk_path, metadata, user) for chunk_path in chunk_paths]
|
tasks = [transcription_handler(request, chunk_path, metadata, user) for chunk_path in chunk_paths]
|
||||||
for coro in asyncio.as_completed(tasks):
|
# gather keeps results in chunk order, unlike as_completed
|
||||||
try:
|
results = await asyncio.gather(*tasks)
|
||||||
results.append(await coro)
|
except HTTPException:
|
||||||
except HTTPException:
|
raise
|
||||||
raise
|
except Exception as transcribe_exc:
|
||||||
except Exception as transcribe_exc:
|
raise HTTPException(
|
||||||
raise HTTPException(
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
detail=f'Error transcribing chunk: {transcribe_exc}',
|
||||||
detail=f'Error transcribing chunk: {transcribe_exc}',
|
)
|
||||||
)
|
|
||||||
finally:
|
finally:
|
||||||
# Clean up only the temporary chunks, never the original file
|
# Clean up only the temporary chunks, never the original file
|
||||||
for chunk_path in chunk_paths:
|
for chunk_path in chunk_paths:
|
||||||
|
|||||||
Reference in new issue
Block a user