Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/ai/types/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class Client {
}

/// List the model IDs the provider serves to this API key, by querying its
/// models endpoint (`GET /v1/models`). Makes a network request.
/// models endpoint (`GET /v1/models`). Makes a network request. Where the
/// provider says what a model is for (Gemini: `supportedGenerationMethods`),
/// the models that cannot be asked through this client are left out.
virtual ModelListResult list_models() {
if (pimpl_)
return pimpl_->list_models();
Expand Down
18 changes: 17 additions & 1 deletion src/providers/google/google_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "google_response_parser.h"
#include "google_stream.h"

#include <algorithm>
#include <cstdio>
#include <exception>
#include <memory>
Expand Down Expand Up @@ -132,7 +133,12 @@ std::string percent_encode(const std::string& value) {
ModelListResult GoogleClient::list_models() {
// `GET /v1beta/models` returns `{"models": [{"name": "models/<id>", ...}],
// "nextPageToken": ...}`; the IDs are reported without the `models/`
// prefix, the way requests take them.
// prefix, the way requests take them. The endpoint lists every model of the
// API: the ones for embeddings, for images and video, and for the Live API
// too. This client speaks `generateContent`, so a model that says which
// methods it serves (`supportedGenerationMethods`) and does not name that
// one is left out. A model that does not say is listed.
constexpr std::string_view kGenerateContentMethod = "generateContent";
constexpr std::string_view kNamePrefix = "models/";
const auto headers = request_builder_->build_headers(config_);
ModelListResult listed;
Expand Down Expand Up @@ -165,6 +171,16 @@ ModelListResult GoogleClient::list_models() {
!entry["name"].is_string()) {
continue;
}
if (const auto methods = entry.find("supportedGenerationMethods");
methods != entry.end() && methods->is_array() &&
std::none_of(methods->begin(), methods->end(),
[&](const nlohmann::json& method) {
return method.is_string() &&
method.get<std::string_view>() ==
kGenerateContentMethod;
})) {
continue;
}
std::string name = entry["name"].get<std::string>();
if (name.starts_with(kNamePrefix)) {
name.erase(0, kNamePrefix.size());
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/list_models_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,43 @@ TEST(ListModelsTest, GoogleFollowsPageTokensAndStripsPrefix) {
EXPECT_EQ(seen_api_key, "gemini-test");
}

TEST(ListModelsTest, GoogleListsOnlyModelsThatGenerateContent) {
LocalServer server([&](httplib::Server& s) {
s.Get("/v1beta/models", [&](const httplib::Request&,
httplib::Response& res) {
res.set_content(
R"({"models":[)"
R"({"name":"models/gemini-embedding-001",)"
R"("supportedGenerationMethods":["embedContent","countTokens"]},)"
R"({"name":"models/gemini-4-hydrogen",)"
R"("supportedGenerationMethods":["generateContent","countTokens"]},)"
R"({"name":"models/imagen-4.0-generate-001",)"
R"("supportedGenerationMethods":["predict"]},)"
R"({"name":"models/gemini-live-preview",)"
R"("supportedGenerationMethods":["bidiGenerateContent"]},)"
R"({"name":"models/says-nothing"},)"
R"({"name":"models/lists-nothing",)"
R"("supportedGenerationMethods":[]},)"
R"({"name":"models/not-a-list",)"
R"("supportedGenerationMethods":"generateContent"},)"
R"({"name":"models/gemini-3.8-flash",)"
R"("supportedGenerationMethods":["countTokens","generateContent"]})"
R"(]})",
"application/json");
});
});

auto client = google::create_client("gemini-test", server.url());
const auto result = client.list_models();

ASSERT_TRUE(result.is_success()) << result.error_message();
// A model is left out only when it says what it serves and that is not
// `generateContent`.
EXPECT_THAT(result.models,
testing::ElementsAre("gemini-4-hydrogen", "says-nothing",
"not-a-list", "gemini-3.8-flash"));
}

TEST(ListModelsTest, ReportsProviderErrors) {
LocalServer server([](httplib::Server& s) {
s.Get("/v1/models", [](const httplib::Request&, httplib::Response& res) {
Expand Down
Loading