@@ -489,19 +489,20 @@ impl LlmClient {
489489 let tool_calls = dedup_tool_calls ( tool_calls) ;
490490 let mut content_text = strip_tool_call_blocks ( & full_text) ;
491491
492- // If we found tool calls, suppress any remaining JSON fragments
493- // in content_text. Gemini sometimes outputs tool call JSON without
494- // the ``` wrapper, leaving partial JSON as "content".
492+ // If we found tool calls, suppress any JSON/code artifacts in content.
493+ // Gemini often leaks partial tool call JSON or closing ``` into the
494+ // content when it outputs a tool call. Only keep content that looks
495+ // like actual natural language prose.
495496 if !tool_calls. is_empty ( ) && !content_text. is_empty ( ) {
496- // Strip if content is just JSON fragments (starts with { or ")
497497 let trimmed = content_text. trim ( ) ;
498- if trimmed. starts_with ( '{' )
499- || trimmed. starts_with ( '"' )
500- || trimmed. ends_with ( "}}" )
501- || trimmed. ends_with ( "}" )
498+ let looks_like_json = trimmed. contains ( "}}" )
502499 || trimmed. contains ( "\" name\" :" )
503500 || trimmed. contains ( "\" arguments\" :" )
504- {
501+ || trimmed. starts_with ( '{' )
502+ || trimmed. starts_with ( '"' )
503+ || trimmed. starts_with ( "```" )
504+ || trimmed. ends_with ( "```" ) ;
505+ if looks_like_json {
505506 content_text. clear ( ) ;
506507 }
507508 }
0 commit comments