11use anyhow:: Result ;
22
3- use super :: types:: GoogleResponse ;
3+ use super :: types:: { GoogleResponse , Part } ;
44use crate :: internal:: llm_client:: {
55 primitive:: request:: RequestBuilder , traits:: WithClient , ErrorCode , LLMCompleteResponse ,
66 LLMCompleteResponseMetadata , LLMErrorResponse , LLMResponse ,
@@ -26,6 +26,7 @@ pub fn parse_google_response<C: WithClient + RequestBuilder>(
2626 instant_now : web_time:: Instant ,
2727 model_name : Option < String > ,
2828) -> LLMResponse {
29+ // baml_log::info!("Parsing Google response: {:#?}", response_body);
2930 let response = match GoogleResponse :: deserialize ( & response_body)
3031 . context ( format ! (
3132 "Failed to parse into a response accepted by {}: {}" ,
@@ -78,11 +79,11 @@ pub fn parse_google_response<C: WithClient + RequestBuilder>(
7879 } ;
7980
8081 let model_name = model_name. unwrap_or ( "<unknown>" . to_string ( ) ) ;
81- let part_index = content_part ( & model_name ) ;
82+ let text_content = text_content_part ( & content . parts ) ;
8283 LLMResponse :: Success ( LLMCompleteResponse {
8384 client : client. context ( ) . name . to_string ( ) ,
8485 prompt : to_prompt ( prompt) ,
85- content : content . parts [ part_index ] . text . clone ( ) ,
86+ content : text_content . unwrap_or_default ( ) ,
8687 start_time : system_now,
8788 latency : instant_now. elapsed ( ) ,
8889 request_options : client. request_options ( ) . clone ( ) ,
@@ -101,12 +102,11 @@ pub fn parse_google_response<C: WithClient + RequestBuilder>(
101102 } )
102103}
103104
104- fn content_part ( model_name : & str ) -> usize {
105- if model_name. contains ( "gemini-2.0-flash-thinking-exp-1219" ) {
106- 1
107- } else {
108- 0
109- }
105+ fn text_content_part ( parts : & Vec < Part > ) -> Option < String > {
106+ parts
107+ . iter ( )
108+ . position ( |part| !part. text . is_empty ( ) && part. thought . unwrap_or ( false ) == false )
109+ . map ( |index| parts[ index] . text . clone ( ) )
110110}
111111
112112pub fn scan_google_response_stream (
@@ -145,18 +145,13 @@ pub fn scan_google_response_stream(
145145 Err ( e) => return Err ( e) ,
146146 } ;
147147 if let Some ( choice) = event. candidates . get ( 0 ) {
148- let part_index = content_part (
149- model_name
150- . as_ref ( )
151- . map ( |s| s. as_str ( ) )
152- . unwrap_or ( "<unknown>" ) ,
153- ) ;
154- if let Some ( content) = choice
148+ let text_content = & choice
155149 . content
156150 . as_ref ( )
157- . and_then ( |c| c. parts . get ( part_index) )
158- {
159- inner. content += & content. text ;
151+ . and_then ( |c| text_content_part ( & c. parts ) ) ;
152+
153+ if let Some ( text_content) = text_content {
154+ inner. content += & text_content;
160155 }
161156 inner. metadata . finish_reason = choice. finish_reason . as_ref ( ) . map ( |r| r. to_string ( ) ) ;
162157 if choice
@@ -235,6 +230,7 @@ mod tests {
235230 function_call: None ,
236231 function_response: None ,
237232 video_metadata: None ,
233+ thought: None ,
238234 } ] ,
239235 role: Some ( "model" . to_string( ) ) ,
240236 } ) ,
0 commit comments