@google/genai 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @license
5
- * Copyright 2024 Google LLC
5
+ * Copyright 2025 Google LLC
6
6
  * SPDX-License-Identifier: Apache-2.0
7
7
  */
8
8
  class BaseModule {
@@ -129,7 +129,7 @@ function getValueByPath(data, keys) {
129
129
 
130
130
  /**
131
131
  * @license
132
- * Copyright 2024 Google LLC
132
+ * Copyright 2025 Google LLC
133
133
  * SPDX-License-Identifier: Apache-2.0
134
134
  */
135
135
  function tModel(apiClient, model) {
@@ -497,14 +497,12 @@ function tFileName(apiClient, fromName) {
497
497
 
498
498
  /**
499
499
  * @license
500
- * Copyright 2024 Google LLC
500
+ * Copyright 2025 Google LLC
501
501
  * SPDX-License-Identifier: Apache-2.0
502
502
  */
503
503
  /**
504
504
  * @fileoverview Pagers for the GenAI List APIs.
505
505
  */
506
- // TODO(b/401555348): Re-enable linting after enforcing strong types.
507
- /* eslint-disable @typescript-eslint/no-explicit-any */
508
506
  var PagedItem;
509
507
  (function (PagedItem) {
510
508
  PagedItem["PAGED_ITEM_BATCH_JOBS"] = "batchJobs";
@@ -519,6 +517,7 @@ var PagedItem;
519
517
  class Pager {
520
518
  constructor(name, request, response, params) {
521
519
  this.pageInternal = [];
520
+ this.paramsInternal = {};
522
521
  this.requestInternal = request;
523
522
  this.init(name, response, params);
524
523
  }
@@ -675,7 +674,7 @@ class Pager {
675
674
 
676
675
  /**
677
676
  * @license
678
- * Copyright 2024 Google LLC
677
+ * Copyright 2025 Google LLC
679
678
  * SPDX-License-Identifier: Apache-2.0
680
679
  */
681
680
  // Code generated by the Google Gen AI SDK generator DO NOT EDIT.
@@ -1264,7 +1263,7 @@ class SessionSendToolResponseParameters {
1264
1263
 
1265
1264
  /**
1266
1265
  * @license
1267
- * Copyright 2024 Google LLC
1266
+ * Copyright 2025 Google LLC
1268
1267
  * SPDX-License-Identifier: Apache-2.0
1269
1268
  */
1270
1269
  class Caches extends BaseModule {
@@ -1279,7 +1278,7 @@ class Caches extends BaseModule {
1279
1278
  *
1280
1279
  * @example
1281
1280
  * ```ts
1282
- * const cachedContents = await client.caches.list({config: {'pageSize': 2}});
1281
+ * const cachedContents = await ai.caches.list({config: {'pageSize': 2}});
1283
1282
  * for (const cachedContent of cachedContents) {
1284
1283
  * console.log(cachedContent);
1285
1284
  * }
@@ -1298,7 +1297,7 @@ class Caches extends BaseModule {
1298
1297
  * @example
1299
1298
  * ```ts
1300
1299
  * const contents = ...; // Initialize the content to cache.
1301
- * const response = await client.caches.create({
1300
+ * const response = await ai.caches.create({
1302
1301
  * model: 'gemini-2.0-flash',
1303
1302
  * config: {
1304
1303
  * 'contents': contents,
@@ -1369,7 +1368,7 @@ class Caches extends BaseModule {
1369
1368
  *
1370
1369
  * @example
1371
1370
  * ```ts
1372
- * await client.caches.get({name: 'gemini-1.5-flash'});
1371
+ * await ai.caches.get({name: 'gemini-1.5-flash'});
1373
1372
  * ```
1374
1373
  */
1375
1374
  async get(params) {
@@ -1432,7 +1431,7 @@ class Caches extends BaseModule {
1432
1431
  *
1433
1432
  * @example
1434
1433
  * ```ts
1435
- * await client.caches.delete({name: 'gemini-1.5-flash'});
1434
+ * await ai.caches.delete({name: 'gemini-1.5-flash'});
1436
1435
  * ```
1437
1436
  */
1438
1437
  async delete(params) {
@@ -1499,7 +1498,7 @@ class Caches extends BaseModule {
1499
1498
  *
1500
1499
  * @example
1501
1500
  * ```ts
1502
- * const response = await client.caches.update({
1501
+ * const response = await ai.caches.update({
1503
1502
  * name: 'gemini-1.5-flash',
1504
1503
  * config: {'ttl': '7600s'}
1505
1504
  * });
@@ -2503,7 +2502,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
2503
2502
 
2504
2503
  /**
2505
2504
  * @license
2506
- * Copyright 2024 Google LLC
2505
+ * Copyright 2025 Google LLC
2507
2506
  * SPDX-License-Identifier: Apache-2.0
2508
2507
  */
2509
2508
  /**
@@ -2608,16 +2607,35 @@ class Chats {
2608
2607
  /**
2609
2608
  * Creates a new chat session.
2610
2609
  *
2610
+ * @remarks
2611
+ * The config in the params will be used for all requests within the chat
2612
+ * session unless overridden by a per-request `config` in
2613
+ * {@link ./types.SendMessageParameters}.
2614
+ *
2611
2615
  * @param params - Parameters for creating a chat session.
2612
2616
  * @returns A new chat session.
2617
+ *
2618
+ * @example
2619
+ * ```ts
2620
+ * const chat = ai.chats.create({
2621
+ * model: 'gemini-2.0-flash'
2622
+ * config: {
2623
+ * temperature: 0.5,
2624
+ * maxOutputTokens: 1024,
2625
+ * }
2626
+ * });
2627
+ * ```
2613
2628
  */
2614
2629
  create(params) {
2615
2630
  return new Chat(this.apiClient, this.modelsModule, params.model, params.config, params.history);
2616
2631
  }
2617
2632
  }
2618
2633
  /**
2619
- * Chat session that enables sending messages and stores the chat history so
2620
- * far.
2634
+ * Chat session that enables sending messages to the model with previous
2635
+ * conversation context.
2636
+ *
2637
+ * @remarks
2638
+ * The session maintains all the turns between user and model.
2621
2639
  */
2622
2640
  class Chat {
2623
2641
  constructor(apiClient, modelsModule, model, config = {}, history = []) {
@@ -2641,6 +2659,15 @@ class Chat {
2641
2659
  * @see {@link Chat#sendMessageStream} for streaming method.
2642
2660
  * @param params - parameters for sending messages within a chat session.
2643
2661
  * @returns The model's response.
2662
+ *
2663
+ * @example
2664
+ * ```ts
2665
+ * const chat = ai.chats.create({model: 'gemini-2.0-flash'});
2666
+ * const response = await chat.sendMessage({
2667
+ * message: 'Why is the sky blue?'
2668
+ * });
2669
+ * console.log(response.text);
2670
+ * ```
2644
2671
  */
2645
2672
  async sendMessage(params) {
2646
2673
  var _a;
@@ -2672,6 +2699,17 @@ class Chat {
2672
2699
  * @see {@link Chat#sendMessage} for non-streaming method.
2673
2700
  * @param params - parameters for sending the message.
2674
2701
  * @return The model's response.
2702
+ *
2703
+ * @example
2704
+ * ```ts
2705
+ * const chat = ai.chats.create({model: 'gemini-2.0-flash'});
2706
+ * const response = await chat.sendMessageStream({
2707
+ * message: 'Why is the sky blue?'
2708
+ * });
2709
+ * for await (const chunk of response) {
2710
+ * console.log(chunk.text);
2711
+ * }
2712
+ * ```
2675
2713
  */
2676
2714
  async sendMessageStream(params) {
2677
2715
  var _a;
@@ -2763,13 +2801,13 @@ class Chat {
2763
2801
 
2764
2802
  /**
2765
2803
  * @license
2766
- * Copyright 2024 Google LLC
2804
+ * Copyright 2025 Google LLC
2767
2805
  * SPDX-License-Identifier: Apache-2.0
2768
2806
  */
2769
2807
  const CONTENT_TYPE_HEADER = 'Content-Type';
2770
2808
  const USER_AGENT_HEADER = 'User-Agent';
2771
2809
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
2772
- const SDK_VERSION = '0.2.0'; // x-release-please-version
2810
+ const SDK_VERSION = '0.3.1'; // x-release-please-version
2773
2811
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
2774
2812
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
2775
2813
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -2981,7 +3019,7 @@ class ApiClient {
2981
3019
  async unaryApiCall(url, requestInit, httpMethod) {
2982
3020
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
2983
3021
  .then(async (response) => {
2984
- await throwErrorIfNotOK(response, url.toString());
3022
+ await throwErrorIfNotOK(response);
2985
3023
  return new HttpResponse(response);
2986
3024
  })
2987
3025
  .catch((e) => {
@@ -2996,7 +3034,7 @@ class ApiClient {
2996
3034
  async streamApiCall(url, requestInit, httpMethod) {
2997
3035
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
2998
3036
  .then(async (response) => {
2999
- await throwErrorIfNotOK(response, url.toString());
3037
+ await throwErrorIfNotOK(response);
3000
3038
  return this.processStreamResponse(response);
3001
3039
  })
3002
3040
  .catch((e) => {
@@ -3141,7 +3179,7 @@ class ApiClient {
3141
3179
  return uploadUrl;
3142
3180
  }
3143
3181
  }
3144
- async function throwErrorIfNotOK(response, url, requestInit) {
3182
+ async function throwErrorIfNotOK(response) {
3145
3183
  var _a;
3146
3184
  if (response === undefined) {
3147
3185
  throw new ServerError('response is undefined');
@@ -3263,7 +3301,7 @@ class CrossWebSocketFactory {
3263
3301
 
3264
3302
  /**
3265
3303
  * @license
3266
- * Copyright 2024 Google LLC
3304
+ * Copyright 2025 Google LLC
3267
3305
  * SPDX-License-Identifier: Apache-2.0
3268
3306
  */
3269
3307
  class Files extends BaseModule {
@@ -3281,7 +3319,7 @@ class Files extends BaseModule {
3281
3319
  * size of each page is 10.
3282
3320
  *
3283
3321
  * ```ts
3284
- * const listResponse = await client.files.list({config: {'pageSize': 10}});
3322
+ * const listResponse = await ai.files.list({config: {'pageSize': 10}});
3285
3323
  * for await (const file of listResponse) {
3286
3324
  * console.log(file.name);
3287
3325
  * }
@@ -3314,30 +3352,32 @@ class Files extends BaseModule {
3314
3352
  *
3315
3353
  * This section can contain multiple paragraphs and code examples.
3316
3354
  *
3317
- * @param file The string path to the file to be uploaded or a Blob object.
3318
- * @param config Optional parameters specified in the `types.UploadFileConfig`
3319
- * interface. Optional @see {@link types.UploadFileConfig}
3355
+ * @param params - Optional parameters specified in the
3356
+ * `common.UploadFileParameters` interface.
3357
+ * Optional @see {@link common.UploadFileParameters}
3320
3358
  * @return A promise that resolves to a `types.File` object.
3321
3359
  * @throws An error if called on a Vertex AI client.
3322
3360
  * @throws An error if the `mimeType` is not provided and can not be inferred,
3323
- * the `mimeType` can be provided in the `config` parameter.
3361
+ * the `mimeType` can be provided in the `params.config` parameter.
3324
3362
  * @throws An error occurs if a suitable upload location cannot be established.
3325
3363
  *
3326
3364
  * @example
3327
3365
  * The following code uploads a file to Gemini API.
3328
3366
  *
3329
3367
  * ```ts
3330
- * const file = await client.files.upload('file.txt', {
3368
+ * const file = await ai.files.upload({file: 'file.txt', config: {
3331
3369
  * mimeType: 'text/plain',
3332
- * });
3370
+ * }});
3333
3371
  * console.log(file.name);
3334
3372
  * ```
3335
3373
  */
3336
- async upload(file, config) {
3374
+ async upload(params) {
3337
3375
  if (this.apiClient.isVertexAI()) {
3338
3376
  throw new Error('Vertex AI does not support uploading files. You can share files through a GCS bucket.');
3339
3377
  }
3340
- return this.apiClient.uploadFile(file, config).then((response) => {
3378
+ return this.apiClient
3379
+ .uploadFile(params.file, params.config)
3380
+ .then((response) => {
3341
3381
  const file = fileFromMldev(this.apiClient, response);
3342
3382
  return file;
3343
3383
  });
@@ -3465,7 +3505,7 @@ class Files extends BaseModule {
3465
3505
  * const config: GetFileParameters = {
3466
3506
  * name: fileName,
3467
3507
  * };
3468
- * file = await client.files.get(config);
3508
+ * file = await ai.files.get(config);
3469
3509
  * console.log(file.name);
3470
3510
  * ```
3471
3511
  */
@@ -3801,7 +3841,7 @@ function createFileResponseFromVertex(apiClient, fromObject) {
3801
3841
 
3802
3842
  /**
3803
3843
  * @license
3804
- * Copyright 2024 Google LLC
3844
+ * Copyright 2025 Google LLC
3805
3845
  * SPDX-License-Identifier: Apache-2.0
3806
3846
  */
3807
3847
  class Models extends BaseModule {
@@ -3831,14 +3871,12 @@ class Models extends BaseModule {
3831
3871
  *
3832
3872
  * Some models support multimodal input and output.
3833
3873
  *
3834
- * @param model - The model to use for generating content.
3835
- * @param contents - The input contents to use for generating content.
3836
- * @param [config] - The configuration for generating content.
3874
+ * @param params - The parameters for generating content.
3837
3875
  * @return The response from generating content.
3838
3876
  *
3839
3877
  * @example
3840
3878
  * ```ts
3841
- * const response = await client.models.generateContent({
3879
+ * const response = await ai.models.generateContent({
3842
3880
  * model: 'gemini-2.0-flash',
3843
3881
  * contents: 'why is the sky blue?',
3844
3882
  * config: {
@@ -3875,14 +3913,12 @@ class Models extends BaseModule {
3875
3913
  *
3876
3914
  * Some models support multimodal input and output.
3877
3915
  *
3878
- * @param model - The model to use for generating content.
3879
- * @param contents - The input contents to use for generating content.
3880
- * @param [config] - The configuration for generating content.
3916
+ * @param params - The parameters for generating content with streaming response.
3881
3917
  * @return The response from generating content.
3882
3918
  *
3883
3919
  * @example
3884
3920
  * ```ts
3885
- * const response = await client.models.generateContentStream({
3921
+ * const response = await ai.models.generateContentStream({
3886
3922
  * model: 'gemini-2.0-flash',
3887
3923
  * contents: 'why is the sky blue?',
3888
3924
  * config: {
@@ -4041,14 +4077,12 @@ class Models extends BaseModule {
4041
4077
  /**
4042
4078
  * Calculates embeddings for the given contents. Only text is supported.
4043
4079
  *
4044
- * @param model - The model to use.
4045
- * @param contents - The contents to embed.
4046
- * @param [config] - The config for embedding contents.
4080
+ * @param params - The parameters for embedding contents.
4047
4081
  * @return The response from the API.
4048
4082
  *
4049
4083
  * @example
4050
4084
  * ```ts
4051
- * const response = await client.models.embedContent({
4085
+ * const response = await ai.models.embedContent({
4052
4086
  * model: 'text-embedding-004',
4053
4087
  * contents: [
4054
4088
  * 'What is your name?',
@@ -4120,14 +4154,12 @@ class Models extends BaseModule {
4120
4154
  /**
4121
4155
  * Generates an image based on a text description and configuration.
4122
4156
  *
4123
- * @param model - The model to use.
4124
- * @param prompt - A text description of the image to generate.
4125
- * @param [config] - The config for image generation.
4157
+ * @param params - The parameters for generating images.
4126
4158
  * @return The response from the API.
4127
4159
  *
4128
4160
  * @example
4129
4161
  * ```ts
4130
- * const response = await client.models.generateImages({
4162
+ * const response = await ai.models.generateImages({
4131
4163
  * model: 'imagen-3.0-generate-002',
4132
4164
  * prompt: 'Robot holding a red skateboard',
4133
4165
  * config: {
@@ -4198,14 +4230,12 @@ class Models extends BaseModule {
4198
4230
  * Counts the number of tokens in the given contents. Multimodal input is
4199
4231
  * supported for Gemini models.
4200
4232
  *
4201
- * @param model - The model to use for counting tokens.
4202
- * @param contents - The contents to count tokens for.
4203
- * @param [config] - The config for counting tokens.
4233
+ * @param params - The parameters for counting tokens.
4204
4234
  * @return The response from the API.
4205
4235
  *
4206
4236
  * @example
4207
4237
  * ```ts
4208
- * const response = await client.models.countTokens({
4238
+ * const response = await ai.models.countTokens({
4209
4239
  * model: 'gemini-2.0-flash',
4210
4240
  * contents: 'The quick brown fox jumps over the lazy dog.'
4211
4241
  * });
@@ -4274,14 +4304,12 @@ class Models extends BaseModule {
4274
4304
  *
4275
4305
  * This method is not supported by the Gemini Developer API.
4276
4306
  *
4277
- * @param model - The model to use.
4278
- * @param contents - The content to compute tokens for.
4279
- * @param [config] - The config for computing tokens.
4307
+ * @param params - The parameters for computing tokens.
4280
4308
  * @return The response from the API.
4281
4309
  *
4282
4310
  * @example
4283
4311
  * ```ts
4284
- * const response = await client.models.computeTokens({
4312
+ * const response = await ai.models.computeTokens({
4285
4313
  * model: 'gemini-2.0-flash',
4286
4314
  * contents: 'What is your name?'
4287
4315
  * });
@@ -6260,7 +6288,7 @@ function computeTokensResponseFromVertex(apiClient, fromObject) {
6260
6288
 
6261
6289
  /**
6262
6290
  * @license
6263
- * Copyright 2024 Google LLC
6291
+ * Copyright 2025 Google LLC
6264
6292
  * SPDX-License-Identifier: Apache-2.0
6265
6293
  */
6266
6294
  const FUNCTION_RESPONSE_REQUIRES_ID = 'FunctionResponse request must have an `id` field from the response of a ToolCall.FunctionalCalls in Google AI.';
@@ -6521,6 +6549,12 @@ function liveServerMessageFromVertex(apiClient, fromObject) {
6521
6549
  /**
6522
6550
  * Handles incoming messages from the WebSocket.
6523
6551
  *
6552
+ * @remarks
6553
+ * This function is responsible for parsing incoming messages, transforming them
6554
+ * into LiveServerMessages, and then calling the onmessage callback. Note that
6555
+ * the first message which is received from the server is a setupComplete
6556
+ * message.
6557
+ *
6524
6558
  * @param apiClient The ApiClient instance.
6525
6559
  * @param onmessage The user-provided onmessage callback (if any).
6526
6560
  * @param event The MessageEvent from the WebSocket.
@@ -6558,20 +6592,14 @@ class Live {
6558
6592
  Establishes a connection to the specified model with the given
6559
6593
  configuration and returns a Session object representing that connection.
6560
6594
 
6561
- > [!CAUTION] This SDK does not yet support the live API for **Google Vertex AI**.
6562
-
6563
6595
  @experimental
6564
6596
 
6565
- @param model - Model to use for the Live session.
6566
- @param config - Configuration parameters for the Live session.
6567
- @param callbacks - Optional callbacks for websocket events. If not
6568
- provided, default no-op callbacks will be used. Generally, prefer to
6569
- provide explicit callbacks to allow for proper handling of websocket
6570
- events (e.g. connection errors).
6597
+ @param params - The parameters for establishing a connection to the model.
6598
+ @return A live session.
6571
6599
 
6572
6600
  @example
6573
6601
  ```ts
6574
- const session = await client.live.connect({
6602
+ const session = await ai.live.connect({
6575
6603
  model: 'gemini-2.0-flash-exp',
6576
6604
  config: {
6577
6605
  responseModalities: [Modality.AUDIO],
@@ -6595,9 +6623,6 @@ class Live {
6595
6623
  */
6596
6624
  async connect(params) {
6597
6625
  var _a, _b;
6598
- if (this.apiClient.isVertexAI()) {
6599
- throw new Error('This SDK does not yet support the live API for **Google Vertex AI**.');
6600
- }
6601
6626
  const websocketBaseUrl = this.apiClient.getWebsocketBaseUrl();
6602
6627
  const apiVersion = this.apiClient.getApiVersion();
6603
6628
  let url;
@@ -6673,8 +6698,7 @@ class Session {
6673
6698
  if (params.turns !== null && params.turns !== undefined) {
6674
6699
  let contents = [];
6675
6700
  try {
6676
- contents =
6677
- tContents(apiClient, params.turns);
6701
+ contents = tContents(apiClient, params.turns);
6678
6702
  if (apiClient.isVertexAI()) {
6679
6703
  contents = contents.map((item) => contentToVertex(apiClient, item));
6680
6704
  }
@@ -6714,8 +6738,10 @@ class Session {
6714
6738
  throw new Error('functionResponses is required.');
6715
6739
  }
6716
6740
  for (const functionResponse of functionResponses) {
6717
- if (typeof functionResponse !== 'object' || functionResponse === null ||
6718
- !('name' in functionResponse) || !('response' in functionResponse)) {
6741
+ if (typeof functionResponse !== 'object' ||
6742
+ functionResponse === null ||
6743
+ !('name' in functionResponse) ||
6744
+ !('response' in functionResponse)) {
6719
6745
  throw new Error(`Could not parse function response, type '${typeof functionResponse}'.`);
6720
6746
  }
6721
6747
  if (!apiClient.isVertexAI() && !('id' in functionResponse)) {
@@ -6723,7 +6749,7 @@ class Session {
6723
6749
  }
6724
6750
  }
6725
6751
  const clientMessage = {
6726
- toolResponse: { functionResponses: functionResponses }
6752
+ toolResponse: { functionResponses: functionResponses },
6727
6753
  };
6728
6754
  return clientMessage;
6729
6755
  }
@@ -6841,7 +6867,7 @@ class Session {
6841
6867
 
6842
6868
  @example
6843
6869
  ```ts
6844
- const session = await client.live.connect({
6870
+ const session = await ai.live.connect({
6845
6871
  model: 'gemini-2.0-flash-exp',
6846
6872
  config: {
6847
6873
  responseModalities: [Modality.AUDIO],