Skip to content

Commit a40131c

Browse files
committed
[Vertex AI] Add minItems and maxItems to Schema
1 parent 5003be4 commit a40131c

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

FirebaseVertexAI/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased
2+
- [added] Added support for specifiying the minimum and maximum number of items
3+
(`minItems` / `maxItems`) to generate in an array `Schema`.
4+
15
# 11.11.0
26
- [added] Emits a warning when attempting to use an incompatible model with
37
`GenerativeModel` or `ImagenModel`. (#14610)

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
@@ -99,6 +99,7 @@ final class GenerationConfigTests: XCTestCase {
9999
responseMIMEType: mimeType,
100100
responseSchema: .object(properties: [
101101
"firstName": .string(),
102+
"middleNames": .array(items: .string(), minItems: 0, maxItems: 3),
102103
"lastName": .string(),
103104
"age": .integer(),
104105
])
@@ -124,12 +125,23 @@ final class GenerationConfigTests: XCTestCase {
124125
"lastName" : {
125126
"nullable" : false,
126127
"type" : "STRING"
128+
},
129+
"middleNames" : {
130+
"items" : {
131+
"nullable" : false,
132+
"type" : "STRING"
133+
},
134+
"maxItems" : 3,
135+
"minItems" : 0,
136+
"nullable" : false,
137+
"type" : "ARRAY"
127138
}
128139
},
129140
"required" : [
130141
"age",
131142
"firstName",
132-
"lastName"
143+
"lastName",
144+
"middleNames"
133145
],
134146
"type" : "OBJECT"
135147
}

0 commit comments

Comments
 (0)