@@ -428,20 +428,18 @@ def mk_tr_details(tr, tc, mx=2000):
428428# %% ../nbs/00_core.ipynb
429429class AsyncStreamFormatter :
430430 def __init__ (self , include_usage = False , mx = 2000 ):
431- self .outp ,self .tcs ,self .include_usage ,self .think , self . mx = '' ,{},include_usage , False ,mx
431+ self .outp ,self .tcs ,self .include_usage ,self .mx = '' ,{},include_usage ,mx
432432
433433 def format_item (self , o ):
434434 "Format a single item from the response stream."
435435 res = ''
436436 if isinstance (o , ModelResponseStream ):
437437 d = o .choices [0 ].delta
438- if nested_idx (d , 'reasoning_content' ) and d ['reasoning_content' ]!= '{"text": ""}' :
439- self .think = True
440- res += '🧠' if not self .outp or self .outp [- 1 ]== '🧠' else '\n \n 🧠'
441- elif self .think :
442- self .think = False
443- res += '\n \n '
444- if c := d .content : res += c
438+ if nested_idx (d , 'reasoning_content' ) and d ['reasoning_content' ]!= '{"text": ""}' :
439+ res += '🧠' if not self .outp or self .outp [- 1 ]== '🧠' else '\n \n 🧠' # gemini can interleave reasoning
440+ elif self .outp and self .outp [- 1 ] == '🧠' : res += '\n \n '
441+ if c := d .content : # gemini has text content in last reasoning chunk
442+ res += f"\n \n { c } " if res and res [- 1 ] == '🧠' else c
445443 elif isinstance (o , ModelResponse ):
446444 if self .include_usage : res += f"\n Usage: { o .usage } "
447445 if c := getattr (contents (o ),'tool_calls' ,None ):
0 commit comments