@google/genai 0.4.0 → 0.6.0

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/README.md CHANGED
@@ -8,24 +8,25 @@
8
8
 
9
9
  ----------------------
10
10
 
11
- The Google Gen AI JavaScript SDK is an **experimental SDK** designed for
11
+ The Google Gen AI JavaScript SDK is designed for
12
12
  TypeScript and JavaScript developers to build applications powered by Gemini. The SDK
13
13
  supports both the [Gemini Developer API](https://ai.google.dev/gemini-api/docs)
14
14
  and [Vertex AI](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/overview).
15
15
 
16
16
  The Google Gen AI SDK is designed to work with Gemini 2.0 features.
17
17
 
18
- > [!CAUTION] **Experimental SDK:** This SDK is under active development and may
19
- experience breaking changes.
18
+ > [!NOTE]
19
+ > **SDK Preview:**
20
+ > See: [Preview Launch](#preview-launch).
20
21
 
21
- > [!CAUTION] **API Key Security:** Avoid exposing API keys in client-side code.
22
- Use server-side implementations in production environments.
22
+ > [!CAUTION]
23
+ > **API Key Security:** Avoid exposing API keys in client-side code.
24
+ > Use server-side implementations in production environments.
23
25
 
24
26
 
25
27
  ## Prerequisites
26
28
 
27
29
  * Node.js version 18 or later
28
- * `pnpm` or `npm`
29
30
 
30
31
  ## Installation
31
32
 
@@ -62,30 +63,31 @@ main();
62
63
  The package contents are also available unzipped in the
63
64
  `package/` directory of the bucket, so an equivalent web example is:
64
65
 
65
- ```
66
+ ```html
66
67
  <!DOCTYPE html>
67
68
  <html lang="en">
68
- <head>
69
- <meta charset="UTF-8">
70
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
69
+ <head>
70
+ <meta charset="UTF-8" />
71
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
71
72
  <title>Using My Package</title>
72
- </head>
73
- <body>
73
+ </head>
74
+ <body>
74
75
  <script type="module">
75
- import {GoogleGenAI, Type} from 'dist/web/index.mjs';
76
- const ai = new GoogleGenAI({apiKey:"YOUR_API_KEY"});
77
-
78
- async function main() {
79
- const response = await ai.models.generateContent({
80
- model: 'gemini-2.0-flash-001',
81
- contents: 'Why is the sky blue?',
82
- });
83
- console.log(response.text());
84
- }
76
+ import {GoogleGenAI} from 'https://cdn.jsdelivr.net/npm/@google/genai@latest/+esm'
77
+
78
+ const ai = new GoogleGenAI({apiKey:"GEMINI_API_KEY"});
85
79
 
86
- main();
80
+ async function main() {
81
+ const response = await ai.models.generateContent({
82
+ model: 'gemini-2.0-flash-001',
83
+ contents: 'Why is the sky blue?',
84
+ });
85
+ console.log(response.text);
86
+ }
87
+
88
+ main();
87
89
  </script>
88
- </body>
90
+ </body>
89
91
  </html>
90
92
  ```
91
93
 
@@ -108,8 +110,9 @@ const ai = new GoogleGenAI({apiKey: 'GEMINI_API_KEY'});
108
110
 
109
111
  #### Browser
110
112
 
111
- > [!CAUTION] **API Key Security:** Avoid exposing API keys in client-side code.
112
- Use server-side implementations in production environments.
113
+ > [!CAUTION]
114
+ > **API Key Security:** Avoid exposing API keys in client-side code.
115
+ > Use server-side implementations in production environments.
113
116
 
114
117
  In the browser the initialization code is identical:
115
118
 
@@ -138,26 +141,26 @@ const ai = new GoogleGenAI({
138
141
  All API features are accessed through an instance of the `GoogleGenAI` classes.
139
142
  The submodules bundle together related API methods:
140
143
 
141
- - [`client.models`](https://googleapis.github.io/js-genai/classes/models.Models.html):
144
+ - [`ai.models`](https://googleapis.github.io/js-genai/classes/models.Models.html):
142
145
  Use `models` to query models (`generateContent`, `generateImages`, ...), or
143
146
  examine their metadata.
144
- - [`client.caches`](https://googleapis.github.io/js-genai/classes/caches.Caches.html):
147
+ - [`ai.caches`](https://googleapis.github.io/js-genai/classes/caches.Caches.html):
145
148
  Create and manage `caches` to reduce costs when repeatedly using the same
146
149
  large prompt prefix.
147
- - [`client.chats`](https://googleapis.github.io/js-genai/classes/chats.Chats.html):
150
+ - [`ai.chats`](https://googleapis.github.io/js-genai/classes/chats.Chats.html):
148
151
  Create local stateful `chat` objects to simplify multi turn interactions.
149
- - [`client.files`](https://googleapis.github.io/js-genai/classes/files.Files.html):
152
+ - [`ai.files`](https://googleapis.github.io/js-genai/classes/files.Files.html):
150
153
  Upload `files` to the API and reference them in your prompts.
151
- This reduces bandwidth if you use a file many times, and handles files too
154
+ This reduces bandwidth if you use a file many times, and handles files too
152
155
  large to fit inline with your prompt.
153
- - [`client.live`](https://googleapis.github.io/js-genai/classes/live.Live.html):
156
+ - [`ai.live`](https://googleapis.github.io/js-genai/classes/live.Live.html):
154
157
  Start a `live` session for real time interaction, allows text + audio + video
155
158
  input, and text or audio output.
156
159
 
157
160
  ## Samples
158
161
 
159
162
  More samples can be found in the
160
- [github samples directory](https://github.com/google-gemini/generative-ai-js/tree/main/sdk-samples).
163
+ [github samples directory](https://github.com/googleapis/js-genai/tree/main/sdk-samples).
161
164
 
162
165
 
163
166
  ### Streaming
@@ -242,3 +245,11 @@ async function main() {
242
245
 
243
246
  main();
244
247
  ```
248
+
249
+
250
+ ## Preview Launch
251
+
252
+ The SDK is curently in a preview launch stage, per [Google's launch stages](https://cloud.google.com/products?hl=en#section-22) this means:
253
+
254
+ > At Preview, products or features are ready for testing by customers. Preview offerings are often publicly announced, but are not necessarily feature-complete, and no SLAs or technical support commitments are provided for these. Unless stated otherwise by Google, Preview offerings are intended for use in test environments only. The average Preview stage lasts about six months.
255
+
package/dist/genai.d.ts CHANGED
@@ -261,10 +261,12 @@ export declare interface Candidate {
261
261
  /** Number of tokens for this candidate.
262
262
  */
263
263
  tokenCount?: number;
264
+ /** The reason why the model stopped generating tokens.
265
+ If empty, the model has not stopped generating the tokens.
266
+ */
267
+ finishReason?: FinishReason;
264
268
  /** Output only. Average log probability score of the candidate. */
265
269
  avgLogprobs?: number;
266
- /** Output only. The reason why the model stopped generating tokens. If empty, the model has not stopped generating the tokens. */
267
- finishReason?: FinishReason;
268
270
  /** Output only. Metadata specifies sources used to ground generated content. */
269
271
  groundingMetadata?: GroundingMetadata;
270
272
  /** Output only. Index of the candidate. */
@@ -375,7 +377,7 @@ export declare class Chats {
375
377
  * @remarks
376
378
  * The config in the params will be used for all requests within the chat
377
379
  * session unless overridden by a per-request `config` in
378
- * {@link ./types.SendMessageParameters}.
380
+ * @see {@link types.SendMessageParameters#config}.
379
381
  *
380
382
  * @param params - Parameters for creating a chat session.
381
383
  * @returns A new chat session.
@@ -496,12 +498,6 @@ export declare interface ContentEmbeddingStatistics {
496
498
 
497
499
  export declare type ContentListUnion = ContentUnion[] | ContentUnion;
498
500
 
499
- /** @internal */
500
- export declare function contentToMldev(apiClient: ApiClient, fromObject: types.Content): Record<string, unknown>;
501
-
502
- /** @internal */
503
- export declare function contentToVertex(apiClient: ApiClient, fromObject: types.Content): Record<string, unknown>;
504
-
505
501
  export declare type ContentUnion = Content | PartUnion[] | PartUnion;
506
502
 
507
503
  /** Configuration for a Control reference image. */
@@ -989,7 +985,8 @@ export declare enum FinishReason {
989
985
  BLOCKLIST = "BLOCKLIST",
990
986
  PROHIBITED_CONTENT = "PROHIBITED_CONTENT",
991
987
  SPII = "SPII",
992
- MALFORMED_FUNCTION_CALL = "MALFORMED_FUNCTION_CALL"
988
+ MALFORMED_FUNCTION_CALL = "MALFORMED_FUNCTION_CALL",
989
+ IMAGE_SAFETY = "IMAGE_SAFETY"
993
990
  }
994
991
 
995
992
  declare function formatMap(templateString: string, valueMap: Record<string, unknown>): string;
@@ -1317,13 +1314,25 @@ export declare class GenerateContentResponsePromptFeedback {
1317
1314
 
1318
1315
  /** Usage metadata about response(s). */
1319
1316
  export declare class GenerateContentResponseUsageMetadata {
1317
+ /** Output only. List of modalities of the cached content in the request input. */
1318
+ cacheTokensDetails?: ModalityTokenCount[];
1320
1319
  /** Output only. Number of tokens in the cached part in the input (the cached content). */
1321
1320
  cachedContentTokenCount?: number;
1322
1321
  /** Number of tokens in the response(s). */
1323
1322
  candidatesTokenCount?: number;
1323
+ /** Output only. List of modalities that were returned in the response. */
1324
+ candidatesTokensDetails?: ModalityTokenCount[];
1324
1325
  /** Number of tokens in the request. When `cached_content` is set, this is still the total effective prompt size meaning this includes the number of tokens in the cached content. */
1325
1326
  promptTokenCount?: number;
1326
- /** Total token count for prompt and response candidates. */
1327
+ /** Output only. List of modalities that were processed in the request input. */
1328
+ promptTokensDetails?: ModalityTokenCount[];
1329
+ /** Output only. Number of tokens present in thoughts output. */
1330
+ thoughtsTokenCount?: number;
1331
+ /** Output only. Number of tokens present in tool-use prompt(s). */
1332
+ toolUsePromptTokenCount?: number;
1333
+ /** Output only. List of modalities that were processed for tool-use request inputs. */
1334
+ toolUsePromptTokensDetails?: ModalityTokenCount[];
1335
+ /** Total token count for prompt, response candidates, and tool-use prompts (if present). */
1327
1336
  totalTokenCount?: number;
1328
1337
  }
1329
1338
 
@@ -1883,6 +1892,11 @@ export declare class Live {
1883
1892
 
1884
1893
  @experimental
1885
1894
 
1895
+ @remarks
1896
+ If using the Gemini API, Live is currently only supported behind API
1897
+ version `v1alpha`. Ensure that the API version is set to `v1alpha` when
1898
+ initializing the SDK if relying on the Gemini API.
1899
+
1886
1900
  @param params - The parameters for establishing a connection to the model.
1887
1901
  @return A live session.
1888
1902
 
@@ -2055,6 +2069,28 @@ export declare interface LiveConnectParameters {
2055
2069
  config?: LiveConnectConfig;
2056
2070
  }
2057
2071
 
2072
+ /** Parameters for sending client content to the live API. */
2073
+ export declare interface LiveSendClientContentParameters {
2074
+ /** Client content to send to the session. */
2075
+ turns?: ContentListUnion;
2076
+ /** If true, indicates that the server content generation should start with
2077
+ the currently accumulated prompt. Otherwise, the server will await
2078
+ additional messages before starting generation. */
2079
+ turnComplete?: boolean;
2080
+ }
2081
+
2082
+ /** Parameters for sending realtime input to the live API. */
2083
+ export declare interface LiveSendRealtimeInputParameters {
2084
+ /** Realtime input to send to the session. */
2085
+ media: Blob_2;
2086
+ }
2087
+
2088
+ /** Parameters for sending tool responses to the live API. */
2089
+ export declare class LiveSendToolResponseParameters {
2090
+ /** Tool responses to send to the session. */
2091
+ functionResponses: FunctionResponse[] | FunctionResponse;
2092
+ }
2093
+
2058
2094
  /** Incremental server update generated by the model in response to client messages.
2059
2095
 
2060
2096
  Content is generated as quickly as possible, and not in real time. Clients
@@ -2182,6 +2218,14 @@ export declare enum Modality {
2182
2218
  AUDIO = "AUDIO"
2183
2219
  }
2184
2220
 
2221
+ /** Represents token counting info for a single modality. */
2222
+ export declare interface ModalityTokenCount {
2223
+ /** The modality associated with this token count. */
2224
+ modality?: Modality;
2225
+ /** Number of tokens. */
2226
+ tokenCount?: number;
2227
+ }
2228
+
2185
2229
  export declare enum Mode {
2186
2230
  MODE_UNSPECIFIED = "MODE_UNSPECIFIED",
2187
2231
  MODE_DYNAMIC = "MODE_DYNAMIC"
@@ -2371,7 +2415,7 @@ export declare enum Outcome {
2371
2415
  * SPDX-License-Identifier: Apache-2.0
2372
2416
  */
2373
2417
  /**
2374
- * @fileoverview Pagers for the GenAI List APIs.
2418
+ * Pagers for the GenAI List APIs.
2375
2419
  */
2376
2420
  declare enum PagedItem {
2377
2421
  PAGED_ITEM_BATCH_JOBS = "batchJobs",
@@ -2752,12 +2796,13 @@ export declare class Session {
2752
2796
  `turnComplete`.
2753
2797
 
2754
2798
  - `turns` will be converted to a `Content[]`
2755
- - `turnComplete: true` indicates that you are done sending content and
2756
- expect a response.
2799
+ - `turnComplete: true` [default] indicates that you are done sending
2800
+ content and expect a response. If `turnComplete: false`, the server
2801
+ will wait for additional messages before starting generation.
2757
2802
 
2758
2803
  @experimental
2759
2804
 
2760
- @remanks
2805
+ @remarks
2761
2806
  There are two ways to send messages to the live API:
2762
2807
  `sendClientContent` and `sendRealtimeInput`.
2763
2808
 
@@ -2793,7 +2838,7 @@ export declare class Session {
2793
2838
  ```
2794
2839
  @experimental
2795
2840
  */
2796
- sendClientContent(params: types.SessionSendClientContentParameters): void;
2841
+ sendClientContent(params: types.LiveSendClientContentParameters): void;
2797
2842
  /**
2798
2843
  Send a realtime message over the established connection.
2799
2844
 
@@ -2803,7 +2848,7 @@ export declare class Session {
2803
2848
 
2804
2849
  @experimental
2805
2850
 
2806
- @remanks
2851
+ @remarks
2807
2852
  Use `sendRealtimeInput` for realtime audio chunks and video frames (images).
2808
2853
 
2809
2854
  With `sendRealtimeInput` the api will respond to audio automatically
@@ -2816,7 +2861,7 @@ export declare class Session {
2816
2861
  Note: The Call signature expects a `Blob` object, but only a subset
2817
2862
  of audio and image mimetypes are allowed.
2818
2863
  */
2819
- sendRealtimeInput(params: types.SessionSendRealtimeInputParameters): void;
2864
+ sendRealtimeInput(params: types.LiveSendRealtimeInputParameters): void;
2820
2865
  /**
2821
2866
  Send a function response message over the established connection.
2822
2867
 
@@ -2824,14 +2869,14 @@ export declare class Session {
2824
2869
 
2825
2870
  - `functionResponses` will be converted to a `functionResponses[]`
2826
2871
 
2827
- @remanks
2872
+ @remarks
2828
2873
  Use `sendFunctionResponse` to reply to `LiveServerToolCall` from the server.
2829
2874
 
2830
- Use {@link LiveConnectConfig#tools} to configure the callable functions.
2875
+ Use {@link types.LiveConnectConfig#tools} to configure the callable functions.
2831
2876
 
2832
2877
  @experimental
2833
2878
  */
2834
- sendToolResponse(params: types.SessionSendToolResponseParameters): void;
2879
+ sendToolResponse(params: types.LiveSendToolResponseParameters): void;
2835
2880
  /**
2836
2881
  Terminates the WebSocket connection.
2837
2882
 
@@ -2852,28 +2897,6 @@ export declare class Session {
2852
2897
  close(): void;
2853
2898
  }
2854
2899
 
2855
- /** Parameters for sending client content to the live API. */
2856
- export declare interface SessionSendClientContentParameters {
2857
- /** Client content to send to the session. */
2858
- turns?: ContentListUnion;
2859
- /** If true, indicates that the server content generation should start with
2860
- the currently accumulated prompt. Otherwise, the server will await
2861
- additional messages before starting generation. */
2862
- turnComplete?: boolean;
2863
- }
2864
-
2865
- /** Parameters for sending realtime input to the live API. */
2866
- export declare interface SessionSendRealtimeInputParameters {
2867
- /** Realtime input to send to the session. */
2868
- media: Blob_2;
2869
- }
2870
-
2871
- /** Parameters for sending tool responses to the live API. */
2872
- export declare class SessionSendToolResponseParameters {
2873
- /** Tool responses to send to the session. */
2874
- functionResponses: FunctionResponse | FunctionResponse[];
2875
- }
2876
-
2877
2900
  declare function setValueByPath(data: Record<string, unknown>, keys: string[], value: unknown): void;
2878
2901
 
2879
2902
  /** The speech generation configuration. */
@@ -3021,12 +3044,6 @@ export declare interface ToolConfig {
3021
3044
 
3022
3045
  export declare type ToolListUnion = Tool[];
3023
3046
 
3024
- /** @internal */
3025
- export declare function toolToMldev(apiClient: ApiClient, fromObject: types.Tool): Record<string, unknown>;
3026
-
3027
- /** @internal */
3028
- export declare function toolToVertex(apiClient: ApiClient, fromObject: types.Tool): Record<string, unknown>;
3029
-
3030
3047
  export declare enum Type {
3031
3048
  TYPE_UNSPECIFIED = "TYPE_UNSPECIFIED",
3032
3049
  STRING = "STRING",
@@ -3059,6 +3076,7 @@ declare namespace types {
3059
3076
  HarmProbability,
3060
3077
  HarmSeverity,
3061
3078
  BlockedReason,
3079
+ Modality,
3062
3080
  State,
3063
3081
  DynamicRetrievalConfigMode,
3064
3082
  FunctionCallingConfigMode,
@@ -3071,7 +3089,6 @@ declare namespace types {
3071
3089
  MaskReferenceMode,
3072
3090
  ControlReferenceType,
3073
3091
  SubjectReferenceType,
3074
- Modality,
3075
3092
  VideoMetadata,
3076
3093
  CodeExecutionResult,
3077
3094
  ExecutableCode,
@@ -3122,6 +3139,7 @@ declare namespace types {
3122
3139
  SafetyRating,
3123
3140
  Candidate,
3124
3141
  GenerateContentResponsePromptFeedback,
3142
+ ModalityTokenCount,
3125
3143
  GenerateContentResponseUsageMetadata,
3126
3144
  GenerateContentResponse,
3127
3145
  EmbedContentConfig,
@@ -3203,9 +3221,9 @@ declare namespace types {
3203
3221
  LiveConnectParameters,
3204
3222
  CreateChatParameters,
3205
3223
  SendMessageParameters,
3206
- SessionSendClientContentParameters,
3207
- SessionSendRealtimeInputParameters,
3208
- SessionSendToolResponseParameters,
3224
+ LiveSendClientContentParameters,
3225
+ LiveSendRealtimeInputParameters,
3226
+ LiveSendToolResponseParameters,
3209
3227
  PartUnion,
3210
3228
  PartListUnion,
3211
3229
  ContentUnion,