@@ -147,8 +147,11 @@ impl LlmGenerationClient for AiStudioClient {
147147 } ) ;
148148 }
149149
150+ let mut need_json = false ;
151+
150152 // If structured output is requested, add schema and responseMimeType
151153 if let Some ( OutputFormat :: JsonSchema { schema, .. } ) = & request. output_format {
154+ need_json = true ;
152155 let mut schema_json = serde_json:: to_value ( schema) ?;
153156 remove_additional_properties ( & mut schema_json) ;
154157 payload[ "generationConfig" ] = serde_json:: json!( {
@@ -161,18 +164,24 @@ impl LlmGenerationClient for AiStudioClient {
161164 let resp = http:: request ( || self . client . post ( & url) . json ( & payload) )
162165 . await
163166 . context ( "Gemini API error" ) ?;
164- let resp_json: Value = resp. json ( ) . await . context ( "Invalid JSON" ) ?;
167+ let mut resp_json: Value = resp. json ( ) . await . context ( "Invalid JSON" ) ?;
165168
166169 if let Some ( error) = resp_json. get ( "error" ) {
167170 bail ! ( "Gemini API error: {:?}" , error) ;
168171 }
169- let mut resp_json = resp_json;
172+
173+ if need_json {
174+ return Ok ( super :: LlmGenerateResponse :: Json ( serde_json:: json!(
175+ resp_json[ "candidates" ] [ 0 ]
176+ ) ) ) ;
177+ }
178+
170179 let text = match & mut resp_json[ "candidates" ] [ 0 ] [ "content" ] [ "parts" ] [ 0 ] [ "text" ] {
171180 Value :: String ( s) => std:: mem:: take ( s) ,
172181 _ => bail ! ( "No text in response" ) ,
173182 } ;
174183
175- Ok ( LlmGenerateResponse { text } )
184+ Ok ( LlmGenerateResponse :: Text ( text) )
176185 }
177186
178187 fn json_schema_options ( & self ) -> ToJsonSchemaOptions {
@@ -333,9 +342,12 @@ impl LlmGenerationClient for VertexAiClient {
333342 . set_parts ( vec ! [ Part :: new( ) . set_text( sys. to_string( ) ) ] )
334343 } ) ;
335344
345+ let mut need_json = false ;
346+
336347 // Compose generation config
337348 let mut generation_config = None ;
338349 if let Some ( OutputFormat :: JsonSchema { schema, .. } ) = & request. output_format {
350+ need_json = true ;
339351 let schema_json = serde_json:: to_value ( schema) ?;
340352 generation_config = Some (
341353 GenerationConfig :: new ( )
@@ -359,6 +371,18 @@ impl LlmGenerationClient for VertexAiClient {
359371
360372 // Call the API
361373 let resp = req. send ( ) . await ?;
374+
375+ if need_json {
376+ match resp. candidates . into_iter ( ) . next ( ) {
377+ Some ( resp_json) => {
378+ return Ok ( super :: LlmGenerateResponse :: Json ( serde_json:: json!(
379+ resp_json
380+ ) ) ) ;
381+ }
382+ None => bail ! ( "No response" ) ,
383+ }
384+ }
385+
362386 // Extract text from response
363387 let Some ( Data :: Text ( text) ) = resp
364388 . candidates
@@ -370,7 +394,7 @@ impl LlmGenerationClient for VertexAiClient {
370394 else {
371395 bail ! ( "No text in response" ) ;
372396 } ;
373- Ok ( super :: LlmGenerateResponse { text } )
397+ Ok ( super :: LlmGenerateResponse :: Text ( text) )
374398 }
375399
376400 fn json_schema_options ( & self ) -> ToJsonSchemaOptions {
0 commit comments