Skip to content

feat: remove unsupported array schema properties and add validation #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-jeans-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@google/generative-ai": patch
---

Remove unsupported minItems and maxItems properties from ArraySchema and add validation to prevent their usage
26 changes: 8 additions & 18 deletions docs/reference/main/generative-ai.arrayschema.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
# ArraySchema Interface

[Home](./index.md) &gt; [@google/generative-ai](./generative-ai.md) &gt; [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 |


4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/models/generative-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
});
});
24 changes: 17 additions & 7 deletions src/models/generative-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -95,14 +103,16 @@ export class GenerativeModel {
* {@link GoogleGenerativeAI.getGenerativeModel }.
*/
async generateContent(
request: GenerateContentRequest | string | Array<string | Part>,
requestOptions: SingleRequestOptions = {},
promptOrParams: string | GenerateContentRequest,
generativeModelRequestOptions?: RequestOptions,
): Promise<GenerateContentResult> {
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,
Expand Down
5 changes: 5 additions & 0 deletions types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ export interface CodeExecutionTool {
*/
codeExecution: {};
}

export interface ArraySchema extends BaseSchema {
items: Schema;
type: typeof SchemaType.ARRAY;
}