17
17
Iterator ,
18
18
List ,
19
19
Optional ,
20
+ Tuple ,
20
21
Union ,
21
22
)
22
23
from urllib .parse import urlencode , urlparse
@@ -75,19 +76,11 @@ def wait_for_completion(self) -> Self:
75
76
"""
76
77
77
78
while True :
78
- try :
79
- self .transcript = api .get_transcript (
80
- self ._client .http_client ,
81
- self .transcript_id ,
82
- )
83
- except Exception as exc :
84
- self .transcript = types .TranscriptResponse (
85
- ** self .transcript .dict (
86
- exclude_none = True , exclude = {"status" , "error" }
87
- ),
88
- status = types .TranscriptStatus .error ,
89
- error = str (exc ),
90
- )
79
+ # No try-except - if there is an HTTP error then surface it to user
80
+ self .transcript = api .get_transcript (
81
+ self ._client .http_client ,
82
+ self .transcript_id ,
83
+ )
91
84
92
85
if self .transcript .status in (
93
86
types .TranscriptStatus .completed ,
@@ -563,8 +556,9 @@ def add_transcript(self, transcript: Union[Transcript, str]) -> None:
563
556
564
557
return self
565
558
566
- def wait_for_completion (self ) -> None :
559
+ def wait_for_completion (self , return_failures ) -> Union [ None , List [ str ]] :
567
560
transcripts : List [Transcript ] = []
561
+ failures : List [str ] = []
568
562
569
563
future_transcripts : Dict [concurrent .futures .Future [Transcript ], str ] = {}
570
564
@@ -575,10 +569,16 @@ def wait_for_completion(self) -> None:
575
569
finished_futures , _ = concurrent .futures .wait (future_transcripts )
576
570
577
571
for future in finished_futures :
578
- transcripts .append (future .result ())
572
+ try :
573
+ transcripts .append (future .result ())
574
+ except types .TranscriptError as e :
575
+ failures .append (str (e ))
579
576
580
577
self .transcripts = transcripts
581
578
579
+ if return_failures :
580
+ return failures
581
+
582
582
583
583
class TranscriptGroup :
584
584
"""
@@ -669,19 +669,37 @@ def add_transcript(
669
669
670
670
return self
671
671
672
- def wait_for_completion (self ) -> Self :
672
+ def wait_for_completion (
673
+ self ,
674
+ return_failures : Optional [bool ] = False ,
675
+ ) -> Union [Self , Tuple [Self , List [str ]]]:
673
676
"""
674
677
Polls each transcript within the `TranscriptGroup`.
675
678
679
+ Note - if an HTTP error is encountered when waiting for a Transcript in the TranscriptGroup, it will be popped from the group and added to the list of failures.
680
+ You can return this list of failures with `return_failures=True`.
681
+
682
+ Args:
683
+ return_failures: Whether to return a list of errors for transcripts that failed due to HTTP errors.
676
684
"""
677
- self ._impl .wait_for_completion ()
685
+ if return_failures :
686
+ failures = self ._impl .wait_for_completion (return_failures = return_failures )
687
+ return self , failures
688
+
689
+ self ._impl .wait_for_completion (return_failures = return_failures )
678
690
679
691
return self
680
692
681
693
def wait_for_completion_async (
682
694
self ,
683
- ) -> concurrent .futures .Future [Self ]:
684
- return self ._executor .submit (self .wait_for_completion )
695
+ return_failures : Optional [bool ] = False ,
696
+ ) -> Union [
697
+ concurrent .futures .Future [Self ],
698
+ concurrent .futures .Future [Tuple [Self , List [str ]]],
699
+ ]:
700
+ return self ._executor .submit (
701
+ self .wait_for_completion , return_failures = return_failures
702
+ )
685
703
686
704
687
705
class _TranscriberImpl :
@@ -722,24 +740,14 @@ def transcribe_url(
722
740
audio_url = url ,
723
741
** config .raw .dict (exclude_none = True ),
724
742
)
725
- try :
726
- transcript = Transcript .from_response (
727
- client = self ._client ,
728
- response = api .create_transcript (
729
- client = self ._client .http_client ,
730
- request = transcript_request ,
731
- ),
732
- )
733
- except Exception as exc :
734
- return Transcript .from_response (
735
- client = self ._client ,
736
- response = types .TranscriptResponse (
737
- audio_url = url ,
738
- ** config .raw .dict (exclude_none = True ),
739
- status = types .TranscriptStatus .error ,
740
- error = str (exc ),
741
- ),
742
- )
743
+ # No try-except - if there is an HTTP error raise it to the user
744
+ transcript = Transcript .from_response (
745
+ client = self ._client ,
746
+ response = api .create_transcript (
747
+ client = self ._client .http_client ,
748
+ request = transcript_request ,
749
+ ),
750
+ )
743
751
744
752
if poll :
745
753
return transcript .wait_for_completion ()
@@ -790,7 +798,8 @@ def transcribe_group(
790
798
data : List [Union [str , BinaryIO ]],
791
799
config : Optional [types .TranscriptionConfig ],
792
800
poll : bool ,
793
- ) -> TranscriptGroup :
801
+ return_failures : Optional [bool ] = False ,
802
+ ) -> Union [TranscriptGroup , Tuple [TranscriptGroup , List [str ]]]:
794
803
if config is None :
795
804
config = self .config
796
805
@@ -812,14 +821,28 @@ def transcribe_group(
812
821
transcript_group = TranscriptGroup (
813
822
client = self ._client ,
814
823
)
824
+ failures = []
815
825
816
826
for future in finished_futures :
817
- transcript_group .add_transcript (future .result ())
827
+ try :
828
+ transcript_group .add_transcript (future .result ())
829
+ except types .TranscriptError as e :
830
+ failures .append (f"Error processing { future_transcripts [future ]} : { e } " )
818
831
819
- if poll :
820
- return transcript_group .wait_for_completion ()
832
+ if poll and return_failures :
833
+ transcript_group , completion_failures = (
834
+ transcript_group .wait_for_completion (return_failures = return_failures )
835
+ )
836
+ failures .extend (completion_failures )
837
+ elif poll :
838
+ transcript_group = transcript_group .wait_for_completion (
839
+ return_failures = return_failures
840
+ )
821
841
822
- return transcript_group
842
+ if return_failures :
843
+ return transcript_group , failures
844
+ else :
845
+ return transcript_group
823
846
824
847
def list_transcripts (
825
848
self ,
@@ -945,19 +968,22 @@ def submit_group(
945
968
self ,
946
969
data : List [Union [str , BinaryIO ]],
947
970
config : Optional [types .TranscriptionConfig ] = None ,
948
- ) -> TranscriptGroup :
971
+ return_failures : Optional [bool ] = False ,
972
+ ) -> Union [TranscriptGroup , Tuple [TranscriptGroup , List [str ]]]:
949
973
"""
950
974
Submits multiple transcription jobs without waiting for their completion.
951
975
952
976
Args:
953
977
data: A list of local paths, URLs, or binary objects (can be mixed).
954
978
config: Transcription options and features. If `None` is given, the Transcriber's
955
979
default configuration will be used.
980
+ return_failures: Whether to include a list of errors for transcriptions that failed due to HTTP errors
956
981
"""
957
982
return self ._impl .transcribe_group (
958
983
data = data ,
959
984
config = config ,
960
985
poll = False ,
986
+ return_failures = return_failures ,
961
987
)
962
988
963
989
def transcribe (
@@ -1005,41 +1031,50 @@ def transcribe_group(
1005
1031
self ,
1006
1032
data : List [Union [str , BinaryIO ]],
1007
1033
config : Optional [types .TranscriptionConfig ] = None ,
1008
- ) -> TranscriptGroup :
1034
+ return_failures : Optional [bool ] = False ,
1035
+ ) -> Union [TranscriptGroup , Tuple [TranscriptGroup , List [str ]]]:
1009
1036
"""
1010
1037
Transcribes a list of files (as local paths, URLs, or binary objects).
1011
1038
1012
1039
Args:
1013
1040
data: A list of local paths, URLs, or binary objects (can be mixed).
1014
1041
config: Transcription options and features. If `None` is given, the Transcriber's
1015
1042
default configuration will be used.
1043
+ return_failures: Whether to include a list of errors for transcriptions that failed due to HTTP errors
1016
1044
"""
1017
1045
1018
1046
return self ._impl .transcribe_group (
1019
1047
data = data ,
1020
1048
config = config ,
1021
1049
poll = True ,
1050
+ return_failures = return_failures ,
1022
1051
)
1023
1052
1024
1053
def transcribe_group_async (
1025
1054
self ,
1026
1055
data : List [Union [str , BinaryIO ]],
1027
1056
config : Optional [types .TranscriptionConfig ] = None ,
1028
- ) -> concurrent .futures .Future [TranscriptGroup ]:
1057
+ return_failures : Optional [bool ] = False ,
1058
+ ) -> Union [
1059
+ concurrent .futures .Future [TranscriptGroup ],
1060
+ concurrent .futures .Future [Tuple [TranscriptGroup , List [str ]]],
1061
+ ]:
1029
1062
"""
1030
1063
Transcribes a list of files (as local paths, URLs, or binary objects) asynchronously.
1031
1064
1032
1065
Args:
1033
1066
data: A list of local paths, URLs, or binary objects (can be mixed).
1034
1067
config: Transcription options and features. If `None` is given, the Transcriber's
1035
1068
default configuration will be used.
1069
+ return_failures: Whether to include a list of errors for transcriptions that failed due to HTTP errors
1036
1070
"""
1037
1071
1038
1072
return self ._executor .submit (
1039
1073
self ._impl .transcribe_group ,
1040
1074
data = data ,
1041
1075
config = config ,
1042
1076
poll = True ,
1077
+ return_failures = return_failures ,
1043
1078
)
1044
1079
1045
1080
def list_transcripts (
0 commit comments