Skip to content

Model Client

Gunpal Jain edited this page Nov 15, 2026 · 4 revisions

ModelClient reads model metadata from the Models API. It can list models, get one model's details (token limits, supported methods, thinking support), update or delete tuned models, and tokenize content on Vertex AI.

Package: Google_GenerativeAI. Namespace: GenerativeAI.Clients.

Getting a ModelClient

GoogleAi and VertexAI (through their GenAI base class) expose a ready-made ModelClient property and the ListModelsAsync / GetModelAsync shortcuts. You can also create the client from a platform adapter:

using GenerativeAI;
using GenerativeAI.Clients;
using GenerativeAI.Types;

var googleAI = new GoogleAi("YOUR_API_KEY");
ModelClient modelClient = googleAI.ModelClient;

// Or standalone
var modelClient2 = new ModelClient(new GoogleAIPlatformAdapter("YOUR_API_KEY"));
var vertexModelClient = new ModelClient(new VertextPlatformAdapter("your-project-id", "global"));

1. Getting Model Information (GetModelAsync)

Model model = await modelClient.GetModelAsync(GoogleAIModels.Gemini38Flash); // "models/gemini-3.8-flash"

Console.WriteLine($"Name: {model.Name}, Version: {model.Version}");
Console.WriteLine($"Input limit: {model.InputTokenLimit}, Output limit: {model.OutputTokenLimit}");
Console.WriteLine($"Methods: {string.Join(", ", model.SupportedGenerationMethods ?? new())}");
Console.WriteLine($"Thinking: {model.Thinking}");

// Shortcut on GoogleAi / VertexAI
var sameModel = await googleAI.GetModelAsync(GoogleAIModels.Gemini38Flash);

You can pass the model name with or without the models/ prefix.

2. Listing Models (ListModelsAsync)

ListModelsResponse response = await modelClient.ListModelsAsync(pageSize: 20);

foreach (var m in response.Models ?? new List<Model>())
{
    Console.WriteLine($"{m.Name} - {m.DisplayName}");
}

// Next page
if (!string.IsNullOrEmpty(response.NextPageToken))
{
    var next = await modelClient.ListModelsAsync(pageSize: 20, pageToken: response.NextPageToken);
}

// Shortcut on GoogleAi / VertexAI
var models = await googleAI.ListModelsAsync(pageSize: 20);

3. Updating a Tuned Model (UpdateModelAsync)

Updates the display name, description or default checkpoint of a tuned model. Only the fields you set are sent.

Model updated = await modelClient.UpdateModelAsync("tunedModels/my-tuned-model", new UpdateModelConfig
{
    DisplayName = "Support bot v2",
    Description = "Tuned on Q3 support tickets"
});

Model names are tunedModels/{id} on the Gemini API, and models/{id} or projects/{project}/locations/{location}/models/{id} on Vertex AI.

4. Deleting a Tuned Model (DeleteModelAsync)

await modelClient.DeleteModelAsync("tunedModels/my-tuned-model");

5. Tokenizing Content (ComputeTokensAsync, Vertex AI only)

Returns the tokens and token IDs for each content. On the Gemini API it throws NotSupportedException. To count tokens on either platform, use GenerativeModel.CountTokensAsync (see Generate Content).

var vertex = new VertexAI("your-project-id", "global");
ComputeTokensResponse tokens = await vertex.ModelClient.ComputeTokensAsync(
    VertexAIModels.Gemini.Gemini38Flash,
    new[] { new Content("Hello world", Roles.User) });

foreach (var info in tokens.TokensInfo ?? new List<TokensInfo>())
{
    Console.WriteLine($"{info.Role}: {string.Join(",", info.TokenIds ?? new List<long>())}");
}

Important Considerations

  1. Model names: On the Gemini API, model resource names look like models/gemini-3.8-flash. Use the constants in GoogleAIModels and VertexAIModels.Gemini.
  2. Pagination: Use pageSize and pageToken to page through long lists.
  3. Errors: Failed calls throw GenerativeAI.Exceptions.ApiException (ErrorCode, ErrorStatus, ErrorMessage).
  4. Authentication: ModelClient uses the credentials of its platform adapter. See Authentication.

Related Pages

API Reference

Clone this wiki locally