@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.
@@ -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,7 +2799,7 @@ 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
  class Models extends BaseModule {
@@ -2791,14 +2829,12 @@ class Models extends BaseModule {
2791
2829
  *
2792
2830
  * Some models support multimodal input and output.
2793
2831
  *
2794
- * @param model - The model to use for generating content.
2795
- * @param contents - The input contents to use for generating content.
2796
- * @param [config] - The configuration for generating content.
2832
+ * @param params - The parameters for generating content.
2797
2833
  * @return The response from generating content.
2798
2834
  *
2799
2835
  * @example
2800
2836
  * ```ts
2801
- * const response = await client.models.generateContent({
2837
+ * const response = await ai.models.generateContent({
2802
2838
  * model: 'gemini-2.0-flash',
2803
2839
  * contents: 'why is the sky blue?',
2804
2840
  * config: {
@@ -2835,14 +2871,12 @@ class Models extends BaseModule {
2835
2871
  *
2836
2872
  * Some models support multimodal input and output.
2837
2873
  *
2838
- * @param model - The model to use for generating content.
2839
- * @param contents - The input contents to use for generating content.
2840
- * @param [config] - The configuration for generating content.
2874
+ * @param params - The parameters for generating content with streaming response.
2841
2875
  * @return The response from generating content.
2842
2876
  *
2843
2877
  * @example
2844
2878
  * ```ts
2845
- * const response = await client.models.generateContentStream({
2879
+ * const response = await ai.models.generateContentStream({
2846
2880
  * model: 'gemini-2.0-flash',
2847
2881
  * contents: 'why is the sky blue?',
2848
2882
  * config: {
@@ -3001,14 +3035,12 @@ class Models extends BaseModule {
3001
3035
  /**
3002
3036
  * Calculates embeddings for the given contents. Only text is supported.
3003
3037
  *
3004
- * @param model - The model to use.
3005
- * @param contents - The contents to embed.
3006
- * @param [config] - The config for embedding contents.
3038
+ * @param params - The parameters for embedding contents.
3007
3039
  * @return The response from the API.
3008
3040
  *
3009
3041
  * @example
3010
3042
  * ```ts
3011
- * const response = await client.models.embedContent({
3043
+ * const response = await ai.models.embedContent({
3012
3044
  * model: 'text-embedding-004',
3013
3045
  * contents: [
3014
3046
  * 'What is your name?',
@@ -3080,14 +3112,12 @@ class Models extends BaseModule {
3080
3112
  /**
3081
3113
  * Generates an image based on a text description and configuration.
3082
3114
  *
3083
- * @param model - The model to use.
3084
- * @param prompt - A text description of the image to generate.
3085
- * @param [config] - The config for image generation.
3115
+ * @param params - The parameters for generating images.
3086
3116
  * @return The response from the API.
3087
3117
  *
3088
3118
  * @example
3089
3119
  * ```ts
3090
- * const response = await client.models.generateImages({
3120
+ * const response = await ai.models.generateImages({
3091
3121
  * model: 'imagen-3.0-generate-002',
3092
3122
  * prompt: 'Robot holding a red skateboard',
3093
3123
  * config: {
@@ -3158,14 +3188,12 @@ class Models extends BaseModule {
3158
3188
  * Counts the number of tokens in the given contents. Multimodal input is
3159
3189
  * supported for Gemini models.
3160
3190
  *
3161
- * @param model - The model to use for counting tokens.
3162
- * @param contents - The contents to count tokens for.
3163
- * @param [config] - The config for counting tokens.
3191
+ * @param params - The parameters for counting tokens.
3164
3192
  * @return The response from the API.
3165
3193
  *
3166
3194
  * @example
3167
3195
  * ```ts
3168
- * const response = await client.models.countTokens({
3196
+ * const response = await ai.models.countTokens({
3169
3197
  * model: 'gemini-2.0-flash',
3170
3198
  * contents: 'The quick brown fox jumps over the lazy dog.'
3171
3199
  * });
@@ -3234,14 +3262,12 @@ class Models extends BaseModule {
3234
3262
  *
3235
3263
  * This method is not supported by the Gemini Developer API.
3236
3264
  *
3237
- * @param model - The model to use.
3238
- * @param contents - The content to compute tokens for.
3239
- * @param [config] - The config for computing tokens.
3265
+ * @param params - The parameters for computing tokens.
3240
3266
  * @return The response from the API.
3241
3267
  *
3242
3268
  * @example
3243
3269
  * ```ts
3244
- * const response = await client.models.computeTokens({
3270
+ * const response = await ai.models.computeTokens({
3245
3271
  * model: 'gemini-2.0-flash',
3246
3272
  * contents: 'What is your name?'
3247
3273
  * });
@@ -5220,7 +5246,7 @@ function computeTokensResponseFromVertex(apiClient, fromObject) {
5220
5246
 
5221
5247
  /**
5222
5248
  * @license
5223
- * Copyright 2024 Google LLC
5249
+ * Copyright 2025 Google LLC
5224
5250
  * SPDX-License-Identifier: Apache-2.0
5225
5251
  */
5226
5252
  const FUNCTION_RESPONSE_REQUIRES_ID = 'FunctionResponse request must have an `id` field from the response of a ToolCall.FunctionalCalls in Google AI.';
@@ -5481,6 +5507,12 @@ function liveServerMessageFromVertex(apiClient, fromObject) {
5481
5507
  /**
5482
5508
  * Handles incoming messages from the WebSocket.
5483
5509
  *
5510
+ * @remarks
5511
+ * This function is responsible for parsing incoming messages, transforming them
5512
+ * into LiveServerMessages, and then calling the onmessage callback. Note that
5513
+ * the first message which is received from the server is a setupComplete
5514
+ * message.
5515
+ *
5484
5516
  * @param apiClient The ApiClient instance.
5485
5517
  * @param onmessage The user-provided onmessage callback (if any).
5486
5518
  * @param event The MessageEvent from the WebSocket.
@@ -5518,20 +5550,14 @@ class Live {
5518
5550
  Establishes a connection to the specified model with the given
5519
5551
  configuration and returns a Session object representing that connection.
5520
5552
 
5521
- > [!CAUTION] This SDK does not yet support the live API for **Google Vertex AI**.
5522
-
5523
5553
  @experimental
5524
5554
 
5525
- @param model - Model to use for the Live session.
5526
- @param config - Configuration parameters for the Live session.
5527
- @param callbacks - Optional callbacks for websocket events. If not
5528
- provided, default no-op callbacks will be used. Generally, prefer to
5529
- provide explicit callbacks to allow for proper handling of websocket
5530
- events (e.g. connection errors).
5555
+ @param params - The parameters for establishing a connection to the model.
5556
+ @return A live session.
5531
5557
 
5532
5558
  @example
5533
5559
  ```ts
5534
- const session = await client.live.connect({
5560
+ const session = await ai.live.connect({
5535
5561
  model: 'gemini-2.0-flash-exp',
5536
5562
  config: {
5537
5563
  responseModalities: [Modality.AUDIO],
@@ -5555,9 +5581,6 @@ class Live {
5555
5581
  */
5556
5582
  async connect(params) {
5557
5583
  var _a, _b;
5558
- if (this.apiClient.isVertexAI()) {
5559
- throw new Error('This SDK does not yet support the live API for **Google Vertex AI**.');
5560
- }
5561
5584
  const websocketBaseUrl = this.apiClient.getWebsocketBaseUrl();
5562
5585
  const apiVersion = this.apiClient.getApiVersion();
5563
5586
  let url;
@@ -5633,8 +5656,7 @@ class Session {
5633
5656
  if (params.turns !== null && params.turns !== undefined) {
5634
5657
  let contents = [];
5635
5658
  try {
5636
- contents =
5637
- tContents(apiClient, params.turns);
5659
+ contents = tContents(apiClient, params.turns);
5638
5660
  if (apiClient.isVertexAI()) {
5639
5661
  contents = contents.map((item) => contentToVertex(apiClient, item));
5640
5662
  }
@@ -5674,8 +5696,10 @@ class Session {
5674
5696
  throw new Error('functionResponses is required.');
5675
5697
  }
5676
5698
  for (const functionResponse of functionResponses) {
5677
- if (typeof functionResponse !== 'object' || functionResponse === null ||
5678
- !('name' in functionResponse) || !('response' in functionResponse)) {
5699
+ if (typeof functionResponse !== 'object' ||
5700
+ functionResponse === null ||
5701
+ !('name' in functionResponse) ||
5702
+ !('response' in functionResponse)) {
5679
5703
  throw new Error(`Could not parse function response, type '${typeof functionResponse}'.`);
5680
5704
  }
5681
5705
  if (!apiClient.isVertexAI() && !('id' in functionResponse)) {
@@ -5683,7 +5707,7 @@ class Session {
5683
5707
  }
5684
5708
  }
5685
5709
  const clientMessage = {
5686
- toolResponse: { functionResponses: functionResponses }
5710
+ toolResponse: { functionResponses: functionResponses },
5687
5711
  };
5688
5712
  return clientMessage;
5689
5713
  }
@@ -5801,7 +5825,7 @@ class Session {
5801
5825
 
5802
5826
  @example
5803
5827
  ```ts
5804
- const session = await client.live.connect({
5828
+ const session = await ai.live.connect({
5805
5829
  model: 'gemini-2.0-flash-exp',
5806
5830
  config: {
5807
5831
  responseModalities: [Modality.AUDIO],
@@ -5838,13 +5862,13 @@ function mapToHeaders(map) {
5838
5862
 
5839
5863
  /**
5840
5864
  * @license
5841
- * Copyright 2024 Google LLC
5865
+ * Copyright 2025 Google LLC
5842
5866
  * SPDX-License-Identifier: Apache-2.0
5843
5867
  */
5844
5868
  const CONTENT_TYPE_HEADER = 'Content-Type';
5845
5869
  const USER_AGENT_HEADER = 'User-Agent';
5846
5870
  const GOOGLE_API_CLIENT_HEADER = 'x-goog-api-client';
5847
- const SDK_VERSION = '0.2.0'; // x-release-please-version
5871
+ const SDK_VERSION = '0.3.1'; // x-release-please-version
5848
5872
  const LIBRARY_LABEL = `google-genai-sdk/${SDK_VERSION}`;
5849
5873
  const VERTEX_AI_API_DEFAULT_VERSION = 'v1beta1';
5850
5874
  const GOOGLE_AI_API_DEFAULT_VERSION = 'v1beta';
@@ -6056,7 +6080,7 @@ class ApiClient {
6056
6080
  async unaryApiCall(url, requestInit, httpMethod) {
6057
6081
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
6058
6082
  .then(async (response) => {
6059
- await throwErrorIfNotOK(response, url.toString());
6083
+ await throwErrorIfNotOK(response);
6060
6084
  return new HttpResponse(response);
6061
6085
  })
6062
6086
  .catch((e) => {
@@ -6071,7 +6095,7 @@ class ApiClient {
6071
6095
  async streamApiCall(url, requestInit, httpMethod) {
6072
6096
  return this.apiCall(url.toString(), Object.assign(Object.assign({}, requestInit), { method: httpMethod }))
6073
6097
  .then(async (response) => {
6074
- await throwErrorIfNotOK(response, url.toString());
6098
+ await throwErrorIfNotOK(response);
6075
6099
  return this.processStreamResponse(response);
6076
6100
  })
6077
6101
  .catch((e) => {
@@ -6216,7 +6240,7 @@ class ApiClient {
6216
6240
  return uploadUrl;
6217
6241
  }
6218
6242
  }
6219
- async function throwErrorIfNotOK(response, url, requestInit) {
6243
+ async function throwErrorIfNotOK(response) {
6220
6244
  var _a;
6221
6245
  if (response === undefined) {
6222
6246
  throw new ServerError('response is undefined');
@@ -6252,7 +6276,7 @@ async function throwErrorIfNotOK(response, url, requestInit) {
6252
6276
 
6253
6277
  /**
6254
6278
  * @license
6255
- * Copyright 2024 Google LLC
6279
+ * Copyright 2025 Google LLC
6256
6280
  * SPDX-License-Identifier: Apache-2.0
6257
6281
  */
6258
6282
  class Files extends BaseModule {
@@ -6270,7 +6294,7 @@ class Files extends BaseModule {
6270
6294
  * size of each page is 10.
6271
6295
  *
6272
6296
  * ```ts
6273
- * const listResponse = await client.files.list({config: {'pageSize': 10}});
6297
+ * const listResponse = await ai.files.list({config: {'pageSize': 10}});
6274
6298
  * for await (const file of listResponse) {
6275
6299
  * console.log(file.name);
6276
6300
  * }
@@ -6303,30 +6327,32 @@ class Files extends BaseModule {
6303
6327
  *
6304
6328
  * This section can contain multiple paragraphs and code examples.
6305
6329
  *
6306
- * @param file The string path to the file to be uploaded or a Blob object.
6307
- * @param config Optional parameters specified in the `types.UploadFileConfig`
6308
- * interface. Optional @see {@link types.UploadFileConfig}
6330
+ * @param params - Optional parameters specified in the
6331
+ * `common.UploadFileParameters` interface.
6332
+ * Optional @see {@link common.UploadFileParameters}
6309
6333
  * @return A promise that resolves to a `types.File` object.
6310
6334
  * @throws An error if called on a Vertex AI client.
6311
6335
  * @throws An error if the `mimeType` is not provided and can not be inferred,
6312
- * the `mimeType` can be provided in the `config` parameter.
6336
+ * the `mimeType` can be provided in the `params.config` parameter.
6313
6337
  * @throws An error occurs if a suitable upload location cannot be established.
6314
6338
  *
6315
6339
  * @example
6316
6340
  * The following code uploads a file to Gemini API.
6317
6341
  *
6318
6342
  * ```ts
6319
- * const file = await client.files.upload('file.txt', {
6343
+ * const file = await ai.files.upload({file: 'file.txt', config: {
6320
6344
  * mimeType: 'text/plain',
6321
- * });
6345
+ * }});
6322
6346
  * console.log(file.name);
6323
6347
  * ```
6324
6348
  */
6325
- async upload(file, config) {
6349
+ async upload(params) {
6326
6350
  if (this.apiClient.isVertexAI()) {
6327
6351
  throw new Error('Vertex AI does not support uploading files. You can share files through a GCS bucket.');
6328
6352
  }
6329
- return this.apiClient.uploadFile(file, config).then((response) => {
6353
+ return this.apiClient
6354
+ .uploadFile(params.file, params.config)
6355
+ .then((response) => {
6330
6356
  const file = fileFromMldev(this.apiClient, response);
6331
6357
  return file;
6332
6358
  });
@@ -6454,7 +6480,7 @@ class Files extends BaseModule {
6454
6480
  * const config: GetFileParameters = {
6455
6481
  * name: fileName,
6456
6482
  * };
6457
- * file = await client.files.get(config);
6483
+ * file = await ai.files.get(config);
6458
6484
  * console.log(file.name);
6459
6485
  * ```
6460
6486
  */
@@ -6961,14 +6987,14 @@ const LANGUAGE_LABEL_PREFIX = 'gl-node/';
6961
6987
  * Initializing the SDK for using the Gemini API:
6962
6988
  * ```ts
6963
6989
  * import {GoogleGenAI} from '@google/genai';
6964
- * const ai = genai.GoogleGenAI({apiKey: 'GEMINI_API_KEY'});
6990
+ * const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'});
6965
6991
  * ```
6966
6992
  *
6967
6993
  * @example
6968
6994
  * Initializing the SDK for using the Vertex AI API:
6969
6995
  * ```ts
6970
6996
  * import {GoogleGenAI} from '@google/genai';
6971
- * const ai = genai.GoogleGenAI({
6997
+ * const ai = new GoogleGenAI({
6972
6998
  * vertexai: true,
6973
6999
  * project: 'PROJECT_ID',
6974
7000
  * location: 'PROJECT_LOCATION'