Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions FirebaseVertexAI/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased
- [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
`GenerativeModel` or `ImagenModel`. (#14610)
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 @@ -99,6 +99,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 @@ -124,12 +125,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