Skip to content

[Vertex AI] Add minItems and maxItems to Schema #14671

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

Merged
merged 4 commits into from
Apr 10, 2025
Merged
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
2 changes: 2 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Note: This feature is in Public Preview and relies on experimental models,
which means that it is not subject to any SLA or deprecation policy and could
change in backwards-incompatible ways.
- [added] Added support for specifying the minimum and maximum number of items
(`minItems` / `maxItems`) to generate in an array `Schema`. (#14671)

# 11.11.0
- [added] Emits a warning when attempting to use an incompatible model with
Expand Down
32 changes: 27 additions & 5 deletions FirebaseVertexAI/Sources/Types/Public/Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public final class Schema: Sendable {
/// Schema of the elements of type `"ARRAY"`.
public let items: Schema?

/// The minimum number of items (elements) in a schema of type `"ARRAY"`.
public let minItems: Int?

/// The maximum number of items (elements) in a schema of type `"ARRAY"`.
public let maxItems: Int?

/// Properties of type `"OBJECT"`.
public let properties: [String: Schema]?

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

required init(type: DataType, format: String? = nil, description: String? = nil,
nullable: Bool = false, enumValues: [String]? = nil, items: Schema? = nil,
minItems: Int? = nil, maxItems: Int? = nil,
properties: [String: Schema]? = nil, requiredProperties: [String]? = nil) {
dataType = type
self.format = format
self.description = description
self.nullable = nullable
self.enumValues = enumValues
self.items = items
self.minItems = minItems
self.maxItems = maxItems
self.properties = properties
self.requiredProperties = requiredProperties
}
Expand Down Expand Up @@ -256,12 +265,23 @@ public final class Schema: Sendable {
/// - Parameters:
/// - items: The `Schema` of the elements that the array will hold.
/// - description: An optional description of what the array should contain or represent; may
/// use Markdown format.
/// use Markdown format.
/// - nullable: If `true`, instructs the model that it may return `null` instead of an array;
/// defaults to `false`, enforcing that an array is returned.
public static func array(items: Schema, description: String? = nil,
nullable: Bool = false) -> Schema {
return self.init(type: .array, description: description, nullable: nullable, items: items)
/// defaults to `false`, enforcing that an array is returned.
/// - minItems: Instructs the model to produce at least the specified minimum number of elements
/// in the array; defaults to `nil`, meaning any number.
/// - maxItems: Instructs the model to produce at most the specified maximum number of elements
/// in the array.
public static func array(items: Schema, description: String? = nil, nullable: Bool = false,
minItems: Int? = nil, maxItems: Int? = nil) -> Schema {
return self.init(
type: .array,
description: description,
nullable: nullable,
items: items,
minItems: minItems,
maxItems: maxItems
)
}

/// Returns a `Schema` representing an object.
Expand Down Expand Up @@ -327,6 +347,8 @@ extension Schema: Encodable {
case nullable
case enumValues = "enum"
case items
case minItems
case maxItems
case properties
case requiredProperties = "required"
}
Expand Down
14 changes: 13 additions & 1 deletion FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ final class GenerationConfigTests: XCTestCase {
responseMIMEType: mimeType,
responseSchema: .object(properties: [
"firstName": .string(),
"middleNames": .array(items: .string(), minItems: 0, maxItems: 3),
"lastName": .string(),
"age": .integer(),
])
Expand All @@ -129,12 +130,23 @@ final class GenerationConfigTests: XCTestCase {
"lastName" : {
"nullable" : false,
"type" : "STRING"
},
"middleNames" : {
"items" : {
"nullable" : false,
"type" : "STRING"
},
"maxItems" : 3,
"minItems" : 0,
"nullable" : false,
"type" : "ARRAY"
}
},
"required" : [
"age",
"firstName",
"lastName"
"lastName",
"middleNames"
],
"type" : "OBJECT"
}
Expand Down
Loading