diff --git a/.changeset/plenty-jeans-return.md b/.changeset/plenty-jeans-return.md new file mode 100644 index 000000000..968aa18ba --- /dev/null +++ b/.changeset/plenty-jeans-return.md @@ -0,0 +1,5 @@ +--- +"@google/generative-ai": patch +--- + +Remove unsupported minItems and maxItems properties from ArraySchema and add validation to prevent their usage diff --git a/docs/reference/main/generative-ai.arrayschema.md b/docs/reference/main/generative-ai.arrayschema.md index 56438ad1c..bb440eabe 100644 --- a/docs/reference/main/generative-ai.arrayschema.md +++ b/docs/reference/main/generative-ai.arrayschema.md @@ -1,24 +1,14 @@ - +# ArraySchema Interface -[Home](./index.md) > [@google/generative-ai](./generative-ai.md) > [ArraySchema](./generative-ai.arrayschema.md) +Represents the schema for array responses. -## ArraySchema interface - -Describes an array, an ordered list of values. - -**Signature:** - -```typescript -export interface ArraySchema extends BaseSchema -``` -**Extends:** BaseSchema +Note: The properties `minItems` and `maxItems` are not currently supported by the API. Attempting to use these properties will result in an error. ## Properties -| Property | Modifiers | Type | Description | -| --- | --- | --- | --- | -| [items](./generative-ai.arrayschema.items.md) | | [Schema](./generative-ai.schema.md) | A schema describing the entries in the array. | -| [maxItems?](./generative-ai.arrayschema.maxitems.md) | | number | _(Optional)_ The maximum number of items in the array. | -| [minItems?](./generative-ai.arrayschema.minitems.md) | | number | _(Optional)_ The minimum number of items in the array. | -| [type](./generative-ai.arrayschema.type.md) | | typeof [SchemaType.ARRAY](./generative-ai.schematype.md) | | +| Property | Type | Description | +|----------|------|-------------| +| items | Schema | The schema for array items | +| type | SchemaType.ARRAY | Must be SchemaType.ARRAY | + diff --git a/package-lock.json b/package-lock.json index a11f272ff..a1f826652 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@google/generative-ai", - "version": "0.21.0", + "version": "0.24.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@google/generative-ai", - "version": "0.21.0", + "version": "0.24.0", "license": "Apache-2.0", "devDependencies": { "@changesets/cli": "^2.27.1", diff --git a/src/models/generative-model.test.ts b/src/models/generative-model.test.ts index e6c6fd85d..48b8aed9c 100644 --- a/src/models/generative-model.test.ts +++ b/src/models/generative-model.test.ts @@ -462,4 +462,25 @@ describe("GenerativeModel", () => { expect(makeRequestStub).to.not.be.called; restore(); }); + it("throws error when using unsupported array schema properties", async () => { + const genModel = new GenerativeModel("apiKey", { + model: "my-model", + generationConfig: { + responseMimeType: "application/json", + responseSchema: { + type: SchemaType.ARRAY, + items: { + type: SchemaType.STRING, + }, + minItems: 1, // This should trigger error + maxItems: 3, // This should trigger error + }, + }, + }); + + await expect(genModel.generateContent("test")).to.be.rejectedWith( + GoogleGenerativeAIError, + "Properties 'minItems' and 'maxItems' are not supported in array schema" + ); + }); }); diff --git a/src/models/generative-model.ts b/src/models/generative-model.ts index 7cd3fe622..8e2e1902d 100644 --- a/src/models/generative-model.ts +++ b/src/models/generative-model.ts @@ -86,6 +86,14 @@ export class GenerativeModel { this.cachedContent = modelParams.cachedContent; } + private validateArraySchema(schema: ArraySchema) { + if ('minItems' in schema || 'maxItems' in schema) { + throw new GoogleGenerativeAIError( + "Properties 'minItems' and 'maxItems' are not supported in array schema" + ); + } + } + /** * Makes a single non-streaming call to the model * and returns an object containing a single {@link GenerateContentResponse}. @@ -95,14 +103,16 @@ export class GenerativeModel { * {@link GoogleGenerativeAI.getGenerativeModel }. */ async generateContent( - request: GenerateContentRequest | string | Array, - requestOptions: SingleRequestOptions = {}, + promptOrParams: string | GenerateContentRequest, + generativeModelRequestOptions?: RequestOptions, ): Promise { - const formattedParams = formatGenerateContentInput(request); - const generativeModelRequestOptions: SingleRequestOptions = { - ...this._requestOptions, - ...requestOptions, - }; + const formattedParams = await this.formatGenerateContentInput(promptOrParams); + + // Add schema validation + if (formattedParams.generationConfig?.responseSchema?.type === SchemaType.ARRAY) { + this.validateArraySchema(formattedParams.generationConfig.responseSchema); + } + return generateContent( this.apiKey, this.model, diff --git a/types/requests.ts b/types/requests.ts index 81285bc20..5927ae1a1 100644 --- a/types/requests.ts +++ b/types/requests.ts @@ -245,3 +245,8 @@ export interface CodeExecutionTool { */ codeExecution: {}; } + +export interface ArraySchema extends BaseSchema { + items: Schema; + type: typeof SchemaType.ARRAY; +}