Skip to content

Commit 3f631be

Browse files
MarkDaoustcopybara-github
authored andcommitted
fix: fix examples
PiperOrigin-RevId: 966104883
1 parent 4c5208b commit 3f631be

85 files changed

Lines changed: 714 additions & 582 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/format.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ jobs:
1919
node-version: 22.x
2020
cache: 'npm'
2121
- run: USE_LOCAL_BUILD=true npm ci
22-
- name: Run format
23-
run: npx prettier '**/*.ts' '**/*.mjs' '**/*.mjs' '**/*.json' --check
24-
- name: Run linter
25-
run: npm run lint
22+
- name: Check code formatting (run 'npm run format' to fix)
23+
run: |
24+
if ! npx prettier '**/*.ts' '**/*.mjs' '**/*.json' --check; then
25+
echo "::error::Code formatting check failed. Please run 'npm run format' locally to format the code."
26+
exit 1
27+
fi
28+
- name: Check linter (run 'npm run lint' to check, 'npm run lint-fix' to fix)
29+
run: |
30+
if ! npm run lint; then
31+
echo "::error::Linter check failed. Please run 'npm run lint' (or 'npm run lint-fix') locally to fix lint errors."
32+
exit 1
33+
fi

sdk-samples/abort_signal.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
* Copyright 2025 Google LLC
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6+
67
import {GoogleGenAI} from '@google/genai';
8+
import {MODEL_FLASH_LITE} from './constants.js';
79

8-
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
10+
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
911
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
1012
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
1113
const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI;
@@ -15,7 +17,7 @@ async function abortStreamingFromMLDev() {
1517
const abortController = new AbortController();
1618
const abortSignal = abortController.signal;
1719
const response = await ai.models.generateContentStream({
18-
model: 'gemini-2.0-flash',
20+
model: MODEL_FLASH_LITE,
1921
contents: 'Tell me a story in 300 words?',
2022
config: {
2123
abortSignal: abortSignal,
@@ -38,7 +40,7 @@ async function abortStreamingFromVertexAI() {
3840
const abortController = new AbortController();
3941
const abortSignal = abortController.signal;
4042
const response = await ai.models.generateContentStream({
41-
model: 'gemini-2.0-flash',
43+
model: MODEL_FLASH_LITE,
4244
contents: 'Tell me a story in 300 words?',
4345
config: {
4446
abortSignal: abortSignal,
@@ -53,14 +55,20 @@ async function abortStreamingFromVertexAI() {
5355
}
5456

5557
async function main() {
58+
const handleAbortError = (e: unknown) => {
59+
if (
60+
e instanceof Error &&
61+
(e.name === 'AbortError' || e.message.toLowerCase().includes('abort'))
62+
) {
63+
console.log('got expected abort error:', e.message);
64+
} else {
65+
throw e;
66+
}
67+
};
5668
if (GOOGLE_GENAI_USE_VERTEXAI) {
57-
await abortStreamingFromVertexAI().catch((e) =>
58-
console.error('got expected abort error', e),
59-
);
69+
await abortStreamingFromVertexAI().catch(handleAbortError);
6070
} else {
61-
await abortStreamingFromMLDev().catch((e) =>
62-
console.error('got expected abort error', e),
63-
);
71+
await abortStreamingFromMLDev().catch(handleAbortError);
6472
}
6573
}
6674

sdk-samples/api_error.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import {GoogleGenAI} from '@google/genai';
77

8-
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
8+
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
99
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
1010
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
1111
const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI;
@@ -34,18 +34,22 @@ async function throwApiErrorForVertexAI() {
3434
}
3535

3636
async function main() {
37+
let errorCaught = false;
38+
const handleError = (e: unknown) => {
39+
errorCaught = true;
40+
const err = e as Error & {status?: number};
41+
console.log('Successfully caught expected API error:');
42+
console.log(' name: ', err.name);
43+
console.log(' message: ', err.message);
44+
console.log(' status: ', err.status);
45+
};
3746
if (GOOGLE_GENAI_USE_VERTEXAI) {
38-
await throwApiErrorForVertexAI().catch((e) => {
39-
console.error('error name: ', e.name);
40-
console.error('error message: ', e.message);
41-
console.error('error status: ', e.status);
42-
});
47+
await throwApiErrorForVertexAI().catch(handleError);
4348
} else {
44-
await throwApiErrorForMLDev().catch((e) => {
45-
console.error('error name: ', e.name);
46-
console.error('error message: ', e.message);
47-
console.error('error status: ', e.status);
48-
});
49+
await throwApiErrorForMLDev().catch(handleError);
50+
}
51+
if (!errorCaught) {
52+
throw new Error('Expected API error was not thrown');
4953
}
5054
}
5155

sdk-samples/api_version.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
import {GoogleGenAI} from '@google/genai';
7+
import {MODEL_FLASH_LITE} from './constants.js';
78

8-
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
9+
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
910
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
1011
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
1112
const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI;
1213

1314
async function setApiVersionForMLDev() {
1415
const ai = new GoogleGenAI({apiKey: GEMINI_API_KEY, apiVersion: 'v1alpha'});
1516
const response = await ai.models.generateContent({
16-
model: 'gemini-2.0-flash',
17+
model: MODEL_FLASH_LITE,
1718
contents: 'Tell me a story in 300 words?',
1819
});
1920
console.log('text response: ', response.text);
@@ -27,7 +28,7 @@ async function setApiVersionForVertexAI() {
2728
apiVersion: 'v1',
2829
});
2930
const response = await ai.models.generateContent({
30-
model: 'gemini-2.0-flash',
31+
model: MODEL_FLASH_LITE,
3132
contents: 'Tell me a story in 300 words?',
3233
});
3334

@@ -36,11 +37,9 @@ async function setApiVersionForVertexAI() {
3637

3738
async function main() {
3839
if (GOOGLE_GENAI_USE_VERTEXAI) {
39-
await setApiVersionForVertexAI().catch((e) =>
40-
console.error('got error', e),
41-
);
40+
await setApiVersionForVertexAI();
4241
} else {
43-
await setApiVersionForMLDev().catch((e) => console.error('got error', e));
42+
await setApiVersionForMLDev();
4443
}
4544
}
4645

sdk-samples/batch_embedding_file.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ import {GoogleGenAI, JobState} from '@google/genai';
1010
import * as fs from 'fs/promises';
1111
import {tmpdir} from 'os';
1212
import * as path from 'path';
13+
import {MODEL_EMBEDDING} from './constants.js';
1314

1415
// Get your API key from https://aistudio.google.com/app/apikey
1516
// and set it as the GEMINI_API_KEY environment variable.
1617
const client = new GoogleGenAI({
17-
apiKey: process.env.GEMINI_API_KEY!,
18-
httpOptions: {
19-
// Use the staging endpoint for testing
20-
baseUrl: 'https://autopush-generativelanguage.sandbox.googleapis.com',
21-
},
18+
apiKey: process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY,
2219
});
2320

24-
const EMBEDDING_MODEL = 'models/gemini-embedding-001';
21+
const EMBEDDING_MODEL = MODEL_EMBEDDING;
2522

2623
async function batchEmbedFile() {
2724
// This is need to allow easy smoke testing of the sample.
@@ -33,28 +30,32 @@ async function batchEmbedFile() {
3330
}
3431
console.log('--- Batch Embedding with File Input ---');
3532

33+
const modelResource = EMBEDDING_MODEL.startsWith('models/')
34+
? EMBEDDING_MODEL
35+
: `models/${EMBEDDING_MODEL}`;
36+
3637
// 1. Prepare the input file content (JSONL)
3738
const jsonlContent = [
3839
{
3940
'key': 'request_1',
4041
'request': {
41-
'model': EMBEDDING_MODEL,
42+
'model': modelResource,
4243
'content': {'parts': [{'text': 'The quick brown fox'}]},
4344
'outputDimensionality': 5,
4445
},
4546
},
4647
{
4748
'key': 'request_2',
4849
'request': {
49-
'model': EMBEDDING_MODEL,
50+
'model': modelResource,
5051
'content': {'parts': [{'text': 'jumps over the lazy dog'}]},
5152
},
5253
'outputDimensionality': 5,
5354
},
5455
{
5556
'key': 'request_3',
5657
'request': {
57-
'model': EMBEDDING_MODEL,
58+
'model': modelResource,
5859
'content': {'parts': [{'text': 'A delightful summer day'}]},
5960
'outputDimensionality': 5,
6061
},

sdk-samples/batch_embedding_inline.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@
77
// tslint:disable:no-default-export
88

99
import {GoogleGenAI, JobState} from '@google/genai';
10+
import {MODEL_EMBEDDING} from './constants.js';
1011

1112
// Get your API key from https://aistudio.google.com/app/apikey
1213
// and set it as the GEMINI_API_KEY environment variable.
1314
const client = new GoogleGenAI({
14-
apiKey: process.env.GEMINI_API_KEY!,
15-
httpOptions: {
16-
// Use the staging endpoint for testing
17-
baseUrl: 'https://autopush-generativelanguage.sandbox.googleapis.com',
18-
},
15+
apiKey: process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY,
1916
});
2017

21-
const EMBEDDING_MODEL = 'models/gemini-embedding-001';
18+
const EMBEDDING_MODEL = MODEL_EMBEDDING;
2219

2320
async function batchEmbedInline() {
2421
// This is need to allow easy smoke testing of the sample.

sdk-samples/caches.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
import {GoogleGenAI, Part} from '@google/genai';
7+
import {MODEL_FLASH_LITE} from './constants.js';
78

89
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
910
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
@@ -35,7 +36,7 @@ async function createCacheFromVertexAI() {
3536
};
3637

3738
const cache = await ai.caches.create({
38-
model: 'gemini-2.5-flash',
39+
model: MODEL_FLASH_LITE,
3940
config: {contents: [cachedContent1, cachedContent2]},
4041
});
4142

@@ -62,9 +63,9 @@ async function createCacheFromVertexAI() {
6263

6364
async function main() {
6465
if (GOOGLE_GENAI_USE_VERTEXAI) {
65-
await createCacheFromVertexAI().catch((e) => console.error('got error', e));
66+
await createCacheFromVertexAI();
6667
} else {
67-
await createCacheFromMLDev().catch((e) => console.error('got error', e));
68+
await createCacheFromMLDev();
6869
}
6970
}
7071

sdk-samples/chat_afc.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import {Client} from '@modelcontextprotocol/sdk/client/index.js';
88
import {InMemoryTransport} from '@modelcontextprotocol/sdk/inMemory.js';
99
import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
1010
import {z} from 'zod';
11+
import {MODEL_FLASH_LITE} from './constants.js';
1112

12-
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
13+
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
1314
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
1415
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
1516
const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI;
@@ -19,7 +20,7 @@ async function chatAutofcSample(ai: GoogleGenAI) {
1920
const multiplyClient = await spinUpMultiplyServer();
2021

2122
const chat = await ai.chats.create({
22-
model: 'gemini-2.5-flash',
23+
model: MODEL_FLASH_LITE,
2324
config: {
2425
tools: [mcpToTool(weatherClient, multiplyClient)],
2526
toolConfig: {
@@ -68,7 +69,7 @@ async function spinUpWeatherServer(): Promise<Client> {
6869
name: 'reporter',
6970
version: '1.0.0',
7071
});
71-
client.connect(transports[1]);
72+
await client.connect(transports[1]);
7273

7374
return client;
7475
}
@@ -104,7 +105,7 @@ async function spinUpMultiplyServer(): Promise<Client> {
104105
name: 'multiplier',
105106
version: '1.0.0',
106107
});
107-
client.connect(transports[1]);
108+
await client.connect(transports[1]);
108109

109110
return client;
110111
}
@@ -121,7 +122,7 @@ async function main() {
121122
ai = new GoogleGenAI({vertexai: false, apiKey: GEMINI_API_KEY});
122123
}
123124

124-
chatAutofcSample(ai);
125+
await chatAutofcSample(ai);
125126
}
126127

127128
main();

sdk-samples/chat_afc_streaming.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import {
1212
Part,
1313
Type,
1414
} from '@google/genai';
15-
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
15+
import {MODEL_FLASH_LITE} from './constants.js';
16+
17+
const GEMINI_API_KEY = process.env.GEMINI_API_KEY || process.env.GOOGLE_API_KEY;
1618
const GOOGLE_CLOUD_PROJECT = process.env.GOOGLE_CLOUD_PROJECT;
1719
const GOOGLE_CLOUD_LOCATION = process.env.GOOGLE_CLOUD_LOCATION;
1820
const GOOGLE_GENAI_USE_VERTEXAI = process.env.GOOGLE_GENAI_USE_VERTEXAI;
@@ -56,7 +58,7 @@ async function chatFromMLDev() {
5658
},
5759
};
5860
const chat = ai.chats.create({
59-
model: 'gemini-2.0-flash',
61+
model: MODEL_FLASH_LITE,
6062
config: {
6163
tools: [controlLightCallableTool],
6264
toolConfig: {
@@ -130,7 +132,7 @@ async function chatFromVertexAI() {
130132
},
131133
};
132134
const chat = ai.chats.create({
133-
model: 'gemini-2.0-flash',
135+
model: MODEL_FLASH_LITE,
134136
config: {
135137
tools: [controlLightCallableTool],
136138
toolConfig: {
@@ -162,9 +164,9 @@ async function chatFromVertexAI() {
162164
}
163165
async function main() {
164166
if (GOOGLE_GENAI_USE_VERTEXAI) {
165-
await chatFromVertexAI().catch((e) => console.error('got error', e));
167+
await chatFromVertexAI();
166168
} else {
167-
await chatFromMLDev().catch((e) => console.error('got error', e));
169+
await chatFromMLDev();
168170
}
169171
}
170172
main();

0 commit comments

Comments
 (0)