Skip to content

Commit 5200f7b

Browse files
authored
feat!(ai): add support for anyOf schemas (#9042)
1 parent cb19688 commit 5200f7b

File tree

14 files changed

+394
-71
lines changed

14 files changed

+394
-71
lines changed

.changeset/healthy-moons-type.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': major
3+
'@firebase/ai': major
4+
---
5+
6+
Add support for `anyOf` schemas

common/api-review/ai.api.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ export interface AIOptions {
6464
backend: Backend;
6565
}
6666

67+
// @public
68+
export class AnyOfSchema extends Schema {
69+
constructor(schemaParams: SchemaParams & {
70+
anyOf: TypedSchema[];
71+
});
72+
// (undocumented)
73+
anyOf: TypedSchema[];
74+
// @internal (undocumented)
75+
toJSON(): SchemaRequest;
76+
}
77+
6778
// @public
6879
export class ArraySchema extends Schema {
6980
constructor(schemaParams: SchemaParams, items: TypedSchema);
@@ -814,6 +825,10 @@ export abstract class Schema implements SchemaInterface {
814825
constructor(schemaParams: SchemaInterface);
815826
[key: string]: unknown;
816827
// (undocumented)
828+
static anyOf(anyOfParams: SchemaParams & {
829+
anyOf: TypedSchema[];
830+
}): AnyOfSchema;
831+
// (undocumented)
817832
static array(arrayParams: SchemaParams & {
818833
items: Schema;
819834
}): ArraySchema;
@@ -845,12 +860,12 @@ export abstract class Schema implements SchemaInterface {
845860
static string(stringParams?: SchemaParams): StringSchema;
846861
// @internal
847862
toJSON(): SchemaRequest;
848-
type: SchemaType;
863+
type?: SchemaType;
849864
}
850865

851866
// @public
852867
export interface SchemaInterface extends SchemaShared<SchemaInterface> {
853-
type: SchemaType;
868+
type?: SchemaType;
854869
}
855870

856871
// @public
@@ -860,13 +875,14 @@ export interface SchemaParams extends SchemaShared<SchemaInterface> {
860875
// @public
861876
export interface SchemaRequest extends SchemaShared<SchemaRequest> {
862877
required?: string[];
863-
type: SchemaType;
878+
type?: SchemaType;
864879
}
865880

866881
// @public
867882
export interface SchemaShared<T> {
868883
// (undocumented)
869884
[key: string]: unknown;
885+
anyOf?: T[];
870886
description?: string;
871887
enum?: string[];
872888
example?: unknown;
@@ -958,7 +974,7 @@ export interface ToolConfig {
958974
}
959975

960976
// @public
961-
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;
977+
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema | AnyOfSchema;
962978

963979
// @public
964980
export interface UsageMetadata {

docs-devsite/_toc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ toc:
1212
path: /docs/reference/js/ai.aimodel.md
1313
- title: AIOptions
1414
path: /docs/reference/js/ai.aioptions.md
15+
- title: AnyOfSchema
16+
path: /docs/reference/js/ai.anyofschema.md
1517
- title: ArraySchema
1618
path: /docs/reference/js/ai.arrayschema.md
1719
- title: Backend

docs-devsite/ai.anyofschema.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Project: /docs/reference/js/_project.yaml
2+
Book: /docs/reference/_book.yaml
3+
page_type: reference
4+
5+
{% comment %}
6+
DO NOT EDIT THIS FILE!
7+
This is generated by the JS SDK team, and any local changes will be
8+
overwritten. Changes should be made in the source code at
9+
https://github.com/firebase/firebase-js-sdk
10+
{% endcomment %}
11+
12+
# AnyOfSchema class
13+
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.
14+
15+
<b>Signature:</b>
16+
17+
```typescript
18+
export declare class AnyOfSchema extends Schema
19+
```
20+
<b>Extends:</b> [Schema](./ai.schema.md#schema_class)
21+
22+
## Constructors
23+
24+
| Constructor | Modifiers | Description |
25+
| --- | --- | --- |
26+
| [(constructor)(schemaParams)](./ai.anyofschema.md#anyofschemaconstructor) | | Constructs a new instance of the <code>AnyOfSchema</code> class |
27+
28+
## Properties
29+
30+
| Property | Modifiers | Type | Description |
31+
| --- | --- | --- | --- |
32+
| [anyOf](./ai.anyofschema.md#anyofschemaanyof) | | [TypedSchema](./ai.md#typedschema)<!-- -->\[\] | |
33+
34+
## AnyOfSchema.(constructor)
35+
36+
Constructs a new instance of the `AnyOfSchema` class
37+
38+
<b>Signature:</b>
39+
40+
```typescript
41+
constructor(schemaParams: SchemaParams & {
42+
anyOf: TypedSchema[];
43+
});
44+
```
45+
46+
#### Parameters
47+
48+
| Parameter | Type | Description |
49+
| --- | --- | --- |
50+
| schemaParams | [SchemaParams](./ai.schemaparams.md#schemaparams_interface) &amp; { anyOf: [TypedSchema](./ai.md#typedschema)<!-- -->\[\]; } | |
51+
52+
## AnyOfSchema.anyOf
53+
54+
<b>Signature:</b>
55+
56+
```typescript
57+
anyOf: TypedSchema[];
58+
```

docs-devsite/ai.generationconfig.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface GenerationConfig
2828
| [presencePenalty](./ai.generationconfig.md#generationconfigpresencepenalty) | number | |
2929
| [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>. |
3030
| [responseModalities](./ai.generationconfig.md#generationconfigresponsemodalities) | [ResponseModality](./ai.md#responsemodality)<!-- -->\[\] | <b><i>(Public Preview)</i></b> Generation modalities to be returned in generation responses. |
31-
| [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>. |
31+
| [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>. |
3232
| [stopSequences](./ai.generationconfig.md#generationconfigstopsequences) | string\[\] | |
3333
| [temperature](./ai.generationconfig.md#generationconfigtemperature) | number | |
3434
| [thinkingConfig](./ai.generationconfig.md#generationconfigthinkingconfig) | [ThinkingConfig](./ai.thinkingconfig.md#thinkingconfig_interface) | Configuration for "thinking" behavior of compatible Gemini models. |
@@ -94,7 +94,7 @@ responseModalities?: ResponseModality[];
9494

9595
## GenerationConfig.responseSchema
9696

97-
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`<!-- -->.
97+
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`<!-- -->.
9898

9999
<b>Signature:</b>
100100

docs-devsite/ai.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The Firebase AI Web SDK.
2828
| --- | --- |
2929
| [AIError](./ai.aierror.md#aierror_class) | Error class for the Firebase AI SDK. |
3030
| [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. |
31+
| [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. |
3132
| [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. |
3233
| [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. |
3334
| [BooleanSchema](./ai.booleanschema.md#booleanschema_class) | Schema class for "boolean" types. |
@@ -762,5 +763,5 @@ A type that includes all specific Schema types.
762763
<b>Signature:</b>
763764

764765
```typescript
765-
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema;
766+
export type TypedSchema = IntegerSchema | NumberSchema | StringSchema | BooleanSchema | ObjectSchema | ArraySchema | AnyOfSchema;
766767
```

docs-devsite/ai.schema.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ export declare abstract class Schema implements SchemaInterface
3636
| [maxItems](./ai.schema.md#schemamaxitems) | | number | The maximum number of items (elements) in a schema of [SchemaType](./ai.md#schematype) <code>array</code>. |
3737
| [minItems](./ai.schema.md#schemaminitems) | | number | The minimum number of items (elements) in a schema of [SchemaType](./ai.md#schematype) <code>array</code>. |
3838
| [nullable](./ai.schema.md#schemanullable) | | boolean | Optional. Whether the property is nullable. Defaults to false. |
39-
| [type](./ai.schema.md#schematype) | | [SchemaType](./ai.md#schematype) | Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
39+
| [type](./ai.schema.md#schematype) | | [SchemaType](./ai.md#schematype) | Optional. 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 [OpenAPI specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->. |
4040
4141
## Methods
4242
4343
| Method | Modifiers | Description |
4444
| --- | --- | --- |
45+
| [anyOf(anyOfParams)](./ai.schema.md#schemaanyof) | <code>static</code> | |
4546
| [array(arrayParams)](./ai.schema.md#schemaarray) | <code>static</code> | |
4647
| [boolean(booleanParams)](./ai.schema.md#schemaboolean) | <code>static</code> | |
4748
| [enumString(stringParams)](./ai.schema.md#schemaenumstring) | <code>static</code> | |
@@ -138,14 +139,34 @@ nullable: boolean;
138139
139140
## Schema.type
140141
141-
Optional. The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
142+
Optional. The type of the property. This can only be undefined when using `anyOf` schemas, which do not have an explicit type in the [OpenAPI specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->.
142143
143144
<b>Signature:</b>
144145
145146
```typescript
146-
type: SchemaType;
147+
type?: SchemaType;
147148
```
148149
150+
## Schema.anyOf()
151+
152+
<b>Signature:</b>
153+
154+
```typescript
155+
static anyOf(anyOfParams: SchemaParams & {
156+
anyOf: TypedSchema[];
157+
}): AnyOfSchema;
158+
```
159+
160+
#### Parameters
161+
162+
| Parameter | Type | Description |
163+
| --- | --- | --- |
164+
| anyOfParams | [SchemaParams](./ai.schemaparams.md#schemaparams_interface) &amp; { anyOf: [TypedSchema](./ai.md#typedschema)<!-- -->\[\]; } | |
165+
166+
<b>Returns:</b>
167+
168+
[AnyOfSchema](./ai.anyofschema.md#anyofschema_class)
169+
149170
## Schema.array()
150171
151172
<b>Signature:</b>

docs-devsite/ai.schemainterface.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ export interface SchemaInterface extends SchemaShared<SchemaInterface>
2323
2424
| Property | Type | Description |
2525
| --- | --- | --- |
26-
| [type](./ai.schemainterface.md#schemainterfacetype) | [SchemaType](./ai.md#schematype) | The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
26+
| [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 [OpenAPI Specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->. |
2727
2828
## SchemaInterface.type
2929
30-
The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
30+
The type of the property. this can only be undefined when using `anyof` schemas, which do not have an explicit type in the [OpenAPI Specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->.
3131
3232
<b>Signature:</b>
3333
3434
```typescript
35-
type: SchemaType;
35+
type?: SchemaType;
3636
```

docs-devsite/ai.schemarequest.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface SchemaRequest extends SchemaShared<SchemaRequest>
2424
| Property | Type | Description |
2525
| --- | --- | --- |
2626
| [required](./ai.schemarequest.md#schemarequestrequired) | string\[\] | Optional. Array of required property. |
27-
| [type](./ai.schemarequest.md#schemarequesttype) | [SchemaType](./ai.md#schematype) | The type of the property. [SchemaType](./ai.md#schematype)<!-- -->. |
27+
| [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 [OpenAPI specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->. |
2828
2929
## SchemaRequest.required
3030
@@ -38,10 +38,10 @@ required?: string[];
3838
3939
## SchemaRequest.type
4040
41-
The type of the property. [SchemaType](./ai.md#schematype)<!-- -->.
41+
The type of the property. this can only be undefined when using `anyOf` schemas, which do not have an explicit type in the [OpenAPI specification](https://swagger.io/docs/specification/v3_0/data-models/data-types/#any-type)<!-- -->.
4242
4343
<b>Signature:</b>
4444
4545
```typescript
46-
type: SchemaType;
46+
type?: SchemaType;
4747
```

docs-devsite/ai.schemashared.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface SchemaShared<T>
2222

2323
| Property | Type | Description |
2424
| --- | --- | --- |
25+
| [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. |
2526
| [description](./ai.schemashared.md#schemashareddescription) | string | Optional. The description of the property. |
2627
| [enum](./ai.schemashared.md#schemasharedenum) | string\[\] | Optional. The enum of the property. |
2728
| [example](./ai.schemashared.md#schemasharedexample) | unknown | Optional. The example of the property. |
@@ -36,6 +37,16 @@ export interface SchemaShared<T>
3637
| [propertyOrdering](./ai.schemashared.md#schemasharedpropertyordering) | string\[\] | A hint suggesting the order in which the keys should appear in the generated JSON string. |
3738
| [title](./ai.schemashared.md#schemasharedtitle) | string | The title of the property. This helps document the schema's purpose but does not typically constrain the generated value. It can subtly guide the model by clarifying the intent of a field. |
3839

40+
## SchemaShared.anyOf
41+
42+
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.
43+
44+
<b>Signature:</b>
45+
46+
```typescript
47+
anyOf?: T[];
48+
```
49+
3950
## SchemaShared.description
4051

4152
Optional. The description of the property.

0 commit comments

Comments
 (0)