@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.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright 2024 Google LLC
3
+ * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  class BaseModule {
@@ -127,7 +127,7 @@ function getValueByPath(data, keys) {
127
127
 
128
128
  /**
129
129
  * @license
130
- * Copyright 2024 Google LLC
130
+ * Copyright 2025 Google LLC
131
131
  * SPDX-License-Identifier: Apache-2.0
132
132
  */
133
133
  function tModel(apiClient, model) {
@@ -495,14 +495,12 @@ function tFileName(apiClient, fromName) {
495
495
 
496
496
  /**
497
497
  * @license
498
- * Copyright 2024 Google LLC
498
+ * Copyright 2025 Google LLC
499
499
  * SPDX-License-Identifier: Apache-2.0
500
500
  */
501
501
  /**
502
502
  * @fileoverview Pagers for the GenAI List APIs.
503
503
  */
504
- // TODO(b/401555348): Re-enable linting after enforcing strong types.
505
- /* eslint-disable @typescript-eslint/no-explicit-any */
506
504
  var PagedItem;
507
505
  (function (PagedItem) {
508
506
  PagedItem["PAGED_ITEM_BATCH_JOBS"] = "batchJobs";
@@ -517,6 +515,7 @@ var PagedItem;
517
515
  class Pager {
518
516
  constructor(name, request, response, params) {
519
517
  this.pageInternal = [];
518
+ this.paramsInternal = {};
520
519
  this.requestInternal = request;
521
520
  this.init(name, response, params);
522
521
  }
@@ -673,7 +672,7 @@ class Pager {
673
672
 
674
673
  /**
675
674
  * @license
676
- * Copyright 2024 Google LLC
675
+ * Copyright 2025 Google LLC
677
676
  * SPDX-License-Identifier: Apache-2.0
678
677
  */
679
678
  // Code generated by the Google Gen AI SDK generator DO NOT EDIT.
@@ -1262,7 +1261,7 @@ class SessionSendToolResponseParameters {
1262
1261
 
1263
1262
  /**
1264
1263
  * @license
1265
- * Copyright 2024 Google LLC
1264
+ * Copyright 2025 Google LLC
1266
1265
  * SPDX-License-Identifier: Apache-2.0
1267
1266
  */
1268
1267
  class Caches extends BaseModule {
@@ -1277,7 +1276,7 @@ class Caches extends BaseModule {
1277
1276
  *
1278
1277
  * @example
1279
1278
  * ```ts
1280
- * const cachedContents = await client.caches.list({config: {'pageSize': 2}});
1279
+ * const cachedContents = await ai.caches.list({config: {'pageSize': 2}});
1281
1280
  * for (const cachedContent of cachedContents) {
1282
1281
  * console.log(cachedContent);
1283
1282
  * }
@@ -1296,7 +1295,7 @@ class Caches extends BaseModule {
1296
1295
  * @example
1297
1296
  * ```ts
1298
1297
  * const contents = ...; // Initialize the content to cache.
1299
- * const response = await client.caches.create({
1298
+ * const response = await ai.caches.create({
1300
1299
  * model: 'gemini-2.0-flash',
1301
1300
  * config: {
1302
1301
  * 'contents': contents,
@@ -1367,7 +1366,7 @@ class Caches extends BaseModule {
1367
1366
  *
1368
1367
  * @example
1369
1368
  * ```ts
1370
- * await client.caches.get({name: 'gemini-1.5-flash'});
1369
+ * await ai.caches.get({name: 'gemini-1.5-flash'});
1371
1370
  * ```
1372
1371
  */
1373
1372
  async get(params) {
@@ -1430,7 +1429,7 @@ class Caches extends BaseModule {
1430
1429
  *
1431
1430
  * @example
1432
1431
  * ```ts
1433
- * await client.caches.delete({name: 'gemini-1.5-flash'});
1432
+ * await ai.caches.delete({name: 'gemini-1.5-flash'});
1434
1433
  * ```
1435
1434
  */
1436
1435
  async delete(params) {
@@ -1497,7 +1496,7 @@ class Caches extends BaseModule {
1497
1496
  *
1498
1497
  * @example
1499
1498
  * ```ts
1500
- * const response = await client.caches.update({
1499
+ * const response = await ai.caches.update({
1501
1500
  * name: 'gemini-1.5-flash',
1502
1501
  * config: {'ttl': '7600s'}
1503
1502
  * });
@@ -2501,7 +2500,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
2501
2500
 
2502
2501
  /**
2503
2502
  * @license
2504
- * Copyright 2024 Google LLC
2503
+ * Copyright 2025 Google LLC
2505
2504
  * SPDX-License-Identifier: Apache-2.0
2506
2505
  */
2507
2506
  /**
@@ -2606,16 +2605,35 @@ class Chats {
2606
2605
  /**
2607
2606
  * Creates a new chat session.
2608
2607
  *
2608
+ * @remarks
2609
+ * The config in the params will be used for all requests within the chat
2610
+ * session unless overridden by a per-request `config` in
2611
+ * {@link ./types.SendMessageParameters}.
2612
+ *
2609
2613
  * @param params - Parameters for creating a chat session.
2610
2614
  * @returns A new chat session.
2615
+ *
2616
+ * @example
2617
+ * ```ts
2618
+ * const chat = ai.chats.create({
2619
+ * model: 'gemini-2.0-flash'
2620
+ * config: {
2621
+ * temperature: 0.5,
2622
+ * maxOutputTokens: 1024,
2623
+ * }
2624
+ * });
2625
+ * ```
2611
2626
  */
2612
2627
  create(params) {
2613
2628
  return new Chat(this.apiClient, this.modelsModule, params.model, params.config, params.history);
2614
2629
  }
2615
2630
  }
2616
2631
  /**
2617
- * Chat session that enables sending messages and stores the chat history so
2618
- * far.
2632
+ * Chat session that enables sending messages to the model with previous
2633
+ * conversation context.
2634
+ *
2635
+ * @remarks
2636
+ * The session maintains all the turns between user and model.
2619
2637
  */
2620
2638
  class Chat {
2621
2639
  constructor(apiClient, modelsModule, model, config = {}, history = []) {
@@ -2639,6 +2657,15 @@ class Chat {
2639
2657
  * @see {@link Chat#sendMessageStream} for streaming method.
2640
2658
  * @param params - parameters for sending messages within a chat session.
2641
2659
  * @returns The model's response.
2660
+ *
2661
+ * @example
2662
+ * ```ts
2663
+ * const chat = ai.chats.create({model: 'gemini-2.0-flash'});
2664
+ * const response = await chat.sendMessage({
2665
+ * message: 'Why is the sky blue?'
2666
+ * });
2667
+ * console.log(response.text);
2668
+ * ```
2642
2669
  */
2643
2670
  async sendMessage(params) {
2644
2671
  var _a;
@@ -2670,6 +2697,17 @@ class Chat {
2670
2697
  * @see {@link Chat#sendMessage} for non-streaming method.
2671
2698
  * @param params - parameters for sending the message.
2672
2699
  * @return The model's response.
2700
+ *
2701
+ * @example
2702
+ * ```ts
2703
+ * const chat = ai.chats.create({model: 'gemini-2.0-flash'});
2704
+ * const response = await chat.sendMessageStream({
2705
+ * message: 'Why is the sky blue?'
2706
+ * });
2707
+ * for await (const chunk of response) {
2708
+ * console.log(chunk.text);
2709
+ * }
2710
+ * ```
2673
2711
  */
2674
2712
  async sendMessageStream(params) {
2675
2713
  var _a;
@@ -2761,13 +2799,13 @@ class Chat {
2761
2799
 
2762
2800
  /**
2763
2801
  * @license
2764
- * Copyright 2024 Google LLC
2802
+ * Copyright 2025 Google LLC
2765
2803
  * SPDX-License-Identifier: Apache-2.0
2766
2804
  */
2767
2805
  const CONTENT_TYPE_HEADER = 'Content-Type';
2768
2806
  const USER_AGENT_HEADER = 'User-Agent';
2769
2807
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
2770
- const SDK_VERSION = '0.2.0'; // x-release-please-version
2808
+ const SDK_VERSION = '0.3.1'; // x-release-please-version
2771
2809
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
2772
2810
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
2773
2811
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -2979,7 +3017,7 @@ class ApiClient {
2979
3017
  async unaryApiCall(url, requestInit, httpMethod) {
2980
3018
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
2981
3019
  .then(async (response) => {
2982
- await throwErrorIfNotOK(response, url.toString());
3020
+ await throwErrorIfNotOK(response);
2983
3021
  return new HttpResponse(response);
2984
3022
  })
2985
3023
  .catch((e) => {
@@ -2994,7 +3032,7 @@ class ApiClient {
2994
3032
  async streamApiCall(url, requestInit, httpMethod) {
2995
3033
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
2996
3034
  .then(async (response) => {
2997
- await throwErrorIfNotOK(response, url.toString());
3035
+ await throwErrorIfNotOK(response);
2998
3036
  return this.processStreamResponse(response);
2999
3037
  })
3000
3038
  .catch((e) => {
@@ -3139,7 +3177,7 @@ class ApiClient {
3139
3177
  return uploadUrl;
3140
3178
  }
3141
3179
  }
3142
- async function throwErrorIfNotOK(response, url, requestInit) {
3180
+ async function throwErrorIfNotOK(response) {
3143
3181
  var _a;
3144
3182
  if (response === undefined) {
3145
3183
  throw new ServerError('response is undefined');
@@ -3261,7 +3299,7 @@ class CrossWebSocketFactory {
3261
3299
 
3262
3300
  /**
3263
3301
  * @license
3264
- * Copyright 2024 Google LLC
3302
+ * Copyright 2025 Google LLC
3265
3303
  * SPDX-License-Identifier: Apache-2.0
3266
3304
  */
3267
3305
  class Files extends BaseModule {
@@ -3279,7 +3317,7 @@ class Files extends BaseModule {
3279
3317
  * size of each page is 10.
3280
3318
  *
3281
3319
  * ```ts
3282
- * const listResponse = await client.files.list({config: {'pageSize': 10}});
3320
+ * const listResponse = await ai.files.list({config: {'pageSize': 10}});
3283
3321
  * for await (const file of listResponse) {
3284
3322
  * console.log(file.name);
3285
3323
  * }
@@ -3312,30 +3350,32 @@ class Files extends BaseModule {
3312
3350
  *
3313
3351
  * This section can contain multiple paragraphs and code examples.
3314
3352
  *
3315
- * @param file The string path to the file to be uploaded or a Blob object.
3316
- * @param config Optional parameters specified in the `types.UploadFileConfig`
3317
- * interface. Optional @see {@link types.UploadFileConfig}
3353
+ * @param params - Optional parameters specified in the
3354
+ * `common.UploadFileParameters` interface.
3355
+ * Optional @see {@link common.UploadFileParameters}
3318
3356
  * @return A promise that resolves to a `types.File` object.
3319
3357
  * @throws An error if called on a Vertex AI client.
3320
3358
  * @throws An error if the `mimeType` is not provided and can not be inferred,
3321
- * the `mimeType` can be provided in the `config` parameter.
3359
+ * the `mimeType` can be provided in the `params.config` parameter.
3322
3360
  * @throws An error occurs if a suitable upload location cannot be established.
3323
3361
  *
3324
3362
  * @example
3325
3363
  * The following code uploads a file to Gemini API.
3326
3364
  *
3327
3365
  * ```ts
3328
- * const file = await client.files.upload('file.txt', {
3366
+ * const file = await ai.files.upload({file: 'file.txt', config: {
3329
3367
  * mimeType: 'text/plain',
3330
- * });
3368
+ * }});
3331
3369
  * console.log(file.name);
3332
3370
  * ```
3333
3371
  */
3334
- async upload(file, config) {
3372
+ async upload(params) {
3335
3373
  if (this.apiClient.isVertexAI()) {
3336
3374
  throw new Error('Vertex AI does not support uploading files. You can share files through a GCS bucket.');
3337
3375
  }
3338
- return this.apiClient.uploadFile(file, config).then((response) => {
3376
+ return this.apiClient
3377
+ .uploadFile(params.file, params.config)
3378
+ .then((response) => {
3339
3379
  const file = fileFromMldev(this.apiClient, response);
3340
3380
  return file;
3341
3381
  });
@@ -3463,7 +3503,7 @@ class Files extends BaseModule {
3463
3503
  * const config: GetFileParameters = {
3464
3504
  * name: fileName,
3465
3505
  * };
3466
- * file = await client.files.get(config);
3506
+ * file = await ai.files.get(config);
3467
3507
  * console.log(file.name);
3468
3508
  * ```
3469
3509
  */
@@ -3799,7 +3839,7 @@ function createFileResponseFromVertex(apiClient, fromObject) {
3799
3839
 
3800
3840
  /**
3801
3841
  * @license
3802
- * Copyright 2024 Google LLC
3842
+ * Copyright 2025 Google LLC
3803
3843
  * SPDX-License-Identifier: Apache-2.0
3804
3844
  */
3805
3845
  class Models extends BaseModule {
@@ -3829,14 +3869,12 @@ class Models extends BaseModule {
3829
3869
  *
3830
3870
  * Some models support multimodal input and output.
3831
3871
  *
3832
- * @param model - The model to use for generating content.
3833
- * @param contents - The input contents to use for generating content.
3834
- * @param [config] - The configuration for generating content.
3872
+ * @param params - The parameters for generating content.
3835
3873
  * @return The response from generating content.
3836
3874
  *
3837
3875
  * @example
3838
3876
  * ```ts
3839
- * const response = await client.models.generateContent({
3877
+ * const response = await ai.models.generateContent({
3840
3878
  * model: 'gemini-2.0-flash',
3841
3879
  * contents: 'why is the sky blue?',
3842
3880
  * config: {
@@ -3873,14 +3911,12 @@ class Models extends BaseModule {
3873
3911
  *
3874
3912
  * Some models support multimodal input and output.
3875
3913
  *
3876
- * @param model - The model to use for generating content.
3877
- * @param contents - The input contents to use for generating content.
3878
- * @param [config] - The configuration for generating content.
3914
+ * @param params - The parameters for generating content with streaming response.
3879
3915
  * @return The response from generating content.
3880
3916
  *
3881
3917
  * @example
3882
3918
  * ```ts
3883
- * const response = await client.models.generateContentStream({
3919
+ * const response = await ai.models.generateContentStream({
3884
3920
  * model: 'gemini-2.0-flash',
3885
3921
  * contents: 'why is the sky blue?',
3886
3922
  * config: {
@@ -4039,14 +4075,12 @@ class Models extends BaseModule {
4039
4075
  /**
4040
4076
  * Calculates embeddings for the given contents. Only text is supported.
4041
4077
  *
4042
- * @param model - The model to use.
4043
- * @param contents - The contents to embed.
4044
- * @param [config] - The config for embedding contents.
4078
+ * @param params - The parameters for embedding contents.
4045
4079
  * @return The response from the API.
4046
4080
  *
4047
4081
  * @example
4048
4082
  * ```ts
4049
- * const response = await client.models.embedContent({
4083
+ * const response = await ai.models.embedContent({
4050
4084
  * model: 'text-embedding-004',
4051
4085
  * contents: [
4052
4086
  * 'What is your name?',
@@ -4118,14 +4152,12 @@ class Models extends BaseModule {
4118
4152
  /**
4119
4153
  * Generates an image based on a text description and configuration.
4120
4154
  *
4121
- * @param model - The model to use.
4122
- * @param prompt - A text description of the image to generate.
4123
- * @param [config] - The config for image generation.
4155
+ * @param params - The parameters for generating images.
4124
4156
  * @return The response from the API.
4125
4157
  *
4126
4158
  * @example
4127
4159
  * ```ts
4128
- * const response = await client.models.generateImages({
4160
+ * const response = await ai.models.generateImages({
4129
4161
  * model: 'imagen-3.0-generate-002',
4130
4162
  * prompt: 'Robot holding a red skateboard',
4131
4163
  * config: {
@@ -4196,14 +4228,12 @@ class Models extends BaseModule {
4196
4228
  * Counts the number of tokens in the given contents. Multimodal input is
4197
4229
  * supported for Gemini models.
4198
4230
  *
4199
- * @param model - The model to use for counting tokens.
4200
- * @param contents - The contents to count tokens for.
4201
- * @param [config] - The config for counting tokens.
4231
+ * @param params - The parameters for counting tokens.
4202
4232
  * @return The response from the API.
4203
4233
  *
4204
4234
  * @example
4205
4235
  * ```ts
4206
- * const response = await client.models.countTokens({
4236
+ * const response = await ai.models.countTokens({
4207
4237
  * model: 'gemini-2.0-flash',
4208
4238
  * contents: 'The quick brown fox jumps over the lazy dog.'
4209
4239
  * });
@@ -4272,14 +4302,12 @@ class Models extends BaseModule {
4272
4302
  *
4273
4303
  * This method is not supported by the Gemini Developer API.
4274
4304
  *
4275
- * @param model - The model to use.
4276
- * @param contents - The content to compute tokens for.
4277
- * @param [config] - The config for computing tokens.
4305
+ * @param params - The parameters for computing tokens.
4278
4306
  * @return The response from the API.
4279
4307
  *
4280
4308
  * @example
4281
4309
  * ```ts
4282
- * const response = await client.models.computeTokens({
4310
+ * const response = await ai.models.computeTokens({
4283
4311
  * model: 'gemini-2.0-flash',
4284
4312
  * contents: 'What is your name?'
4285
4313
  * });
@@ -6258,7 +6286,7 @@ function computeTokensResponseFromVertex(apiClient, fromObject) {
6258
6286
 
6259
6287
  /**
6260
6288
  * @license
6261
- * Copyright 2024 Google LLC
6289
+ * Copyright 2025 Google LLC
6262
6290
  * SPDX-License-Identifier: Apache-2.0
6263
6291
  */
6264
6292
  const FUNCTION_RESPONSE_REQUIRES_ID = 'FunctionResponse request must have an `id` field from the response of a ToolCall.FunctionalCalls in Google AI.';
@@ -6519,6 +6547,12 @@ function liveServerMessageFromVertex(apiClient, fromObject) {
6519
6547
  /**
6520
6548
  * Handles incoming messages from the WebSocket.
6521
6549
  *
6550
+ * @remarks
6551
+ * This function is responsible for parsing incoming messages, transforming them
6552
+ * into LiveServerMessages, and then calling the onmessage callback. Note that
6553
+ * the first message which is received from the server is a setupComplete
6554
+ * message.
6555
+ *
6522
6556
  * @param apiClient The ApiClient instance.
6523
6557
  * @param onmessage The user-provided onmessage callback (if any).
6524
6558
  * @param event The MessageEvent from the WebSocket.
@@ -6556,20 +6590,14 @@ class Live {
6556
6590
  Establishes a connection to the specified model with the given
6557
6591
  configuration and returns a Session object representing that connection.
6558
6592
 
6559
- > [!CAUTION] This SDK does not yet support the live API for **Google Vertex AI**.
6560
-
6561
6593
  @experimental
6562
6594
 
6563
- @param model - Model to use for the Live session.
6564
- @param config - Configuration parameters for the Live session.
6565
- @param callbacks - Optional callbacks for websocket events. If not
6566
- provided, default no-op callbacks will be used. Generally, prefer to
6567
- provide explicit callbacks to allow for proper handling of websocket
6568
- events (e.g. connection errors).
6595
+ @param params - The parameters for establishing a connection to the model.
6596
+ @return A live session.
6569
6597
 
6570
6598
  @example
6571
6599
  ```ts
6572
- const session = await client.live.connect({
6600
+ const session = await ai.live.connect({
6573
6601
  model: 'gemini-2.0-flash-exp',
6574
6602
  config: {
6575
6603
  responseModalities: [Modality.AUDIO],
@@ -6593,9 +6621,6 @@ class Live {
6593
6621
  */
6594
6622
  async connect(params) {
6595
6623
  var _a, _b;
6596
- if (this.apiClient.isVertexAI()) {
6597
- throw new Error('This SDK does not yet support the live API for **Google Vertex AI**.');
6598
- }
6599
6624
  const websocketBaseUrl = this.apiClient.getWebsocketBaseUrl();
6600
6625
  const apiVersion = this.apiClient.getApiVersion();
6601
6626
  let url;
@@ -6671,8 +6696,7 @@ class Session {
6671
6696
  if (params.turns !== null && params.turns !== undefined) {
6672
6697
  let contents = [];
6673
6698
  try {
6674
- contents =
6675
- tContents(apiClient, params.turns);
6699
+ contents = tContents(apiClient, params.turns);
6676
6700
  if (apiClient.isVertexAI()) {
6677
6701
  contents = contents.map((item) => contentToVertex(apiClient, item));
6678
6702
  }
@@ -6712,8 +6736,10 @@ class Session {
6712
6736
  throw new Error('functionResponses is required.');
6713
6737
  }
6714
6738
  for (const functionResponse of functionResponses) {
6715
- if (typeof functionResponse !== 'object' || functionResponse === null ||
6716
- !('name' in functionResponse) || !('response' in functionResponse)) {
6739
+ if (typeof functionResponse !== 'object' ||
6740
+ functionResponse === null ||
6741
+ !('name' in functionResponse) ||
6742
+ !('response' in functionResponse)) {
6717
6743
  throw new Error(`Could not parse function response, type '${typeof functionResponse}'.`);
6718
6744
  }
6719
6745
  if (!apiClient.isVertexAI() && !('id' in functionResponse)) {
@@ -6721,7 +6747,7 @@ class Session {
6721
6747
  }
6722
6748
  }
6723
6749
  const clientMessage = {
6724
- toolResponse: { functionResponses: functionResponses }
6750
+ toolResponse: { functionResponses: functionResponses },
6725
6751
  };
6726
6752
  return clientMessage;
6727
6753
  }
@@ -6839,7 +6865,7 @@ class Session {
6839
6865
 
6840
6866
  @example
6841
6867
  ```ts
6842
- const session = await client.live.connect({
6868
+ const session = await ai.live.connect({
6843
6869
  model: 'gemini-2.0-flash-exp',
6844
6870
  config: {
6845
6871
  responseModalities: [Modality.AUDIO],