rig-core 0.47.0

An opinionated library for building LLM powered applications.
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
use crate::catalog::ReasoningSupport;
use serde_json::{Value, json};

use crate::completion::{
    CacheRetention, CompletionRequest, Effort, GenerationOptions, OnUnsupported,
    ProviderToolDefinition, Reasoning, ServiceTier,
};
use crate::error::ProviderError;
use crate::operation::Completion;
use crate::providers::gemini::GeminiConfig;
use crate::providers::gemini::completion::GenerateContent;
use crate::providers::gemini::interactions_api::{InteractionResume, Interactions};
use crate::wire::{Body, Mode, Operation, Wire};

fn sent<W>(wire: &W, request: CompletionRequest) -> Result<Value, ProviderError>
where
    W: Wire<Op = Completion, Payload = crate::wire::Encoded>,
{
    let request = Completion::prepare(request, &wire.describe())?;
    let encoded = wire.encode(request, Mode::Unary)?;
    let Body::Bytes(bytes) = encoded.request.body() else {
        return Err(ProviderError::request("a Gemini body is JSON"));
    };
    if bytes.is_empty() {
        return Ok(Value::Null);
    }
    Ok(serde_json::from_slice(bytes)?)
}

fn with(options: GenerationOptions) -> CompletionRequest {
    CompletionRequest::new("hi").options(options)
}

fn refused(result: Result<Value, ProviderError>) -> Option<&'static str> {
    match result {
        Err(ProviderError::UnsupportedOption(option)) => match option.option {
            std::borrow::Cow::Borrowed(name) => Some(name),
            std::borrow::Cow::Owned(name) => panic!("a GenerationOptions field, got {name}"),
        },
        _ => None,
    }
}

fn rest(model: &str) -> GenerateContent {
    GenerateContent::new(GeminiConfig::new("k"), model)
}

#[test]
fn generate_content_reasoning_follows_the_models_thinking() {
    let body = sent(
        &rest("gemini-2.5-flash"),
        with(GenerationOptions::default().reasoning(Reasoning::Off)),
    )
    .expect("2.5 Flash turns thinking off with a zero budget");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingBudget": 0})
    );
    let body = sent(
        &rest("gemini-2.5-pro"),
        with(GenerationOptions::default().reasoning(Reasoning::Budget { tokens: 4096 })),
    )
    .expect("2.5 Pro takes a budget in range");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingBudget": 4096})
    );
    let body = sent(
        &rest("gemini-3.8-flash"),
        with(GenerationOptions::default().reasoning(Effort::Low)),
    )
    .expect("3.8 Flash takes `low`");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingLevel": "low"})
    );
    let body = sent(
        &rest("gemini-4-hydrogen"),
        with(GenerationOptions::default().reasoning(Effort::XHigh)),
    )
    .expect("4 Hydrogen takes `xhigh`");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingLevel": "xhigh"})
    );
    for (model, reasoning) in [
        ("gemini-4-hydrogen", Reasoning::Effort(Effort::Max)),
        ("gemini-4-carbon", Reasoning::Effort(Effort::XHigh)),
        ("gemini-5-unlisted", Reasoning::Effort(Effort::XHigh)),
        ("gemini-3.8-flash", Reasoning::Effort(Effort::Minimal)),
        ("gemini-3.8-flash", Reasoning::Off),
        ("gemini-3.8-flash", Reasoning::Effort(Effort::XHigh)),
        ("gemini-2.5-pro", Reasoning::Off),
        ("gemini-2.5-pro", Reasoning::Budget { tokens: 64 }),
        ("gemini-2.5-flash", Reasoning::Effort(Effort::High)),
    ] {
        assert_eq!(
            refused(sent(
                &rest(model),
                with(GenerationOptions::default().reasoning(reasoning))
            )),
            Some("reasoning"),
            "{model}: {reasoning:?}"
        );
    }
}

/// On GenerateContent the encoder refuses a reasoning option exactly when
/// `ModelSpec::validate` does, for every Gemini API row whose thinking the
/// catalog knows. A row whose controls it does not know refuses nothing in
/// `validate`; the encoder answers it by its family, as an unlisted id.
#[test]
fn generate_content_reasoning_agrees_with_validate() {
    let reasonings = [
        Reasoning::Off,
        Reasoning::Effort(Effort::Minimal),
        Reasoning::Effort(Effort::Low),
        Reasoning::Effort(Effort::Medium),
        Reasoning::Effort(Effort::High),
        Reasoning::Effort(Effort::XHigh),
        Reasoning::Budget { tokens: 0 },
        Reasoning::Budget { tokens: 1024 },
        Reasoning::Budget { tokens: 30_000 },
    ];
    let mut checked = 0;
    for spec in crate::catalog::Catalog::builtin()
        .iter()
        .filter(|spec| spec.provider.vendor() == "gcp.gemini")
    {
        for reasoning in &reasonings {
            let options = GenerationOptions::default().reasoning(*reasoning);
            let encoded = sent(&rest(&spec.id), with(options.clone()));
            if let ReasoningSupport::Unknown { .. } = spec.reasoning {
                assert!(spec.validate(&options).is_ok(), "{}", spec.id);
                continue;
            }
            assert_eq!(
                refused(encoded).is_some(),
                spec.validate(&options).is_err(),
                "{}: {reasoning:?}",
                spec.id
            );
            checked += 1;
        }
    }
    assert!(checked > 300, "{checked}");
}

/// Gemma 4 turns thinking on with level `high` and off with `minimal`; the
/// image models think as their model pages say.
#[test]
fn gemma_and_the_image_models_think_as_documented() {
    for model in ["gemma-4-26b-a4b-it", "gemma-4-31b-it"] {
        let body = sent(
            &rest(model),
            with(GenerationOptions::default().reasoning(Reasoning::Off)),
        )
        .expect("Gemma 4 turns thinking off");
        assert_eq!(
            body["generationConfig"]["thinkingConfig"],
            json!({"thinkingLevel": "minimal"})
        );
        let body = sent(
            &rest(model),
            with(GenerationOptions::default().reasoning(Effort::High)),
        )
        .expect("Gemma 4 thinks at `high`");
        assert_eq!(
            body["generationConfig"]["thinkingConfig"],
            json!({"thinkingLevel": "high"})
        );
    }
    let body = sent(
        &rest("gemini-3-pro-image-preview"),
        with(GenerationOptions::default().reasoning(Effort::Low)),
    )
    .expect("the preview takes the GA model's levels");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingLevel": "low"})
    );
    let body = sent(
        &rest("gemini-2.5-flash-image"),
        with(GenerationOptions::default().reasoning(Reasoning::Off)),
    )
    .expect("2.5 Flash Image does not think");
    assert!(
        body["generationConfig"].get("thinkingConfig").is_none(),
        "{body}"
    );
}

/// An id the catalog does not list takes the thinking of the model it
/// versions: a `-001` revision or a dated preview.
#[test]
fn a_versioned_gemini_id_takes_its_models_thinking() {
    let body = sent(
        &rest("gemini-2.5-flash-preview-09-2025"),
        with(GenerationOptions::default().reasoning(Reasoning::Off)),
    )
    .expect("a 2.5 Flash preview turns thinking off");
    assert_eq!(
        body["generationConfig"]["thinkingConfig"],
        json!({"thinkingBudget": 0})
    );
    assert_eq!(
        refused(sent(
            &rest("models/gemini-2.5-flash-lite-preview-06-17"),
            with(GenerationOptions::default().reasoning(Reasoning::Budget { tokens: 100 })),
        )),
        Some("reasoning"),
        "2.5 Flash-Lite's budget starts at 512"
    );
    let body = sent(
        &rest("gemini-2.0-flash-001"),
        with(GenerationOptions::default().reasoning(Reasoning::Off)),
    )
    .expect("2.0 Flash does not think");
    assert!(
        body.get("generationConfig")
            .is_none_or(|config| config.get("thinkingConfig").is_none()),
        "{body}"
    );
    let body = sent(
        &rest("gemini-1.5-pro-002"),
        with(GenerationOptions::default().reasoning(Reasoning::Off)),
    )
    .expect("an unlisted model before 2.5 does not think");
    assert!(
        body.get("generationConfig")
            .is_none_or(|config| config.get("thinkingConfig").is_none()),
        "{body}"
    );
}

/// A raw `generationConfig` merges over the typed fields and the mapped
/// options, key by key: a key set both ways is the raw one.
#[test]
fn a_raw_generation_config_beats_the_typed_fields_and_keeps_the_rest() {
    let body = sent(
        &rest("gemini-3-flash-preview"),
        with(GenerationOptions::default().top_p(0.5).seed(7))
            .temperature(0.2)
            .max_tokens(64)
            .additional_params(json!({"generationConfig": {"temperature": 0.9, "topK": 3}})),
    )
    .expect("encodes");
    assert_eq!(
        body["generationConfig"],
        json!({"temperature": 0.9, "maxOutputTokens": 64, "topP": 0.5, "seed": 7, "topK": 3})
    );
}

/// A raw `generationConfig: null` is absent, as it was before the merge:
/// the typed fields and the mapped thinking are still sent.
#[test]
fn a_raw_null_generation_config_keeps_the_typed_fields() {
    let schema: schemars::Schema =
        serde_json::from_value(json!({"type": "object"})).expect("a schema");
    let body = sent(
        &rest("gemini-2.5-flash"),
        with(GenerationOptions::default().reasoning(Reasoning::Off))
            .temperature(0.2)
            .max_tokens(64)
            .output_schema(schema)
            .additional_params(json!({"generationConfig": null})),
    )
    .expect("encodes");
    assert_eq!(
        body["generationConfig"],
        json!({
            "responseMimeType": "application/json",
            "responseJsonSchema": {"type": "object"},
            "temperature": 0.2,
            "maxOutputTokens": 64,
            "thinkingConfig": {"thinkingBudget": 0},
        })
    );
}

#[test]
fn tiers_follow_the_route() {
    let body = sent(
        &rest("gemini-3-flash-preview"),
        with(GenerationOptions::default().service_tier(ServiceTier::Flex)),
    )
    .expect("the Gemini API takes flex");
    assert_eq!(body["serviceTier"], "flex");
    let body = sent(
        &rest("gemini-3-flash-preview"),
        with(GenerationOptions::default().service_tier(ServiceTier::Auto)),
    )
    .expect("auto is the default");
    assert!(body.get("serviceTier").is_none(), "{body}");
    assert_eq!(
        refused(sent(
            &rest("gemini-3-flash-preview"),
            with(GenerationOptions::default().seed(u64::from(u32::MAX)))
        )),
        Some("seed")
    );
}

#[test]
fn interactions_cells() {
    let wire = Interactions::new(GeminiConfig::new("k"), "gemini-3-flash-preview");
    let body = sent(
        &wire,
        with(
            GenerationOptions::default()
                .reasoning(Effort::High)
                .seed(3)
                .stop(["END"])
                .service_tier(ServiceTier::Priority),
        )
        .temperature(0.1),
    )
    .expect("Interactions options");
    assert_eq!(
        body["generation_config"],
        json!({"temperature": 0.1, "thinking_level": "high", "seed": 3, "stop_sequences": ["END"]})
    );
    assert_eq!(body["service_tier"], "priority");
    for options in [
        GenerationOptions::default().reasoning(Reasoning::Off),
        GenerationOptions::default().top_p(0.5),
        GenerationOptions::default().cache(CacheRetention::Long),
    ] {
        assert!(refused(sent(&wire, with(options))).is_some());
    }
    let hydrogen = Interactions::new(GeminiConfig::new("k"), "gemini-4-hydrogen");
    let body = sent(
        &hydrogen,
        with(GenerationOptions::default().reasoning(Effort::XHigh)),
    )
    .expect("4 Hydrogen takes `xhigh`");
    assert_eq!(body["generation_config"]["thinking_level"], "xhigh");
    let unlisted = Interactions::new(GeminiConfig::new("k"), "gemini-5-unlisted");
    for wire in [&wire, &unlisted] {
        assert!(
            refused(sent(
                wire,
                with(GenerationOptions::default().reasoning(Effort::XHigh))
            ))
            .is_some()
        );
    }
}

#[test]
fn a_resumed_interaction_refuses_options_and_raw_params() {
    let resume = InteractionResume::new(GeminiConfig::new("k"), "interaction-1");
    assert_eq!(
        refused(sent(&resume, with(GenerationOptions::default().seed(1)))),
        Some("seed")
    );
    let body = sent(
        &resume,
        with(
            GenerationOptions::default()
                .seed(1)
                .on_unsupported(OnUnsupported::Ignore),
        ),
    )
    .expect("an ignored option leaves an empty read");
    assert_eq!(body, Value::Null);
    let error = sent(
        &resume,
        CompletionRequest::new("hi").additional_params(json!({"stream": true})),
    )
    .expect_err("raw parameters have nothing to reach");
    assert!(error.to_string().contains("additional_params"), "{error}");
    // Raw tools leave the raw layer for the base to append; the resume base
    // refuses them rather than drop them.
    for request in [
        CompletionRequest::new("hi").additional_params(json!({"tools": [{"googleSearch": {}}]})),
        CompletionRequest::new("hi").provider_tool(ProviderToolDefinition::new("googleSearch")),
    ] {
        let error = sent(&resume, request).expect_err("raw tools have nothing to reach");
        assert!(
            error.to_string().contains("additional_params.tools"),
            "{error}"
        );
    }
}

#[test]
fn uncatalogued_names_keep_specific_prefixes_and_unknown_families() {
    use super::{Thinking, named_thinking};

    assert!(matches!(
        named_thinking("gemini-2.5-flash-lite-future"),
        Thinking::Budget { range, can_disable: true } if range == (512..=24_576)
    ));
    assert!(matches!(
        named_thinking("gemini-2.5-flash-future"),
        Thinking::Budget { range, can_disable: true } if range == (0..=24_576)
    ));
    assert!(matches!(
        named_thinking("gemini-3-pro-preview-future"),
        Thinking::Levels {
            levels: [Effort::Low, Effort::High],
            can_disable: false
        }
    ));
    for model in ["gemini-3.9-flash", "gemini-2.1-pro", "unknown"] {
        assert!(matches!(named_thinking(model), Thinking::Unknown));
    }
    assert!(matches!(
        named_thinking("gemini-2.0-flash-future"),
        Thinking::None
    ));
}