Skip to content

Commit 1ada1a0

Browse files
authored
fix(aiplatform): update Gemini model from 2.0-flash to gemini-2.5-flash (#3342)
# fix(aiplatform): update Gemini models and rewrite image generation sample ## Description This PR updates the model references and APIs used across the .NET AI Platform samples (`aiplatform/api/AIPlatform.Samples`) and integration tests to align with current Vertex AI standards: - **Gemini Models Update:** Upgrades text and multimodal model references across all samples and tests from `gemini-2.0-flash-001` to `gemini-2.5-flash`. - **Image Sample Refactor:** Rewrites `GenerateImage.cs` to use the modern Generative AI API (`GenerateContentAsync`) and the `gemini-2.5-flash-image` model. This replaces the legacy `PredictAsync` API pattern and JSON-encoded structures. **Reason for Update:** - `gemini-2.0-flash-001` reached end-of-life and was decommissioned on June 1, 2026. - The legacy `imagegeneration@006` model was also decommissioned, causing `NotFound` RPC errors in CI (`GenerateImageTest.TestGenerateImageAsync`). Migrating to `gemini-2.5-flash-image` via the `GenerateContent` API ensures active service availability and fixes the CI pipeline. Fixes Internal: b/542706371
1 parent 51253e4 commit 1ada1a0

20 files changed

Lines changed: 46 additions & 45 deletions

aiplatform/api/AIPlatform.Samples/AudioInputSummarization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<string> SummarizeAudio(
2626
string projectId = "your-project-id",
2727
string location = "us-central1",
2828
string publisher = "google",
29-
string model = "gemini-2.0-flash-001")
29+
string model = "gemini-2.5-flash")
3030
{
3131
var predictionServiceClient = new PredictionServiceClientBuilder
3232
{

aiplatform/api/AIPlatform.Samples/AudioInputTranscription.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<string> TranscribeAudio(
2626
string projectId = "your-project-id",
2727
string location = "us-central1",
2828
string publisher = "google",
29-
string model = "gemini-2.0-flash-001")
29+
string model = "gemini-2.5-flash")
3030
{
3131

3232
var predictionServiceClient = new PredictionServiceClientBuilder

aiplatform/api/AIPlatform.Samples/ContextCaching/CreateContextCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task<CachedContentName> Create(string projectId)
3535
Parent = $"projects/{projectId}/locations/us-central1",
3636
CachedContent = new CachedContent
3737
{
38-
Model = $"projects/{projectId}/locations/us-central1/publishers/google/models/gemini-2.0-flash-001",
38+
Model = $"projects/{projectId}/locations/us-central1/publishers/google/models/gemini-2.5-flash",
3939
SystemInstruction = new Content
4040
{
4141
Parts =

aiplatform/api/AIPlatform.Samples/ContextCaching/UseContextCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task<string> Use(string projectId, CachedContentName name)
3232
var generateContentRequest = new GenerateContentRequest
3333
{
3434
CachedContentAsCachedContentName = name,
35-
Model = $"projects/{projectId}/locations/us-central1/publishers/google/models/gemini-2.0-flash-001",
35+
Model = $"projects/{projectId}/locations/us-central1/publishers/google/models/gemini-2.5-flash",
3636
Contents =
3737
{
3838
new Content

aiplatform/api/AIPlatform.Samples/ControlledGeneration.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<string> GenerateContentWithResponseMimeType(
2626
string projectId = "your-project-id",
2727
string location = "us-central1",
2828
string publisher = "google",
29-
string model = "gemini-2.0-flash-001")
29+
string model = "gemini-2.5-flash")
3030
{
3131

3232
var predictionServiceClient = new PredictionServiceClientBuilder
@@ -73,7 +73,7 @@ public async Task<string> GenerateContentWithResponseSchema(
7373
string projectId = "your-project-id",
7474
string location = "us-central1",
7575
string publisher = "google",
76-
string model = "gemini-2.0-flash-001")
76+
string model = "gemini-2.5-flash")
7777
{
7878

7979
var predictionServiceClient = new PredictionServiceClientBuilder
@@ -130,7 +130,7 @@ public async Task<string> GenerateContentWithResponseSchema2(
130130
string projectId = "your-project-id",
131131
string location = "us-central1",
132132
string publisher = "google",
133-
string model = "gemini-2.0-flash-001")
133+
string model = "gemini-2.5-flash")
134134
{
135135

136136
var predictionServiceClient = new PredictionServiceClientBuilder
@@ -194,7 +194,7 @@ public async Task<string> GenerateContentWithResponseSchema3(
194194
string projectId = "your-project-id",
195195
string location = "us-central1",
196196
string publisher = "google",
197-
string model = "gemini-2.0-flash-001")
197+
string model = "gemini-2.5-flash")
198198
{
199199

200200
var predictionServiceClient = new PredictionServiceClientBuilder
@@ -270,7 +270,7 @@ public async Task<string> GenerateContentWithResponseSchema4(
270270
string projectId = "your-project-id",
271271
string location = "us-central1",
272272
string publisher = "google",
273-
string model = "gemini-2.0-flash-001")
273+
string model = "gemini-2.5-flash")
274274
{
275275

276276
var predictionServiceClient = new PredictionServiceClientBuilder
@@ -362,7 +362,7 @@ public async Task<string> GenerateContentWithResponseSchema6(
362362
string projectId = "your-project-id",
363363
string location = "us-central1",
364364
string publisher = "google",
365-
string model = "gemini-2.0-flash-001")
365+
string model = "gemini-2.5-flash")
366366
{
367367

368368
var predictionServiceClient = new PredictionServiceClientBuilder

aiplatform/api/AIPlatform.Samples/FunctionCalling.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task<string> GenerateFunctionCall(
2828
string projectId = "your-project-id",
2929
string location = "us-central1",
3030
string publisher = "google",
31-
string model = "gemini-2.0-flash-001")
31+
string model = "gemini-2.5-flash")
3232
{
3333
var predictionServiceClient = new PredictionServiceClientBuilder
3434
{

aiplatform/api/AIPlatform.Samples/GeminiQuickstart.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<string> GenerateContent(
2727
string projectId = "your-project-id",
2828
string location = "us-central1",
2929
string publisher = "google",
30-
string model = "gemini-2.0-flash-001"
30+
string model = "gemini-2.5-flash"
3131
)
3232
{
3333
// Create client

aiplatform/api/AIPlatform.Samples/GenerateImage.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using System.IO;
2222
using System.Linq;
2323
using System.Threading.Tasks;
24-
using Value = Google.Protobuf.WellKnownTypes.Value;
2524

2625
public class GenerateImage
2726
{
@@ -33,35 +32,37 @@ public async Task<FileInfo> Generate(
3332
Endpoint = "us-central1-aiplatform.googleapis.com"
3433
}.Build();
3534

36-
3735
string prompt = "a dog reading a newspaper";
3836
string outputFileName = "dog_newspaper.png";
39-
string model = "imagegeneration@006";
37+
string location = "us-central1";
38+
string model = "gemini-2.5-flash-image";
4039

41-
var predictRequest = new PredictRequest
40+
var generateContentRequest = new GenerateContentRequest
4241
{
43-
EndpointAsEndpointName = EndpointName.FromProjectLocationPublisherModel(projectId, "us-central1", "google", model),
44-
Instances =
42+
Model = $"projects/{projectId}/locations/{location}/publishers/google/models/{model}",
43+
Contents =
4544
{
46-
Value.ForStruct(new()
45+
new Content
4746
{
48-
Fields =
49-
{
50-
["prompt"] = Value.ForString(prompt)
47+
Role = "USER",
48+
Parts =
49+
{
50+
new Part { Text = prompt }
5151
}
52-
})
53-
},
54-
Parameters = Value.ForStruct(new()
55-
{
56-
Fields =
57-
{
58-
["sampleCount"] = Value.ForNumber(1)
5952
}
60-
})
53+
}
6154
};
6255

63-
PredictResponse response = await predictionServiceClient.PredictAsync(predictRequest);
64-
byte[] imageBytes = Convert.FromBase64String(response.Predictions.First().StructValue.Fields["bytesBase64Encoded"].StringValue);
56+
GenerateContentResponse response = await predictionServiceClient.GenerateContentAsync(generateContentRequest);
57+
58+
var imagePart = response.Candidates.FirstOrDefault()?.Content?.Parts?.FirstOrDefault(p => p.InlineData != null);
59+
60+
if (imagePart == null)
61+
{
62+
throw new Exception("No image data was returned by the model.");
63+
}
64+
65+
byte[] imageBytes = imagePart.InlineData.Data.ToByteArray();
6566

6667
File.WriteAllBytes(outputFileName, imageBytes);
6768
FileInfo fileInfo = new FileInfo(Path.GetFullPath(outputFileName));
@@ -71,4 +72,4 @@ public async Task<FileInfo> Generate(
7172
}
7273
}
7374

74-
// [END generativeaionvertexai_imagen_generate_image]
75+
// [END generativeaionvertexai_imagen_generate_image]

aiplatform/api/AIPlatform.Samples/GetTokenCount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task<int> CountTokens(
2727
string projectId = "your-project-id",
2828
string location = "us-central1",
2929
string publisher = "google",
30-
string model = "gemini-2.0-flash-001"
30+
string model = "gemini-2.5-flash"
3131
)
3232
{
3333
var client = new LlmUtilityServiceClientBuilder

aiplatform/api/AIPlatform.Samples/GroundingVertexAiSearchSample.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public async Task<string> GenerateTextWithVertexAiSearch(
2626
string projectId = "your-project-id",
2727
string location = "us-central1",
2828
string publisher = "google",
29-
string model = "gemini-2.0-flash-001",
29+
string model = "gemini-2.5-flash",
3030
string dataStoreLocation = "global",
3131
string dataStoreId = "your-datastore-id")
3232
{

0 commit comments

Comments
 (0)