Skip to main content

gadk_model/
variant.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Detection of the Google backend and Gemini model generation behind an [`Llm`].
17
18use std::sync::LazyLock;
19
20use regex::Regex;
21
22use crate::llm::{Backend, Llm};
23
24/// `^gemini-(\d+(\.\d+)?)`; `None` only if the literal failed to compile (impossible),
25/// in which case nothing matches.
26static GEMINI_MODEL_VERSION_REGEX: LazyLock<Option<Regex>> =
27    LazyLock::new(|| Regex::new(r"^gemini-(\d+(\.\d+)?)").ok());
28
29/// The Google LLM variant of `llm` (Gemini API or Vertex AI), or `None` when unspecified.
30pub fn google_variant(llm: &dyn Llm) -> Option<Backend> {
31    llm.google_variant()
32}
33
34/// Whether `model` names a Gemini model.
35pub fn is_gemini_model(model: &str) -> bool {
36    extract_model_name(model).starts_with("gemini-")
37}
38
39/// Whether `model` names a Gemini 1.x model.
40pub fn is_gemini1_model(model: &str) -> bool {
41    extract_gemini_version(model).is_some_and(|v| v < 2.0)
42}
43
44/// Whether `model` names a Gemini 2.5 or lower model.
45pub fn is_gemini25_or_lower(model: &str) -> bool {
46    extract_gemini_version(model).is_some_and(|v| v <= 2.5)
47}
48
49/// Whether `model` names a Gemini 2.0 or above model.
50pub fn is_gemini2_or_above(model: &str) -> bool {
51    extract_gemini_version(model).is_some_and(|v| v >= 2.0)
52}
53
54/// Whether `llm` supports an output schema together with tools natively: only Vertex AI
55/// for Gemini 2.0+.
56pub fn can_use_output_schema_with_tools(llm: Option<&dyn Llm>) -> bool {
57    let Some(llm) = llm else {
58        return false;
59    };
60    is_gemini_model(llm.name())
61        && google_variant(llm) == Some(Backend::VertexAi)
62        && is_gemini2_or_above(llm.name())
63}
64
65/// Whether `llm` is served by the Gemini API (not Vertex AI).
66pub fn is_gemini_api_variant(llm: &dyn Llm) -> bool {
67    google_variant(llm) == Some(Backend::GeminiApi)
68}
69
70/// Whether `llm` is a Gemini model that needs the `set_model_response` tool workaround
71/// because it does not support output schema with tools natively.
72pub fn needs_output_schema_processor(llm: Option<&dyn Llm>) -> bool {
73    let Some(inner) = llm else {
74        return false;
75    };
76    is_gemini_model(inner.name()) && !can_use_output_schema_with_tools(llm)
77}
78
79/// Whether `model` names a Gemini 3.x Live model other than Live
80/// Translate (adk-python `_is_gemini_3_x_live`).
81pub fn is_gemini_3x_live(model: &str) -> bool {
82    let name = extract_model_name(model);
83    name.starts_with("gemini-3.") && name.contains("-live") && !is_gemini_35_live_translate(model)
84}
85
86/// Whether `model` names a Gemini 3.5 Live Translate model.
87pub fn is_gemini_35_live_translate(model: &str) -> bool {
88    extract_model_name(model).starts_with("gemini-3.5-live-translate")
89}
90
91fn extract_gemini_version(model: &str) -> Option<f64> {
92    let name = extract_model_name(model);
93    let re = GEMINI_MODEL_VERSION_REGEX.as_ref()?;
94    let caps = re.captures(&name)?;
95    caps.get(1)?.as_str().parse::<f64>().ok()
96}
97
98fn extract_model_name(model: &str) -> String {
99    model.rsplit('/').next().unwrap_or(model).to_lowercase()
100}