Repository navigation
Model Client
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.
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"));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.
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);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.
await modelClient.DeleteModelAsync("tunedModels/my-tuned-model");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>())}");
}-
Model names: On the Gemini API, model resource names look like
models/gemini-3.8-flash. Use the constants inGoogleAIModelsandVertexAIModels.Gemini. -
Pagination: Use
pageSizeandpageTokento page through long lists. -
Errors: Failed calls throw
GenerativeAI.Exceptions.ApiException(ErrorCode,ErrorStatus,ErrorMessage). -
Authentication:
ModelClientuses the credentials of its platform adapter. See Authentication.
Getting Started
- Initialization
- Authentication
- Configuration & Error Handling
- Supported Models
- Migration Guide (v4)
- Release Notes (v4)
Core Features
- Generate Content
- Chat Session
- Streaming
- Multimodal Features
- JSON Mode
- Function Calling
- Embeddings
- Context Caching
Gemini APIs
- Interactions API
- Multimodal Live API
- File Search
- Batch API
- Model Tuning
- Webhooks
- Agent APIs
- Image Generation
- Video Generation
- Speech Generation
- Files Client
- Model Client
Vertex AI
Integrations
Guides
Legacy