@@ -54,6 +54,7 @@ def __init__(self,
5454 stream : bool = True ,
5555 stream_chunk_size : int = 1 ,
5656 timeout : int = 3600 ,
57+ finish_reason_confirm : bool = True ,
5758 max_workers : Optional [int ] = None ):
5859
5960 super ().__init__ (
@@ -84,6 +85,7 @@ def __init__(self,
8485 self .stream_chunk_size = stream_chunk_size
8586 self .openai_extra_kwargs = openai_extra_kwargs
8687 self .timeout = timeout
88+ self .finish_reason_confirm = finish_reason_confirm
8789
8890 def _create_fresh_client (self ):
8991 """Create a fresh OpenAI client for each request to avoid
@@ -336,17 +338,18 @@ def log_with_thread(message, level='info'):
336338 # Combine reasoning content and regular content
337339 complete_content = '' .join (completion_chunks )
338340
339- if finish_reason is None :
340- elapsed = time .time () - start_time
341- log_with_thread (
342- f'Stream ended without finish_reason (possible truncation). '
343- f'elapsed={ elapsed :.1f} s chunks={ chunk_count } '
344- f'content_len={ sum (len (x ) for x in completion_chunks )} '
345- f'reasoning_len={ len (reasoning_content )} ' ,
346- 'error' ,
347- )
348- raise RuntimeError (
349- 'Streaming ended without finish_reason (truncated).' )
341+ if self .finish_reason_confirm :
342+ if finish_reason is None :
343+ elapsed = time .time () - start_time
344+ log_with_thread (
345+ f'Stream ended without finish_reason (truncated). '
346+ f'elapsed={ elapsed :.1f} s chunks={ chunk_count } '
347+ f'content_len={ sum (len (x ) for x in completion_chunks )} '
348+ f'reasoning_len={ len (reasoning_content )} ' ,
349+ 'error' ,
350+ )
351+ raise RuntimeError (
352+ 'Streaming ended without finish_reason (truncated).' )
350353
351354 if self .verbose :
352355 log_with_thread (f'Stream processing complete. Content length: '
0 commit comments