@@ -442,20 +442,18 @@ def mk_tr_details(tr, tc, mx=2000):
442442# %% ../nbs/00_core.ipynb
443443class AsyncStreamFormatter :
444444 def __init__ (self , include_usage = False , mx = 2000 ):
445- self .outp ,self .tcs ,self .include_usage ,self .think , self . mx = '' ,{},include_usage , False ,mx
445+ self .outp ,self .tcs ,self .include_usage ,self .mx = '' ,{},include_usage ,mx
446446
447447 def format_item (self , o ):
448448 "Format a single item from the response stream."
449449 res = ''
450450 if isinstance (o , ModelResponseStream ):
451451 d = o .choices [0 ].delta
452- if nested_idx (d , 'reasoning_content' ) and d ['reasoning_content' ]!= '{"text": ""}' :
453- self .think = True
454- res += '🧠' if not self .outp or self .outp [- 1 ]== '🧠' else '\n \n 🧠'
455- elif self .think :
456- self .think = False
457- res += '\n \n '
458- if c := d .content : res += c
452+ if nested_idx (d , 'reasoning_content' ) and d ['reasoning_content' ]!= '{"text": ""}' :
453+ res += '🧠' if not self .outp or self .outp [- 1 ]== '🧠' else '\n \n 🧠' # gemini can interleave reasoning
454+ elif self .outp and self .outp [- 1 ] == '🧠' : res += '\n \n '
455+ if c := d .content : # gemini has text content in last reasoning chunk
456+ res += f"\n \n { c } " if res and res [- 1 ] == '🧠' else c
459457 elif isinstance (o , ModelResponse ):
460458 if self .include_usage : res += f"\n Usage: { o .usage } "
461459 if c := getattr (contents (o ),'tool_calls' ,None ):
0 commit comments