diff --git a/include/ai/types/client.h b/include/ai/types/client.h index 3cb536a..7fc071f 100644 --- a/include/ai/types/client.h +++ b/include/ai/types/client.h @@ -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(); diff --git a/src/providers/google/google_client.cpp b/src/providers/google/google_client.cpp index a5b3ae3..6dea660 100644 --- a/src/providers/google/google_client.cpp +++ b/src/providers/google/google_client.cpp @@ -6,6 +6,7 @@ #include "google_response_parser.h" #include "google_stream.h" +#include #include #include #include @@ -132,7 +133,12 @@ std::string percent_encode(const std::string& value) { ModelListResult GoogleClient::list_models() { // `GET /v1beta/models` returns `{"models": [{"name": "models/", ...}], // "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; @@ -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() == + kGenerateContentMethod; + })) { + continue; + } std::string name = entry["name"].get(); if (name.starts_with(kNamePrefix)) { name.erase(0, kNamePrefix.size()); diff --git a/tests/unit/list_models_test.cpp b/tests/unit/list_models_test.cpp index 17a1fea..2cc0c57 100644 --- a/tests/unit/list_models_test.cpp +++ b/tests/unit/list_models_test.cpp @@ -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) {