Skip to content

feat!(ai): add support for anyOf schemas #9042

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 2 commits 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
6 changes: 6 additions & 0 deletions .changeset/healthy-moons-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'firebase': major
'@firebase/ai': major
---

Add support for `anyOf` schemas
24 changes: 20 additions & 4 deletions common/api-review/ai.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ export interface AIOptions {
backend: Backend;
}

// @public
export class AnyOfSchema extends Schema {
constructor(schemaParams: SchemaParams & {
anyOf: TypedSchema[];
});
// (undocumented)
anyOf: TypedSchema[];
// @internal (undocumented)
toJSON(): SchemaRequest;
}

// @public
export class ArraySchema extends Schema {
constructor(schemaParams: SchemaParams, items: TypedSchema);
Expand Down Expand Up @@ -776,6 +787,10 @@ export abstract class Schema implements SchemaInterface {
constructor(schemaParams: SchemaInterface);
[key: string]: unknown;
// (undocumented)
static anyOf(anyOfParams: SchemaParams & {
anyOf: TypedSchema[];
}): AnyOfSchema;
// (undocumented)
static array(arrayParams: SchemaParams & {
items: Schema;
}): ArraySchema;
Expand Down Expand Up @@ -804,12 +819,12 @@ export abstract class Schema implements SchemaInterface {
static string(stringParams?: SchemaParams): StringSchema;
// @internal
toJSON(): SchemaRequest;
type: SchemaType;
type?: SchemaType;
}

// @public
export interface SchemaInterface extends SchemaShared<SchemaInterface> {
type: SchemaType;
type?: SchemaType;
}

// @public
Expand All @@ -819,13 +834,14 @@ export interface SchemaParams extends SchemaShared<SchemaInterface> {
// @public
export interface SchemaRequest extends SchemaShared<SchemaRequest> {
required?: string[];
type: SchemaType;
type?: SchemaType;
}

// @public
export interface SchemaShared<T> {
// (undocumented)
[key: string]: unknown;
anyOf?: T[];
description?: string;
enum?: string[];
example?: unknown;
Expand Down Expand Up @@ -900,7 +916,7 @@ export interface ToolConfig {
}

// @public
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema | AnyOfSchema;

// @public
export interface UsageMetadata {
Expand Down
2 changes: 2 additions & 0 deletions docs-devsite/_toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ toc:
path: /docs/reference/js/ai.aimodel.md
- title: AIOptions
path: /docs/reference/js/ai.aioptions.md
- title: AnyOfSchema
path: /docs/reference/js/ai.anyofschema.md
- title: ArraySchema
path: /docs/reference/js/ai.arrayschema.md
- title: Backend
Expand Down
58 changes: 58 additions & 0 deletions docs-devsite/ai.anyofschema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Project: /docs/reference/js/_project.yaml
Book: /docs/reference/_book.yaml
page_type: reference

{% comment %}
DO NOT EDIT THIS FILE!
This is generated by the JS SDK team, and any local changes will be
overwritten. Changes should be made in the source code at
https://github.com/firebase/firebase-js-sdk
{% endcomment %}

# AnyOfSchema class
Schema class representing a value that can conform to any of the provided sub-schemas. This is useful when a field can accept multiple distinct types or structures.

<b>Signature:</b>

```typescript
export declare class AnyOfSchema extends Schema
```
<b>Extends:</b> [Schema](./ai.schema.md#schema_class)

## Constructors

| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(schemaParams)](./ai.anyofschema.md#anyofschemaconstructor) | | Constructs a new instance of the <code>AnyOfSchema</code> class |

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [anyOf](./ai.anyofschema.md#anyofschemaanyof) | | [TypedSchema](./ai.md#typedschema)<!-- -->\[\] | |

## AnyOfSchema.(constructor)

Constructs a new instance of the `AnyOfSchema` class

<b>Signature:</b>

```typescript
constructor(schemaParams: SchemaParams & {
anyOf: TypedSchema[];
});
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| schemaParams | [SchemaParams](./ai.schemaparams.md#schemaparams_interface) &amp; { anyOf: [TypedSchema](./ai.md#typedschema)<!-- -->\[\]; } | |

## AnyOfSchema.anyOf

<b>Signature:</b>

```typescript
anyOf: TypedSchema[];
```
4 changes: 2 additions & 2 deletions docs-devsite/ai.generationconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface GenerationConfig
| [presencePenalty](./ai.generationconfig.md#generationconfigpresencepenalty) | number | |
| [responseMimeType](./ai.generationconfig.md#generationconfigresponsemimetype) | string | Output response MIME type of the generated candidate text. Supported MIME types are <code>text/plain</code> (default, text output), <code>application/json</code> (JSON response in the candidates), and <code>text/x.enum</code>. |
| [responseModalities](./ai.generationconfig.md#generationconfigresponsemodalities) | [ResponseModality](./ai.md#responsemodality)<!-- -->\[\] | <b><i>(Public Preview)</i></b> Generation modalities to be returned in generation responses. |
| [responseSchema](./ai.generationconfig.md#generationconfigresponseschema) | [TypedSchema](./ai.md#typedschema) \| [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) | Output response schema of the generated candidate text. This value can be a class generated with a [Schema](./ai.schema.md#schema_class) static method like <code>Schema.string()</code> or <code>Schema.object()</code> or it can be a plain JS object matching the [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) interface. <br/>Note: This only applies when the specified <code>responseMIMEType</code> supports a schema; currently this is limited to <code>application/json</code> and <code>text/x.enum</code>. |
| [responseSchema](./ai.generationconfig.md#generationconfigresponseschema) | [TypedSchema](./ai.md#typedschema) \| [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) | Output response schema of the generated candidate text. This value can be a class generated with a [Schema](./ai.schema.md#schema_class) static method like <code>Schema.string()</code> or <code>Schema.object()</code> or it can be a plain JS object matching the [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) interface. <br/>Note: This only applies when the specified <code>responseMimeType</code> supports a schema; currently this is limited to <code>application/json</code> and <code>text/x.enum</code>. |
| [stopSequences](./ai.generationconfig.md#generationconfigstopsequences) | string\[\] | |
| [temperature](./ai.generationconfig.md#generationconfigtemperature) | number | |
| [topK](./ai.generationconfig.md#generationconfigtopk) | number | |
Expand Down Expand Up @@ -93,7 +93,7 @@ responseModalities?: ResponseModality[];

## GenerationConfig.responseSchema

Output response schema of the generated candidate text. This value can be a class generated with a [Schema](./ai.schema.md#schema_class) static method like `Schema.string()` or `Schema.object()` or it can be a plain JS object matching the [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) interface. <br/>Note: This only applies when the specified `responseMIMEType` supports a schema; currently this is limited to `application/json` and `text/x.enum`<!-- -->.
Output response schema of the generated candidate text. This value can be a class generated with a [Schema](./ai.schema.md#schema_class) static method like `Schema.string()` or `Schema.object()` or it can be a plain JS object matching the [SchemaRequest](./ai.schemarequest.md#schemarequest_interface) interface. <br/>Note: This only applies when the specified `responseMimeType` supports a schema; currently this is limited to `application/json` and `text/x.enum`<!-- -->.

<b>Signature:</b>

Expand Down
3 changes: 2 additions & 1 deletion docs-devsite/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The Firebase AI Web SDK.
| --- | --- |
| [AIError](./ai.aierror.md#aierror_class) | Error class for the Firebase AI SDK. |
| [AIModel](./ai.aimodel.md#aimodel_class) | Base class for Firebase AI model APIs.<!-- -->Instances of this class are associated with a specific Firebase AI [Backend](./ai.backend.md#backend_class) and provide methods for interacting with the configured generative model. |
| [AnyOfSchema](./ai.anyofschema.md#anyofschema_class) | Schema class representing a value that can conform to any of the provided sub-schemas. This is useful when a field can accept multiple distinct types or structures. |
| [ArraySchema](./ai.arrayschema.md#arrayschema_class) | Schema class for "array" types. The <code>items</code> param should refer to the type of item that can be a member of the array. |
| [Backend](./ai.backend.md#backend_class) | Abstract base class representing the configuration for an AI service backend. This class should not be instantiated directly. Use its subclasses; [GoogleAIBackend](./ai.googleaibackend.md#googleaibackend_class) for the Gemini Developer API (via [Google AI](https://ai.google/)<!-- -->), and [VertexAIBackend](./ai.vertexaibackend.md#vertexaibackend_class) for the Vertex AI Gemini API. |
| [BooleanSchema](./ai.booleanschema.md#booleanschema_class) | Schema class for "boolean" types. |
Expand Down Expand Up @@ -410,7 +411,7 @@ A type that includes all specific Schema types.
<b>Signature:</b>

```typescript
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema | AnyOfSchema;
```

## VertexAI
Expand Down
27 changes: 24 additions & 3 deletions docs-devsite/ai.schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ export declare abstract class Schema implements SchemaInterface
| [example](./ai.schema.md#schemaexample) | | unknown | Optional. The example of the property. |
| [format](./ai.schema.md#schemaformat) | | string | Optional. The format of the property. Supported formats:<br/> <ul> <li>for NUMBER type: "float", "double"</li> <li>for INTEGER type: "int32", "int64"</li> <li>for STRING type: "email", "byte", etc</li> </ul> |
| [nullable](./ai.schema.md#schemanullable) | | boolean | Optional. Whether the property is nullable. Defaults to false. |
| [type](./ai.schema.md#schematype) | | [SchemaType](./ai.md#schematype) | Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
| [type](./ai.schema.md#schematype) | | [SchemaType](./ai.md#schematype) | Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. This can only be undefined when using <code>anyOf</code> schemas, which do not have an explicit type in the . |

## Methods

| Method | Modifiers | Description |
| --- | --- | --- |
| [anyOf(anyOfParams)](./ai.schema.md#schemaanyof) | <code>static</code> | |
| [array(arrayParams)](./ai.schema.md#schemaarray) | <code>static</code> | |
| [boolean(booleanParams)](./ai.schema.md#schemaboolean) | <code>static</code> | |
| [enumString(stringParams)](./ai.schema.md#schemaenumstring) | <code>static</code> | |
Expand Down Expand Up @@ -105,14 +106,34 @@ nullable: boolean;

## Schema.type

Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. This can only be undefined when using `anyOf` schemas, which do not have an explicit type in the .

<b>Signature:</b>

```typescript
type: SchemaType;
type?: SchemaType;
```

## Schema.anyOf()

<b>Signature:</b>

```typescript
static anyOf(anyOfParams: SchemaParams & {
anyOf: TypedSchema[];
}): AnyOfSchema;
```

#### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| anyOfParams | [SchemaParams](./ai.schemaparams.md#schemaparams_interface) &amp; { anyOf: [TypedSchema](./ai.md#typedschema)<!-- -->\[\]; } | |

<b>Returns:</b>

[AnyOfSchema](./ai.anyofschema.md#anyofschema_class)

## Schema.array()

<b>Signature:</b>
Expand Down
6 changes: 3 additions & 3 deletions docs-devsite/ai.schemainterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export interface SchemaInterface extends SchemaShared<SchemaInterface>

| Property | Type | Description |
| --- | --- | --- |
| [type](./ai.schemainterface.md#schemainterfacetype) | [SchemaType](./ai.md#schematype) | The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
| [type](./ai.schemainterface.md#schemainterfacetype) | [SchemaType](./ai.md#schematype) | The type of the property. this can only be undefined when using <code>anyof</code> schemas, which do not have an explicit type in the . |

## SchemaInterface.type

The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
The type of the property. this can only be undefined when using `anyof` schemas, which do not have an explicit type in the .

<b>Signature:</b>

```typescript
type: SchemaType;
type?: SchemaType;
```
6 changes: 3 additions & 3 deletions docs-devsite/ai.schemarequest.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface SchemaRequest extends SchemaShared<SchemaRequest>
| Property | Type | Description |
| --- | --- | --- |
| [required](./ai.schemarequest.md#schemarequestrequired) | string\[\] | Optional. Array of required property. |
| [type](./ai.schemarequest.md#schemarequesttype) | [SchemaType](./ai.md#schematype) | The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
| [type](./ai.schemarequest.md#schemarequesttype) | [SchemaType](./ai.md#schematype) | The type of the property. this can only be undefined when using <code>anyof</code> schemas, which do not have an explicit type in the . |

## SchemaRequest.required

Expand All @@ -38,10 +38,10 @@ required?: string[];

## SchemaRequest.type

The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
The type of the property. this can only be undefined when using `anyof` schemas, which do not have an explicit type in the .

<b>Signature:</b>

```typescript
type: SchemaType;
type?: SchemaType;
```
11 changes: 11 additions & 0 deletions docs-devsite/ai.schemashared.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface SchemaShared<T>

| Property | Type | Description |
| --- | --- | --- |
| [anyOf](./ai.schemashared.md#schemasharedanyof) | T\[\] | An array of [Schema](./ai.schema.md#schema_class)<!-- -->. The generated data must be valid against any of the schemas listed in this array. This allows specifying multiple possible structures or types for a single field. |
| [description](./ai.schemashared.md#schemashareddescription) | string | Optional. The description of the property. |
| [enum](./ai.schemashared.md#schemasharedenum) | string\[\] | Optional. The enum of the property. |
| [example](./ai.schemashared.md#schemasharedexample) | unknown | Optional. The example of the property. |
Expand All @@ -30,6 +31,16 @@ export interface SchemaShared<T>
| [nullable](./ai.schemashared.md#schemasharednullable) | boolean | Optional. Whether the property is nullable. |
| [properties](./ai.schemashared.md#schemasharedproperties) | { \[k: string\]: T; } | Optional. Map of <code>Schema</code> objects. |

## SchemaShared.anyOf

An array of [Schema](./ai.schema.md#schema_class)<!-- -->. The generated data must be valid against any of the schemas listed in this array. This allows specifying multiple possible structures or types for a single field.

<b>Signature:</b>

```typescript
anyOf?: T[];
```

## SchemaShared.description

Optional. The description of the property.
Expand Down
Loading
Loading