@@ -245,11 +245,9 @@ def _iter():
245245 ),
246246 )
247247 except json .JSONDecodeError as e :
248- # Log the problematic data for debugging
249248 print (f"JSON decode error: { e } , data: { repr (_sse .data )} " )
250249 continue
251250 except Exception as e :
252- # Log other parsing errors
253251 print (f"Parsing error: { e } , event: { _sse .event } , data: { repr (_sse .data )} " )
254252 continue
255253
@@ -1335,20 +1333,33 @@ async def _stream() -> AsyncHttpResponse[typing.AsyncIterator[V2ChatStreamRespon
13351333 if 200 <= _response .status_code < 300 :
13361334
13371335 async def _iter ():
1338- _event_source = httpx_sse . EventSource (_response )
1336+ _event_source = EventSource (_response )
13391337 async for _sse in _event_source .aiter_sse ():
1340- if _sse .data == None :
1341- return
13421338 try :
1339+ # Skip empty events
1340+ if not _sse .data or _sse .data .strip () == "" :
1341+ continue
1342+
1343+ # Handle [DONE] token from OpenAI-style APIs
1344+ if _sse .data .strip () == '[DONE]' :
1345+ continue
1346+
1347+ parsed_data = json .loads (_sse .data )
1348+
13431349 yield typing .cast (
13441350 V2ChatStreamResponse ,
13451351 construct_type (
13461352 type_ = V2ChatStreamResponse , # type: ignore
1347- object_ = json . loads ( _sse . data ) ,
1353+ object_ = parsed_data ,
13481354 ),
13491355 )
1350- except Exception :
1351- pass
1356+ except json .JSONDecodeError as e :
1357+ print (f"JSON decode error: { e } , data: { repr (_sse .data )} " )
1358+ continue
1359+ except Exception as e :
1360+ print (f"Parsing error: { e } , event: { _sse .event } , data: { repr (_sse .data )} " )
1361+ continue
1362+
13521363 return
13531364
13541365 return AsyncHttpResponse (response = _response , data = _iter ())
0 commit comments