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
85 changes: 85 additions & 0 deletions genai/batch-prediction/batchpredict-embeddings-with-gcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_batchpredict_embeddings_with_gcs]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
const OUTPUT_URI = 'gs://your-bucket/your-prefix';

async function runBatchPredictionJob(
outputUri = OUTPUT_URI,
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
httpOptions: {
apiVersion: 'v1',
},
});

// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
let job = await client.batches.create({
model: 'text-embedding-005',
// Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl
src: 'gs://cloud-samples-data/generative-ai/embeddings/embeddings_input.jsonl',
config: {
dest: outputUri,
},
});

console.log(`Job name: ${job.name}`);
console.log(`Job state: ${job.state}`);

// Example response:
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
// Job state: JOB_STATE_PENDING

const completedStates = new Set([
'JOB_STATE_SUCCEEDED',
'JOB_STATE_FAILED',
'JOB_STATE_CANCELLED',
'JOB_STATE_PAUSED',
]);

while (completedStates.has(job.state)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The polling loop condition is inverted. It currently checks while (completedStates.has(job.state)), which means the loop will only run if the job is already in a completed state. Since the initial state is JOB_STATE_PENDING, the loop will not execute at all, and the function will return prematurely. The condition should be negated to poll until the job reaches a completed state.

Suggested change
while (completedStates.has(job.state)) {
while (!completedStates.has(job.state)) {

await new Promise(resolve => setTimeout(resolve, 30000));
job = await client.batches.get({name: job.name});
console.log(`Job state: ${job.state}`);
if (job.state === 'JOB_STATE_FAILED') {
console.log(`Job state: ${job.state}`);
break;
}
Comment on lines +66 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This if block is redundant. The console.log duplicates the one on the preceding line, and the break is unnecessary if the main loop condition is corrected, as JOB_STATE_FAILED is a completed state that will terminate the loop.

}

// Example response:
// Job state: JOB_STATE_PENDING
// Job state: JOB_STATE_RUNNING
// Job state: JOB_STATE_RUNNING
// ...
// Job state: JOB_STATE_SUCCEEDED

return job.state;
}
// [END googlegenaisdk_batchpredict_embeddings_with_gcs]

module.exports = {
runBatchPredictionJob,
};
82 changes: 82 additions & 0 deletions genai/batch-prediction/batchpredict-with-bq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_batchpredict_with_bq]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
const OUTPUT_URI = 'bq://your-project.your_dataset.your_table';

async function runBatchPredictionJob(
outputUri = OUTPUT_URI,
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
httpOptions: {
apiVersion: 'v1',
},
});

// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
let job = await client.batches.create({
// To use a tuned model, set the model param to your tuned model using the following format:
// model="projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}"
model: 'gemini-2.5-flash',
src: 'bq://storage-samples.generative_ai.batch_requests_for_multimodal_input',
config: {
dest: outputUri,
},
});

console.log(`Job name: ${job.name}`);
console.log(`Job state: ${job.state}`);

// Example response:
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
// Job state: JOB_STATE_PENDING

const completedStates = new Set([
'JOB_STATE_SUCCEEDED',
'JOB_STATE_FAILED',
'JOB_STATE_CANCELLED',
'JOB_STATE_PAUSED',
]);

while (completedStates.has(job.state)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The polling loop condition is inverted. It currently checks while (completedStates.has(job.state)), which means the loop will only run if the job is already in a completed state. Since the initial state is JOB_STATE_PENDING, the loop will not execute at all, and the function will return prematurely. The condition should be negated to poll until the job reaches a completed state.

Suggested change
while (completedStates.has(job.state)) {
while (!completedStates.has(job.state)) {

await new Promise(resolve => setTimeout(resolve, 30000));
job = await client.batches.get({name: job.name});
console.log(`Job state: ${job.state}`);
}

// Example response:
// Job state: JOB_STATE_PENDING
// Job state: JOB_STATE_RUNNING
// Job state: JOB_STATE_RUNNING
// ...
// Job state: JOB_STATE_SUCCEEDED

return job.state;
}
// [END googlegenaisdk_batchpredict_with_bq]

module.exports = {
runBatchPredictionJob,
};
83 changes: 83 additions & 0 deletions genai/batch-prediction/batchpredict-with-gcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START googlegenaisdk_batchpredict_with_gcs]
const {GoogleGenAI} = require('@google/genai');

const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION || 'global';
const OUTPUT_URI = 'gs://your-bucket/your-prefix';

async function runBatchPredictionJob(
outputUri = OUTPUT_URI,
projectId = GOOGLE_CLOUD_PROJECT,
location = GOOGLE_CLOUD_LOCATION
) {
const client = new GoogleGenAI({
vertexai: true,
project: projectId,
location: location,
httpOptions: {
apiVersion: 'v1',
},
});

// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create
let job = await client.batches.create({
// To use a tuned model, set the model param to your tuned model using the following format:
// model="projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}"
model: 'gemini-2.5-flash',
// Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl
src: 'gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl',
config: {
dest: outputUri,
},
});

console.log(`Job name: ${job.name}`);
console.log(`Job state: ${job.state}`);

// Example response:
// Job name: projects/%PROJECT_ID%/locations/us-central1/batchPredictionJobs/9876453210000000000
// Job state: JOB_STATE_PENDING

const completedStates = new Set([
'JOB_STATE_SUCCEEDED',
'JOB_STATE_FAILED',
'JOB_STATE_CANCELLED',
'JOB_STATE_PAUSED',
]);

while (completedStates.has(job.state)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The polling loop condition is inverted. It currently checks while (completedStates.has(job.state)), which means the loop will only run if the job is already in a completed state. Since the initial state is JOB_STATE_PENDING, the loop will not execute at all, and the function will return prematurely. The condition should be negated to poll until the job reaches a completed state.

Suggested change
while (completedStates.has(job.state)) {
while (!completedStates.has(job.state)) {

await new Promise(resolve => setTimeout(resolve, 30000));
job = await client.batches.get({name: job.name});
console.log(`Job state: ${job.state}`);
}

// Example response:
// Job state: JOB_STATE_PENDING
// Job state: JOB_STATE_RUNNING
// Job state: JOB_STATE_RUNNING
// ...
// Job state: JOB_STATE_SUCCEEDED

return job.state;
}
// [END googlegenaisdk_batchpredict_with_gcs]

module.exports = {
runBatchPredictionJob,
};
2 changes: 2 additions & 0 deletions genai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"test": "c8 mocha -p -j 2 --timeout 2400000 test/*.test.js test/**/*.test.js"
},
"dependencies": {
"@google-cloud/bigquery": "^8.1.1",
"@google-cloud/storage": "^7.17.0",
"@google/genai": "1.12.0",
"axios": "^1.6.2",
"luxon": "^3.7.1",
Expand Down
59 changes: 59 additions & 0 deletions genai/test/batchpredict-embeddings-with-gcs.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const {Storage} = require('@google-cloud/storage');

const storage = new Storage();

const GCS_OUTPUT_BUCKET = 'nodejs-docs-samples-tests';

const projectId = process.env.CAIP_PROJECT_ID;
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
const sample = require('../batch-prediction/batchpredict-embeddings-with-gcs');

async function gcs_output_uri() {
const dt = new Date();
const prefix = `text_output/${dt.toISOString()}`;
const fullUri = `gs://${GCS_OUTPUT_BUCKET}/${prefix}`;

return {
uri: fullUri,
async cleanup() {
const [files] = await storage.bucket(GCS_OUTPUT_BUCKET).getFiles({
prefix,
});
for (const file of files) {
await file.delete();
}
},
};
}

describe('batchpredict-embeddings-with-gcs', () => {
it('should return the batch job state', async function () {
this.timeout(50000);
const gscOutput = gcs_output_uri();
const gscUri = (await gscOutput).uri;
const output = await sample.runBatchPredictionJob(
gscUri,
projectId,
location
);
assert.notEqual(output, undefined);
});
Comment on lines +48 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test has two issues:

  1. It doesn't clean up the GCS resources it creates, which can lead to leftover artifacts in the test bucket. Using a try...finally block will ensure cleanup happens even if the test fails.
  2. The assertion assert.notEqual(output, undefined) is too weak. With the bug in the sample file, it would pass by returning JOB_STATE_PENDING. A better assertion is to check for JOB_STATE_SUCCEEDED to confirm the job completed successfully.

Also, there's a minor typo gscOutput which could be gcsOutput for consistency.

  it('should return the batch job state', async function () {
    this.timeout(50000);
    const gcsOutput = await gcs_output_uri();
    try {
      const output = await sample.runBatchPredictionJob(
        gcsOutput.uri,
        projectId,
        location
      );
      assert.strictEqual(output, 'JOB_STATE_SUCCEEDED');
    } finally {
      await gcsOutput.cleanup();
    }
  });

});
57 changes: 57 additions & 0 deletions genai/test/batchpredict-with-bq.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const {BigQuery} = require('@google-cloud/bigquery');

const bigquery = new BigQuery();

const BQ_OUTPUT_DATASET = `${process.env.BQ_OUTPUT_DATASET}.gen_ai_batch_prediction`;

const projectId = process.env.CAIP_PROJECT_ID;
const location = process.env.GOOGLE_CLOUD_LOCATION || 'global';
const sample = require('../batch-prediction/batchpredict-with-bq');

async function bq_output_uri() {
const dt = new Date();
const tableName = `text_output_${dt.getFullYear()}_${dt.getMonth() + 1}_${dt.getDate()}_T${dt.getHours()}_${dt.getMinutes()}_${dt.getSeconds()}`;
const tableUri = `${BQ_OUTPUT_DATASET}.${tableName}`;
const fullUri = `bq://${tableUri}`;

return {
uri: fullUri,
async cleanup() {
await bigquery.dataset(BQ_OUTPUT_DATASET).table(tableName).delete({
ignoreNotFound: true,
});
},
};
}

describe('batchpredict-with-bq', () => {
it('should return the batch job state', async function () {
this.timeout(50000);
const bqOutput = bq_output_uri();
const bqUri = (await bqOutput).uri;
const output = await sample.runBatchPredictionJob(
bqUri,
projectId,
location
);
assert.notEqual(output, undefined);
});
Comment on lines +46 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This test has two issues:

  1. It doesn't clean up the BigQuery table it creates, which can lead to leftover artifacts. Using a try...finally block will ensure cleanup happens even if the test fails.
  2. The assertion assert.notEqual(output, undefined) is too weak. With the bug in the sample file, it would pass by returning JOB_STATE_PENDING. A better assertion is to check for JOB_STATE_SUCCEEDED to confirm the job completed successfully.
  it('should return the batch job state', async function () {
    this.timeout(50000);
    const bqOutput = await bq_output_uri();
    try {
      const output = await sample.runBatchPredictionJob(
        bqOutput.uri,
        projectId,
        location
      );
      assert.strictEqual(output, 'JOB_STATE_SUCCEEDED');
    } finally {
      await bqOutput.cleanup();
    }
  });

});
Loading