api_gemini 0.5.0

Gemini's API for accessing large language models (LLMs).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Audio Processing Integration Tests for Gemini API Client
//!
//! These tests verify audio processing capabilities including:
//! - Audio content transcription and analysis
//! - Multiple audio format support (MP3, WAV, OGG, FLAC, M4A)
//! - Audio content generation from text descriptions
//! - Audio content safety filtering and validation
//! - Large audio file handling and streaming
//! - Audio metadata extraction and processing
//! - Error handling for unsupported formats and corrupted audio
//!
//! All tests use feature gating and validate actual API responses.


use api_gemini::
{
  client ::Client,
  models ::*,
  error ::Error,
};

/// Test basic audio transcription with MP3 format
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_audio_transcription_mp3()
{
  let client = Client::new()
  .expect( "Failed to create client for audio transcription test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Create a simple audio content request with MP3 data
  // Using a minimal MP3 header for testing (this would be real audio data in practice)
  let audio_data = create_test_mp3_data();
  
  let request = GenerateContentRequest 
  {
    contents: vec![ Content 
    {
      parts: vec![
      Part
      {
        text: Some( "Please transcribe this audio and describe what you hear.".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      },
      Part
      {
        text: None,
        inline_data: Some( Blob
        {
          mime_type: "audio/mp3".to_string(),
          data: audio_data,
        } ),
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    } ],
    tools: None,
    tool_config: None,
    system_instruction: None,
    safety_settings: None,
    generation_config: None,
    cached_content: None,
  };
  
  let result = model.generate_content( &request ).await;
  
  match result 
  {
    Ok( response ) => 
    {
      // Verify we got a response with candidates
      assert!( !response.candidates.is_empty() );
      
      let content = &response.candidates[ 0 ].content;
      assert!( !content.parts.is_empty() );
      
      // Verify the response contains text (transcription or description)
      if let Some( text ) = &content.parts[ 0 ].text
      {
        assert!( !text.is_empty() );
      println!( "Audio transcription result : {text}" );
      }
    },
    Err( e ) => 
    {
      // Audio processing might not be supported yet
    println!( "Audio transcription test failed (expected): {e}" );
      // For now, we expect this to fail until audio support is confirmed
      match e
      {
        Error::InvalidArgument( _ ) | Error::ApiError( _ ) => {
          // These are acceptable failures for unsupported features
        },
      _ => panic!( "Unexpected error type : {e}" ),
      }
    }
  }
}

/// Test audio content analysis with WAV format
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_audio_analysis_wav()
{
  let client = Client::new()
  .expect( "Failed to create client for audio analysis test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Create a WAV audio content request
  let audio_data = create_test_wav_data();
  
  let request = GenerateContentRequest 
  {
    contents: vec![ Content 
    {
      parts: vec![
      Part 
      {
        text: Some( "Analyze the emotional tone and content of this audio recording.".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      },
      Part 
      {
        text: None,
        inline_data: Some( Blob 
        {
          mime_type: "audio/wav".to_string(),
          data: audio_data,
        } ),
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    } ],
    tools: None,
    tool_config: None,
    system_instruction: None,
    safety_settings: None,
    generation_config: None,
    cached_content: None,
  };
  
  let result = model.generate_content( &request ).await;
  
  // Similar to MP3 test - expect potential failure until audio support is confirmed
  match result 
  {
    Ok( response ) => 
    {
      assert!( !response.candidates.is_empty() );
      let content = &response.candidates[ 0 ].content;
      assert!( !content.parts.is_empty() );
      
      if let Some( text ) = &content.parts[ 0 ].text
      {
        assert!( !text.is_empty() );
      println!( "Audio analysis result : {text}" );
      }
    },
    Err( e ) => 
    {
    println!( "Audio analysis test failed (expected): {e}" );
      match e
      {
        Error::InvalidArgument( _ ) | Error::ApiError( _ ) => {
          // Acceptable failures for unsupported features
        },
      _ => panic!( "Unexpected error type : {e}" ),
      }
    }
  }
}

/// Test multiple audio format support
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_multiple_audio_formats()
{
  let client = Client::new()
  .expect( "Failed to create client for multi-format audio test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  let audio_formats = vec![
  ( "audio/mp3", create_test_mp3_data() ),
  ( "audio/wav", create_test_wav_data() ),
  ( "audio/ogg", create_test_ogg_data() ),
  ( "audio/flac", create_test_flac_data() ),
  ( "audio/m4a", create_test_m4a_data() ),
  ];
  
  for ( mime_type, audio_data ) in audio_formats 
  {
    let request = GenerateContentRequest 
    {
      contents: vec![ Content 
      {
        parts: vec![
        Part 
        {
        text : Some( format!( "Process this {mime_type} audio file." ) ),
          inline_data: None,
          function_call: None,
          function_response: None,
          ..Default::default()
        },
        Part 
        {
          text: None,
          inline_data: Some( Blob 
          {
            mime_type: mime_type.to_string(),
            data: audio_data,
          } ),
          function_call: None,
          function_response: None,
          ..Default::default()
        }
        ],
        role: "user".to_string(),
      } ],
      tools: None,
      tool_config: None,
      system_instruction: None,
      safety_settings: None,
      generation_config: None,
      cached_content: None,
    };
  
    let result = model.generate_content( &request ).await;
  
    match result 
    {
      Ok( response ) => 
      {
        assert!( !response.candidates.is_empty() );
      println!( "Successfully processed {mime_type} format" );
      },
      Err( e ) => 
      {
    println!( "Format {mime_type} failed (expected): {e}" );
        // Document which formats are not yet supported
        match e
        {
          Error::InvalidArgument( _ ) | Error::ApiError( _ ) => {
            // Expected for unsupported formats
          },
      _ => panic!( "Unexpected error for format {mime_type}: {e}" ),
        }
      }
    }
  }
}

/// Test audio content safety filtering
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_audio_safety_filtering()
{
  let client = Client::new()
  .expect( "Failed to create client for audio safety test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Test audio content with safety settings
  let request = GenerateContentRequest 
  {
    contents: vec![ Content 
    {
      parts: vec![
      Part 
      {
        text: Some( "Analyze this audio for content safety.".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      },
      Part 
      {
        text: None,
        inline_data: Some( Blob 
        {
          mime_type: "audio/wav".to_string(),
          data: create_test_wav_data(),
        } ),
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    } ],
    tools: None,
    safety_settings: Some( vec![ SafetySetting 
    {
      category: "HARM_CATEGORY_HARASSMENT".to_string(),
      threshold: "BLOCK_MEDIUM_AND_ABOVE".to_string(),
    } ] ),
    tool_config: None,
    system_instruction: None,
    generation_config: None,
    cached_content: None,
  };
  
  let result = model.generate_content( &request ).await;
  
  match result 
  {
    Ok( response ) => 
    {
      // Verify response structure
      assert!( !response.candidates.is_empty() );
      
      // Check if safety ratings are present
      if let Some( safety_ratings ) = &response.candidates[ 0 ].safety_ratings
      {
        assert!( !safety_ratings.is_empty() );
      println!( "Audio safety analysis completed with {} ratings", safety_ratings.len() );
      }
    },
    Err( e ) => 
    {
    println!( "Audio safety test failed (expected): {e}" );
      match e
      {
        Error::InvalidArgument( _ ) | Error::ApiError( _ ) => {
          // Expected for unsupported features
        },
      _ => panic!( "Unexpected error type : {e}" ),
      }
    }
  }
}

/// Test large audio file handling
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_large_audio_file()
{
  let client = Client::new()
  .expect( "Failed to create client for large audio test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Create a larger audio data sample (simulated)
  let large_audio_data = create_large_test_audio_data();
  
  let request = GenerateContentRequest 
  {
    contents: vec![ Content 
    {
      parts: vec![
      Part 
      {
        text: Some( "Summarize this long audio recording.".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      },
      Part 
      {
        text: None,
        inline_data: Some( Blob 
        {
          mime_type: "audio/mp3".to_string(),
          data: large_audio_data,
        } ),
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    } ],
    tools: None,
    tool_config: None,
    system_instruction: None,
    safety_settings: None,
    generation_config: None,
    cached_content: None,
  };
  
  let result = model.generate_content( &request ).await;
  
  match result 
  {
    Ok( response ) => 
    {
      assert!( !response.candidates.is_empty() );
      println!( "Successfully processed large audio file" );
    },
    Err( e ) => 
    {
    println!( "Large audio test failed : {e}" );
      // Could fail due to size limits or unsupported feature
      match e
      {
        Error::InvalidArgument( _ ) | Error::ApiError( _ ) | Error::NetworkError( _ ) => {
          // Expected failures
        },
      _ => panic!( "Unexpected error type : {e}" ),
      }
    }
  }
}

/// Test audio with invalid format
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]
async fn test_invalid_audio_format()
{
  let client = Client::new()
  .expect( "Failed to create client for invalid audio test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Test with corrupted/invalid audio data
  let request = GenerateContentRequest 
  {
    contents: vec![ Content 
    {
      parts: vec![
      Part 
      {
        text: Some( "Process this audio.".to_string() ),
        inline_data: None,
        function_call: None,
        function_response: None,
        ..Default::default()
      },
      Part 
      {
        text: None,
        inline_data: Some( Blob 
        {
          mime_type: "audio/mp3".to_string(),
          data: "invalid_audio_data".to_string(), // Invalid base64 data
        } ),
        function_call: None,
        function_response: None,
        ..Default::default()
      }
      ],
      role: "user".to_string(),
    } ],
    tools: None,
    tool_config: None,
    system_instruction: None,
    safety_settings: None,
    generation_config: None,
    cached_content: None,
  };
  
  let result = model.generate_content( &request ).await;
  
  // This should fail with proper error handling
  match result 
  {
    Ok( _ ) => panic!( "Expected failure for invalid audio data" ),
    Err( e ) => 
    {
      // Verify we get appropriate error types for invalid data
      match e
      {
        Error::InvalidArgument( _ ) | Error::DeserializationError( _ ) | Error::ApiError( _ ) => {
        println!( "Correctly rejected invalid audio data : {e}" );
        },
      _ => panic!( "Unexpected error type for invalid audio : {e}" ),
      }
    }
  }
}

/// Test batch audio processing
#[ tokio::test ]
#[ cfg( feature = "integration" ) ]

async fn test_batch_audio_processing()
{
  let client = Client::new()
  .expect( "Failed to create client for batch audio test" );
  
  let models_api = client.models();
  let model = models_api.by_name( "gemini-1.5-pro" );
  
  // Process multiple audio files in sequence
  let audio_files = vec![
  create_test_mp3_data(),
  create_test_wav_data(),
  create_test_ogg_data(),
  ];
  
  let mut results = Vec::new();
  
  for ( index, audio_data ) in audio_files.into_iter().enumerate() 
  {
    let request = GenerateContentRequest 
    {
      contents: vec![ Content 
      {
        parts: vec![
        Part 
        {
        text : Some( format!( "Analyze audio file #{}", index + 1 ) ),
          inline_data: None,
          function_call: None,
          function_response: None,
          ..Default::default()
        },
        Part 
        {
          text: None,
          inline_data: Some( Blob 
          {
            mime_type: match index
            {
              0 => "audio/mp3",
              1 => "audio/wav", 
              _ => "audio/ogg",
            }.to_string(),
            data: audio_data,
          } ),
          function_call: None,
          function_response: None,
          ..Default::default()
        }
        ],
        role: "user".to_string(),
      } ],
      tools: None,
      tool_config: None,
      system_instruction: None,
      safety_settings: None,
      generation_config: None,
      cached_content: None,
    };
  
    let result = model.generate_content( &request ).await;
    results.push( result );
  
    // Add small delay between requests to avoid rate limiting
    tokio ::time::sleep( core::time::Duration::from_millis( 100 ) ).await;
  }
  
  // Verify batch processing results
  let successful = results.iter().filter( | r | r.is_ok() ).count();
  let failed = results.iter().filter( | r | r.is_err() ).count();
  
println!( "Batch audio processing : {successful} successful, {failed} failed" );
  
  // At minimum, we expect proper error handling even if audio isn't supported
  assert_eq!( successful + failed, 3 );
}

// Helper functions to create test audio data

/// Create test MP3 data (minimal MP3 header for testing)
fn create_test_mp3_data() -> String
{
  // This is a minimal MP3 frame header encoded in base64
  // In a real implementation, this would be actual audio data
  use base64::Engine;
  let mp3_header = vec![ 0xFF, 0xFB, 0x90, 0x00 ]; // Basic MP3 sync frame
  base64 ::engine::general_purpose::STANDARD.encode( &mp3_header )
}

/// Create test WAV data (minimal WAV header for testing)
fn create_test_wav_data() -> String
{
  // Minimal WAV file header
  use base64::Engine;
  let wav_header = b"RIFF\x24\x00\x00\x00WAVEfmt \x10\x00\x00\x00";
  base64 ::engine::general_purpose::STANDARD.encode( wav_header )
}

/// Create test OGG data (minimal OGG header for testing)
fn create_test_ogg_data() -> String
{
  // Minimal OGG file header
  use base64::Engine;
  let ogg_header = b"OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00";
  base64 ::engine::general_purpose::STANDARD.encode( ogg_header )
}

/// Create test FLAC data (minimal FLAC header for testing)
fn create_test_flac_data() -> String
{
  // Minimal FLAC file header
  use base64::Engine;
  let flac_header = b"fLaC\x00\x00\x00\x22";
  base64 ::engine::general_purpose::STANDARD.encode( flac_header )
}

/// Create test M4A data (minimal M4A header for testing)
fn create_test_m4a_data() -> String
{
  // Minimal M4A/MP4 file header
  use base64::Engine;
  let m4a_header = b"\x00\x00\x00\x20ftypM4A ";
  base64 ::engine::general_purpose::STANDARD.encode( m4a_header )
}

/// Create larger test audio data for size limit testing
fn create_large_test_audio_data() -> String
{
  // Create a larger audio data sample (still minimal for testing)
  use base64::Engine;
  let mut large_data = Vec::new();
  
  // MP3 header
  large_data.extend_from_slice( &[ 0xFF, 0xFB, 0x90, 0x00 ] );
  
  // Add some dummy audio frames (simplified)
  for _ in 0..1000
  {
    large_data.extend_from_slice( &[ 0x00, 0x01, 0x02, 0x03 ] );
  }
  
  base64 ::engine::general_purpose::STANDARD.encode( &large_data )
}