Skip to content

Commit 56301dd

Browse files
authored
[Vertex AI] Add minItems and maxItems to Schema (#14671)
1 parent a1c38ed commit 56301dd

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
Note: This feature is in Public Preview and relies on experimental models,
77
which means that it is not subject to any SLA or deprecation policy and could
88
change in backwards-incompatible ways.
9+
- [added] Added support for specifying the minimum and maximum number of items
10+
(`minItems` / `maxItems`) to generate in an array `Schema`. (#14671)
911

1012
# 11.11.0
1113
- [added] Emits a warning when attempting to use an incompatible model with

FirebaseVertexAI/Sources/Types/Public/Schema.swift

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public final class Schema: Sendable {
8080
/// Schema of the elements of type `"ARRAY"`.
8181
public let items: Schema?
8282

83+
/// The minimum number of items (elements) in a schema of type `"ARRAY"`.
84+
public let minItems: Int?
85+
86+
/// The maximum number of items (elements) in a schema of type `"ARRAY"`.
87+
public let maxItems: Int?
88+
8389
/// Properties of type `"OBJECT"`.
8490
public let properties: [String: Schema]?
8591

@@ -88,13 +94,16 @@ public final class Schema: Sendable {
8894

8995
required init(type: DataType, format: String? = nil, description: String? = nil,
9096
nullable: Bool = false, enumValues: [String]? = nil, items: Schema? = nil,
97+
minItems: Int? = nil, maxItems: Int? = nil,
9198
properties: [String: Schema]? = nil, requiredProperties: [String]? = nil) {
9299
dataType = type
93100
self.format = format
94101
self.description = description
95102
self.nullable = nullable
96103
self.enumValues = enumValues
97104
self.items = items
105+
self.minItems = minItems
106+
self.maxItems = maxItems
98107
self.properties = properties
99108
self.requiredProperties = requiredProperties
100109
}
@@ -256,12 +265,23 @@ public final class Schema: Sendable {
256265
/// - Parameters:
257266
/// - items: The `Schema` of the elements that the array will hold.
258267
/// - description: An optional description of what the array should contain or represent; may
259-
/// use Markdown format.
268+
/// use Markdown format.
260269
/// - nullable: If `true`, instructs the model that it may return `null` instead of an array;
261-
/// defaults to `false`, enforcing that an array is returned.
262-
public static func array(items: Schema, description: String? = nil,
263-
nullable: Bool = false) -> Schema {
264-
return self.init(type: .array, description: description, nullable: nullable, items: items)
270+
/// defaults to `false`, enforcing that an array is returned.
271+
/// - minItems: Instructs the model to produce at least the specified minimum number of elements
272+
/// in the array; defaults to `nil`, meaning any number.
273+
/// - maxItems: Instructs the model to produce at most the specified maximum number of elements
274+
/// in the array.
275+
public static func array(items: Schema, description: String? = nil, nullable: Bool = false,
276+
minItems: Int? = nil, maxItems: Int? = nil) -> Schema {
277+
return self.init(
278+
type: .array,
279+
description: description,
280+
nullable: nullable,
281+
items: items,
282+
minItems: minItems,
283+
maxItems: maxItems
284+
)
265285
}
266286

267287
/// Returns a `Schema` representing an object.
@@ -327,6 +347,8 @@ extension Schema: Encodable {
327347
case nullable
328348
case enumValues = "enum"
329349
case items
350+
case minItems
351+
case maxItems
330352
case properties
331353
case requiredProperties = "required"
332354
}

FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ final class GenerationConfigTests: XCTestCase {
104104
responseMIMEType: mimeType,
105105
responseSchema: .object(properties: [
106106
"firstName": .string(),
107+
"middleNames": .array(items: .string(), minItems: 0, maxItems: 3),
107108
"lastName": .string(),
108109
"age": .integer(),
109110
])
@@ -129,12 +130,23 @@ final class GenerationConfigTests: XCTestCase {
129130
"lastName" : {
130131
"nullable" : false,
131132
"type" : "STRING"
133+
},
134+
"middleNames" : {
135+
"items" : {
136+
"nullable" : false,
137+
"type" : "STRING"
138+
},
139+
"maxItems" : 3,
140+
"minItems" : 0,
141+
"nullable" : false,
142+
"type" : "ARRAY"
132143
}
133144
},
134145
"required" : [
135146
"age",
136147
"firstName",
137-
"lastName"
148+
"lastName",
149+
"middleNames"
138150
],
139151
"type" : "OBJECT"
140152
}

0 commit comments

Comments
 (0)