adk-rs 0.6.0

Rust port of the Google Agent Development Kit (ADK).
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! Gemini HTTP client.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use parking_lot::Mutex;
use reqwest::Client;
use tracing::{debug, instrument, warn};

use crate::core::cache::{CacheMetadata, ContextCacheConfig};
use crate::core::retry::RetryConfig;
use crate::core::stream::LlmResponseStream;
use crate::core::{LlmRequest, LlmResponse, Model};
use crate::error::{Error, ProviderError, Result};
use crate::providers::common::send_with_retry;

use crate::providers::gemini::convert::{
    WireCachedContentCreate, parse_response, to_wire, to_wire_cached,
};
use crate::providers::gemini::stream::from_sse;

/// One explicit-cache slot, keyed by the fingerprint of the cacheable
/// prefix (model + system instruction + tools).
#[derive(Debug, Clone)]
enum CacheSlot {
    /// A live server-side cache entry.
    Active {
        name: String,
        expires_at: Instant,
        uses: u32,
    },
    /// Caching was attempted and failed (e.g. prefix below the server-side
    /// minimum); don't retry until this deadline.
    Disabled { until: Instant },
}

/// Configuration for [`Gemini`].
#[derive(Debug, Clone)]
pub struct GeminiConfig {
    /// Base API URL (default: `https://generativelanguage.googleapis.com`).
    pub base_url: String,
    /// API version path segment (default: `v1beta`).
    pub api_version: String,
    /// API key (required). If empty, loaded from `$GOOGLE_API_KEY`.
    pub api_key: String,
    /// Total timeout for non-streaming requests. Streaming requests are
    /// exempt (an SSE body lasts as long as the generation does); only the
    /// connect timeout applies to them.
    pub timeout: Duration,
    /// Retry policy for transient failures (429 / 5xx / connect errors).
    pub retry: RetryConfig,
}

impl Default for GeminiConfig {
    fn default() -> Self {
        Self {
            base_url: "https://generativelanguage.googleapis.com".into(),
            api_version: "v1beta".into(),
            api_key: String::new(),
            timeout: Duration::from_secs(60),
            retry: RetryConfig::default(),
        }
    }
}

/// Gemini provider.
#[derive(Debug, Clone)]
pub struct Gemini {
    model_name: String,
    cfg: GeminiConfig,
    http: Client,
    /// Explicit context-cache entries, keyed by prefix fingerprint. Only
    /// consulted when a request carries a
    /// [`crate::core::ContextCacheConfig`].
    caches: Arc<Mutex<HashMap<u64, CacheSlot>>>,
}

impl Gemini {
    /// Construct from config and a model name.
    pub fn new(model_name: impl Into<String>, cfg: GeminiConfig) -> Result<Self> {
        // Refuse to ship an API key over plaintext HTTP. Loopback is allowed
        // for local mocks (the test suite below depends on this).
        crate::transport_security::require_secure_url(&cfg.base_url, "GeminiConfig.base_url")?;
        // No client-wide total timeout: it would also cap streaming bodies,
        // killing any SSE generation longer than the timeout. Unary calls
        // apply `cfg.timeout` per-request instead. Redirects are disabled:
        // reqwest re-sends custom headers (`x-goog-api-key`) on redirect,
        // so a redirecting endpoint could exfiltrate the key.
        let http = Client::builder()
            .connect_timeout(Duration::from_secs(10))
            .redirect(reqwest::redirect::Policy::none())
            .user_agent(concat!("adk-rs/", env!("CARGO_PKG_VERSION")))
            .build()
            .map_err(|e| ProviderError::Transport(e.to_string()))?;
        Ok(Self {
            model_name: model_name.into(),
            cfg,
            http,
            caches: Arc::new(Mutex::new(HashMap::new())),
        })
    }

    /// Construct from `$GOOGLE_API_KEY`.
    pub fn from_env(model_name: impl Into<String>) -> Result<Self> {
        let api_key = std::env::var("GOOGLE_API_KEY")
            .map_err(|_| Error::config("GOOGLE_API_KEY env var not set"))?;
        Self::new(
            model_name,
            GeminiConfig {
                api_key,
                ..GeminiConfig::default()
            },
        )
    }

    /// Config accessor for sibling modules (the `live` WebSocket client).
    #[cfg(feature = "live")]
    pub(crate) fn config(&self) -> &GeminiConfig {
        &self.cfg
    }

    fn endpoint(&self, action: &str) -> String {
        format!(
            "{}/{}/models/{}:{}",
            self.cfg.base_url.trim_end_matches('/'),
            self.cfg.api_version,
            self.model_name,
            action
        )
    }

    fn auth_header(&self) -> Result<String> {
        if self.cfg.api_key.is_empty() {
            return Err(Error::Provider(ProviderError::Auth(
                "Gemini api_key is empty; set $GOOGLE_API_KEY".into(),
            )));
        }
        Ok(self.cfg.api_key.clone())
    }

    /// Fingerprint of the cacheable prefix: model + system instruction +
    /// tool declarations. Any change invalidates the cache entry.
    fn cache_fingerprint(&self, req: &LlmRequest) -> u64 {
        use std::hash::{Hash, Hasher};
        let mut h = std::collections::hash_map::DefaultHasher::new();
        self.model_name.hash(&mut h);
        serde_json::to_string(&req.config.system_instruction)
            .unwrap_or_default()
            .hash(&mut h);
        serde_json::to_string(&req.config.tools)
            .unwrap_or_default()
            .hash(&mut h);
        h.finish()
    }

    /// Rough token estimate of the cacheable prefix (chars / 4).
    fn estimate_prefix_tokens(req: &LlmRequest) -> u64 {
        let sys = serde_json::to_string(&req.config.system_instruction).unwrap_or_default();
        let tools = serde_json::to_string(&req.config.tools).unwrap_or_default();
        ((sys.len() + tools.len()) / 4) as u64
    }

    /// Resolve (or create) the explicit cache entry for `req`. Returns the
    /// cache resource name and whether it was a hit, or `None` when caching
    /// is skipped (too small, disabled after a failure, or creation failed).
    async fn resolve_cache(
        &self,
        req: &LlmRequest,
        cfg: &ContextCacheConfig,
    ) -> Option<CacheMetadata> {
        if req.config.system_instruction.is_none() && req.config.tools.is_empty() {
            return None;
        }
        if Self::estimate_prefix_tokens(req) < cfg.min_tokens {
            return None;
        }
        let fp = self.cache_fingerprint(req);
        {
            let mut guard = self.caches.lock();
            match guard.get_mut(&fp) {
                Some(CacheSlot::Active {
                    name,
                    expires_at,
                    uses,
                }) if *expires_at > Instant::now() && *uses < cfg.cache_intervals => {
                    *uses += 1;
                    return Some(CacheMetadata {
                        cache_name: name.clone(),
                        cache_hit: true,
                    });
                }
                Some(CacheSlot::Disabled { until }) if *until > Instant::now() => {
                    return None;
                }
                _ => {}
            }
        }

        // Create (or refresh) the entry. Failures disable caching for one
        // TTL rather than failing the call — caching is an optimization.
        match self.create_cached_content(req, cfg).await {
            Ok(name) => {
                self.caches.lock().insert(
                    fp,
                    CacheSlot::Active {
                        name: name.clone(),
                        expires_at: Instant::now() + Duration::from_secs(cfg.ttl_seconds),
                        uses: 1,
                    },
                );
                Some(CacheMetadata {
                    cache_name: name,
                    cache_hit: false,
                })
            }
            Err(e) => {
                warn!("context-cache creation failed (caching disabled for ttl): {e}");
                self.caches.lock().insert(
                    fp,
                    CacheSlot::Disabled {
                        until: Instant::now() + Duration::from_secs(cfg.ttl_seconds),
                    },
                );
                None
            }
        }
    }

    /// `POST /{version}/cachedContents` — create an explicit cache entry for
    /// the request's prefix. Returns the resource name.
    async fn create_cached_content(
        &self,
        req: &LlmRequest,
        cfg: &ContextCacheConfig,
    ) -> Result<String> {
        let url = format!(
            "{}/{}/cachedContents",
            self.cfg.base_url.trim_end_matches('/'),
            self.cfg.api_version,
        );
        let body = WireCachedContentCreate {
            model: format!("models/{}", self.model_name),
            system_instruction: req.config.system_instruction.as_ref(),
            tools: &req.config.tools,
            ttl: format!("{}s", cfg.ttl_seconds),
        };
        let key = self.auth_header()?;
        let body = serde_json::to_vec(&body)?;
        let resp = send_with_retry(&self.cfg.retry, || {
            self.http
                .post(&url)
                .timeout(self.cfg.timeout)
                .header("x-goog-api-key", key.clone())
                .header("content-type", "application/json")
                .body(body.clone())
                .send()
        })
        .await?;
        let status = resp.status();
        let bytes = resp
            .bytes()
            .await
            .map_err(|e| ProviderError::Transport(e.to_string()))?;
        if !status.is_success() {
            return Err(Error::Provider(ProviderError::Http {
                status: status.as_u16(),
                body: String::from_utf8_lossy(&bytes).to_string(),
            }));
        }
        let v: serde_json::Value = serde_json::from_slice(&bytes)?;
        v.get("name")
            .and_then(|n| n.as_str())
            .map(str::to_string)
            .ok_or_else(|| {
                Error::Provider(ProviderError::Decode(
                    "cachedContents response missing `name`".into(),
                ))
            })
    }

    /// Drop the cache entry that produced `cache_name` (e.g. after the
    /// server rejected it) so the next call re-creates it.
    fn invalidate_cache_entry(&self, cache_name: &str) {
        self.caches.lock().retain(
            |_, slot| !matches!(slot, CacheSlot::Active { name, .. } if name == cache_name),
        );
    }

    /// Serialize `req` to a wire body, consulting the explicit cache when a
    /// cache config is attached. Returns the body and the cache metadata.
    async fn wire_body(&self, req: &LlmRequest) -> Result<(Vec<u8>, Option<CacheMetadata>)> {
        if let Some(cfg) = &req.cache_config {
            if let Some(meta) = self.resolve_cache(req, cfg).await {
                let body = serde_json::to_vec(&to_wire_cached(req, &meta.cache_name))?;
                return Ok((body, Some(meta)));
            }
        }
        Ok((serde_json::to_vec(&to_wire(req))?, None))
    }
}

#[async_trait]
impl Model for Gemini {
    fn name(&self) -> &str {
        &self.model_name
    }

    fn supported_models(&self) -> &'static [&'static str] {
        &["gemini-*"]
    }

    #[instrument(skip(self, req), fields(model = %self.model_name))]
    async fn generate_content(&self, req: LlmRequest) -> Result<LlmResponse> {
        let url = self.endpoint("generateContent");
        let (body, mut cache_meta) = self.wire_body(&req).await?;
        debug!(bytes = body.len(), %url, cached = cache_meta.is_some(), "Gemini request");
        let key = self.auth_header()?;
        let mut resp = send_with_retry(&self.cfg.retry, || {
            self.http
                .post(&url)
                .timeout(self.cfg.timeout)
                .header("x-goog-api-key", key.clone())
                .header("content-type", "application/json")
                .body(body.clone())
                .send()
        })
        .await?;
        let mut status = resp.status();

        // If the cached reference was rejected (expired/evicted server-side),
        // invalidate and retry once without the cache.
        if !status.is_success() && status.is_client_error() {
            if let Some(meta) = cache_meta.take() {
                warn!(
                    status = status.as_u16(),
                    cache = %meta.cache_name,
                    "cached request rejected; retrying without cache"
                );
                self.invalidate_cache_entry(&meta.cache_name);
                let body = serde_json::to_vec(&to_wire(&req))?;
                resp = send_with_retry(&self.cfg.retry, || {
                    self.http
                        .post(&url)
                        .timeout(self.cfg.timeout)
                        .header("x-goog-api-key", key.clone())
                        .header("content-type", "application/json")
                        .body(body.clone())
                        .send()
                })
                .await?;
                status = resp.status();
            }
        }

        let bytes = resp
            .bytes()
            .await
            .map_err(|e| ProviderError::Transport(e.to_string()))?;
        if !status.is_success() {
            let body = String::from_utf8_lossy(&bytes).to_string();
            return Err(Error::Provider(ProviderError::Http {
                status: status.as_u16(),
                body,
            }));
        }
        let mut response = parse_response(&bytes)
            .map_err(|e| Error::Provider(ProviderError::Decode(format!("{e}"))))?;
        response.cache_metadata = cache_meta;
        Ok(response)
    }

    async fn stream_generate_content(&self, req: LlmRequest) -> Result<LlmResponseStream> {
        let url = format!("{}?alt=sse", self.endpoint("streamGenerateContent"));
        let (body, mut cache_meta) = self.wire_body(&req).await?;
        let key = self.auth_header()?;
        let mut resp = send_with_retry(&self.cfg.retry, || {
            self.http
                .post(&url)
                .header("x-goog-api-key", key.clone())
                .header("content-type", "application/json")
                .body(body.clone())
                .send()
        })
        .await?;
        if !resp.status().is_success() && resp.status().is_client_error() {
            if let Some(meta) = cache_meta.take() {
                warn!(
                    status = resp.status().as_u16(),
                    cache = %meta.cache_name,
                    "cached stream request rejected; retrying without cache"
                );
                self.invalidate_cache_entry(&meta.cache_name);
                let body = serde_json::to_vec(&to_wire(&req))?;
                resp = send_with_retry(&self.cfg.retry, || {
                    self.http
                        .post(&url)
                        .header("x-goog-api-key", key.clone())
                        .header("content-type", "application/json")
                        .body(body.clone())
                        .send()
                })
                .await?;
            }
        }
        if !resp.status().is_success() {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_else(|_| "<no body>".into());
            return Err(Error::Provider(ProviderError::Http { status, body }));
        }
        Ok(from_sse(resp))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use wiremock::matchers::{header, method, path, query_param};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    #[tokio::test]
    async fn rejects_plaintext_http_base_url() {
        let err = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: "http://generativelanguage.googleapis.com".into(),
                api_key: "k".into(),
                ..GeminiConfig::default()
            },
        )
        .unwrap_err();
        assert!(err.to_string().to_lowercase().contains("https"));
    }

    #[tokio::test]
    async fn generate_content_happy_path() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1beta/models/gemini-2.5-flash:generateContent"))
            .and(header("x-goog-api-key", "test-key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "candidates": [{
                    "content": {"role": "model", "parts": [{"text": "ok"}]},
                    "finishReason": "STOP"
                }],
                "usageMetadata": {"promptTokenCount": 1, "candidatesTokenCount": 1, "totalTokenCount": 2}
            })))
            .mount(&server)
            .await;
        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "test-key".into(),
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let req = LlmRequest {
            contents: vec![crate::genai_types::Content::user_text("hi")],
            ..Default::default()
        };
        let r = g.generate_content(req).await.unwrap();
        assert_eq!(r.content.unwrap().text_concat(), "ok");
        let usage = r.usage_metadata.unwrap();
        assert_eq!(usage.prompt_token_count, Some(1));
    }

    #[tokio::test]
    async fn context_cache_created_once_and_reused() {
        use wiremock::matchers::body_partial_json;

        let server = MockServer::start().await;
        // Cache creation must happen exactly once.
        Mock::given(method("POST"))
            .and(path("/v1beta/cachedContents"))
            .respond_with(
                ResponseTemplate::new(200).set_body_json(json!({"name": "cachedContents/abc123"})),
            )
            .expect(1)
            .mount(&server)
            .await;
        // Cached generate calls reference the entry and omit the prefix.
        Mock::given(method("POST"))
            .and(path("/v1beta/models/gemini-2.5-flash:generateContent"))
            .and(body_partial_json(
                json!({"cachedContent": "cachedContents/abc123"}),
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "candidates": [{
                    "content": {"role": "model", "parts": [{"text": "ok"}]},
                    "finishReason": "STOP"
                }]
            })))
            .expect(2)
            .mount(&server)
            .await;

        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let mut req = LlmRequest {
            contents: vec![crate::genai_types::Content::user_text("hi")],
            cache_config: Some(ContextCacheConfig::default()),
            ..Default::default()
        };
        req.append_system_text("a long stable instruction");

        let r1 = g.generate_content(req.clone()).await.unwrap();
        let m1 = r1.cache_metadata.unwrap();
        assert_eq!(m1.cache_name, "cachedContents/abc123");
        assert!(!m1.cache_hit, "first call creates the entry");

        let r2 = g.generate_content(req).await.unwrap();
        let m2 = r2.cache_metadata.unwrap();
        assert!(m2.cache_hit, "second call reuses the entry");
        // Mock expectations (1 create, 2 cached generates) assert the rest.
    }

    #[tokio::test]
    async fn context_cache_skipped_below_min_tokens() {
        let server = MockServer::start().await;
        // No cachedContents call expected at all.
        Mock::given(method("POST"))
            .and(path("/v1beta/cachedContents"))
            .respond_with(ResponseTemplate::new(500))
            .expect(0)
            .mount(&server)
            .await;
        Mock::given(method("POST"))
            .and(path("/v1beta/models/gemini-2.5-flash:generateContent"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "candidates": [{
                    "content": {"role": "model", "parts": [{"text": "ok"}]},
                    "finishReason": "STOP"
                }]
            })))
            .expect(1)
            .mount(&server)
            .await;

        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let mut req = LlmRequest {
            cache_config: Some(ContextCacheConfig {
                min_tokens: 100_000,
                ..ContextCacheConfig::default()
            }),
            ..Default::default()
        };
        req.append_system_text("tiny");
        let r = g.generate_content(req).await.unwrap();
        assert!(r.cache_metadata.is_none());
    }

    #[tokio::test]
    async fn http_error_surfaces_as_provider_error() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(429).set_body_string("rate limited"))
            .mount(&server)
            .await;
        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                retry: RetryConfig::disabled(),
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let err = g.generate_content(LlmRequest::default()).await.unwrap_err();
        assert!(matches!(
            err,
            Error::Provider(ProviderError::Http { status: 429, .. })
        ));
    }

    #[tokio::test]
    async fn rate_limit_retried_until_success() {
        let server = MockServer::start().await;
        // First two attempts are rate-limited (one with Retry-After), the
        // third succeeds. Mount order matters: wiremock picks the first
        // matching mock with remaining allowance.
        Mock::given(method("POST"))
            .respond_with(
                ResponseTemplate::new(429)
                    .insert_header("retry-after", "0")
                    .set_body_string("rate limited"),
            )
            .up_to_n_times(2)
            .expect(2)
            .mount(&server)
            .await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                "candidates": [{
                    "content": {"role": "model", "parts": [{"text": "ok"}]},
                    "finishReason": "STOP"
                }]
            })))
            .expect(1)
            .mount(&server)
            .await;
        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                retry: RetryConfig {
                    max_retries: 2,
                    initial_backoff: std::time::Duration::from_millis(5),
                    ..RetryConfig::default()
                },
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let r = g.generate_content(LlmRequest::default()).await.unwrap();
        assert_eq!(r.content.unwrap().text_concat(), "ok");
    }

    #[tokio::test]
    async fn retry_budget_exhausted_surfaces_last_error() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(503).set_body_string("overloaded"))
            .expect(2) // initial attempt + 1 retry
            .mount(&server)
            .await;
        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                retry: RetryConfig {
                    max_retries: 1,
                    initial_backoff: std::time::Duration::from_millis(5),
                    ..RetryConfig::default()
                },
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let err = g.generate_content(LlmRequest::default()).await.unwrap_err();
        assert!(matches!(
            err,
            Error::Provider(ProviderError::Http { status: 503, .. })
        ));
    }

    #[tokio::test]
    async fn stream_endpoint_uses_sse_query() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/v1beta/models/gemini-2.5-flash:streamGenerateContent"))
            .and(query_param("alt", "sse"))
            .respond_with(
                ResponseTemplate::new(200)
                    .insert_header("content-type", "text/event-stream")
                    .set_body_string(
                        "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"hi\"}]},\"finishReason\":\"STOP\"}]}\n\n",
                    ),
            )
            .mount(&server)
            .await;
        let g = Gemini::new(
            "gemini-2.5-flash",
            GeminiConfig {
                base_url: server.uri(),
                api_key: "k".into(),
                ..GeminiConfig::default()
            },
        )
        .unwrap();
        let stream = g
            .stream_generate_content(LlmRequest::default())
            .await
            .unwrap();
        let chunks = crate::providers::gemini::stream::collect_stream(stream)
            .await
            .unwrap();
        assert!(!chunks.is_empty());
        assert_eq!(chunks[0].content.as_ref().unwrap().text_concat(), "hi");
    }
}