Skip to content

Revert "refactor(genai): migrate region tags from generative-ai to genai folder (Batch 1)"#4339

Merged
iennae merged 3 commits into
mainfrom
revert-generative-ai
Jun 9, 2026
Merged

Revert "refactor(genai): migrate region tags from generative-ai to genai folder (Batch 1)"#4339
iennae merged 3 commits into
mainfrom
revert-generative-ai

Conversation

@angelcaamal

Copy link
Copy Markdown
Contributor

Description

Fixes #

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

Note: Any check with (dev), (experimental), or (legacy) can be ignored and should not block your PR from merging (see CI testing).

@product-auto-label product-auto-label Bot added samples Issues that are directly related to samples. api: genai labels Jun 9, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request cleans up region tags in existing files and introduces a new suite of Node.js code snippets and tests under generative-ai/snippets/ for various Vertex AI Gemini features. The review feedback highlights several improvement opportunities: resolving a logical bug in safetySettings.js where a safety termination message is printed unconditionally, using CAIP_PROJECT_ID consistently in tests, simplifying assertions in gemini-translate.test.js by avoiding unnecessary stringification, and applying optional chaining across multiple snippets to safely access nested API response properties and prevent potential runtime crashes.

Comment on lines +62 to +70
for await (const item of responseStream.stream) {
if (item.candidates[0].finishReason === 'SAFETY') {
console.log('This response stream terminated due to safety concerns.');
break;
} else {
process.stdout.write(item.candidates[0].content.parts[0].text);
}
}
console.log('This response stream terminated due to safety concerns.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

There is a logical bug in this sample: the message 'This response stream terminated due to safety concerns.' is printed unconditionally at the end of the function, even if the stream completes successfully without any safety triggers. Additionally, accessing item.candidates[0] directly without checking if candidates exists can lead to a TypeError if the response is completely blocked or empty. We can resolve both issues by using a boolean flag to track safety termination and applying optional chaining for robust property access.

  let safetyBlocked = false;
  for await (const item of responseStream.stream) {
    if (item.candidates?.[0]?.finishReason === 'SAFETY') {
      safetyBlocked = true;
      break;
    } else {
      process.stdout.write(item.candidates?.[0]?.content?.parts?.[0]?.text ?? '');
    }
  }
  if (safetyBlocked) {
    console.log('\nThis response stream terminated due to safety concerns.');
  } else {
    console.log();
  }

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const projectId = process.env.GOOGLE_SAMPLES_PROJECT;
const location = process.env.LOCATION;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

This test uses process.env.GOOGLE_SAMPLES_PROJECT to retrieve the project ID, whereas all other 16 test files in this suite consistently use process.env.CAIP_PROJECT_ID. To prevent potential test failures in CI environments where GOOGLE_SAMPLES_PROJECT is not set, please use CAIP_PROJECT_ID for consistency.

Suggested change
const location = process.env.LOCATION;
const projectId = process.env.CAIP_PROJECT_ID;
References
  1. Maintain established boilerplate patterns across samples within a repository for consistency.

`node ./gemini-translate.js ${projectId} ${location} ${model}`
);

assert(JSON.stringify(response).match(/Bonjour/));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Calling JSON.stringify(response) on a string before matching it with a regular expression is unnecessary and can introduce unexpected formatting characters. Since assert is imported from Node's strict assertion module, you can use assert.match directly on the string response.

Suggested change
assert(JSON.stringify(response).match(/Bonjour/));
assert.match(response, /Bonjour/);

Comment on lines +61 to +62
const fullTextResponse =
response.response.candidates[0].content.parts[0].text;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To prevent potential runtime crashes (e.g., TypeError: Cannot read properties of undefined) if the API response is empty or blocked by safety filters, use optional chaining and a fallback value when accessing deeply nested properties.

Suggested change
const fullTextResponse =
response.response.candidates[0].content.parts[0].text;
const fullTextResponse =
response.response?.candidates?.[0]?.content?.parts?.[0]?.text ?? '';

Comment on lines +58 to +59
const fullTextResponse =
aggregatedResponse.candidates[0].content.parts[0].text;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To prevent potential runtime crashes (e.g., TypeError: Cannot read properties of undefined) if the API response is empty or blocked by safety filters, use optional chaining and a fallback value when accessing deeply nested properties.

Suggested change
const fullTextResponse =
aggregatedResponse.candidates[0].content.parts[0].text;
const fullTextResponse =
aggregatedResponse.candidates?.[0]?.content?.parts?.[0]?.text ?? '';

Comment on lines +40 to +42
for await (const item of result1.stream) {
console.log(item.candidates[0].content.parts[0].text);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To prevent potential runtime crashes (e.g., TypeError: Cannot read properties of undefined) if the API response is empty or blocked by safety filters, use optional chaining and a fallback value when accessing deeply nested properties.

Suggested change
for await (const item of result1.stream) {
console.log(item.candidates[0].content.parts[0].text);
}
for await (const item of result1.stream) {
console.log(item.candidates?.[0]?.content?.parts?.[0]?.text ?? '');
}

Comment on lines +47 to +49
for await (const item of responseStream.stream) {
process.stdout.write(item.candidates[0].content.parts[0].text);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To prevent potential runtime crashes (e.g., TypeError: Cannot read properties of undefined) if the API response is empty or blocked by safety filters, use optional chaining and a fallback value when accessing deeply nested properties.

Suggested change
for await (const item of responseStream.stream) {
process.stdout.write(item.candidates[0].content.parts[0].text);
}
for await (const item of responseStream.stream) {
process.stdout.write(item.candidates?.[0]?.content?.parts?.[0]?.text ?? '');
}

@angelcaamal angelcaamal marked this pull request as ready for review June 9, 2026 23:04
@angelcaamal angelcaamal requested review from a team as code owners June 9, 2026 23:04
@snippet-bot

snippet-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 10 region tags.
You are about to delete 10 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@angelcaamal angelcaamal changed the title Revert generative ai samples refactor(genai): migrate region tags from generative-ai to genai folder (Batch 1) Jun 9, 2026
@angelcaamal angelcaamal changed the title refactor(genai): migrate region tags from generative-ai to genai folder (Batch 1) Revert "refactor(genai): migrate region tags from generative-ai to genai folder (Batch 1)" Jun 9, 2026
@iennae iennae merged commit e29c4b7 into main Jun 9, 2026
35 of 37 checks passed
@iennae iennae deleted the revert-generative-ai branch June 9, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: genai samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants