From a40131c7480b1f2d36c4618d334dff385bbd7c7a Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 7 Apr 2025 18:51:51 -0400 Subject: [PATCH 1/2] [Vertex AI] Add `minItems` and `maxItems` to `Schema` --- FirebaseVertexAI/CHANGELOG.md | 4 +++ .../Sources/Types/Public/Schema.swift | 32 ++++++++++++++++--- .../Tests/Unit/GenerationConfigTests.swift | 14 +++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index d70368aa708..8ed0e34b2f9 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -1,3 +1,7 @@ +# Unreleased +- [added] Added support for specifiying the minimum and maximum number of items + (`minItems` / `maxItems`) to generate in an array `Schema`. + # 11.11.0 - [added] Emits a warning when attempting to use an incompatible model with `GenerativeModel` or `ImagenModel`. (#14610) diff --git a/FirebaseVertexAI/Sources/Types/Public/Schema.swift b/FirebaseVertexAI/Sources/Types/Public/Schema.swift index 912a1d41c0b..1fb2886ed14 100644 --- a/FirebaseVertexAI/Sources/Types/Public/Schema.swift +++ b/FirebaseVertexAI/Sources/Types/Public/Schema.swift @@ -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]? @@ -88,6 +94,7 @@ 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 @@ -95,6 +102,8 @@ public final class Schema: Sendable { self.nullable = nullable self.enumValues = enumValues self.items = items + self.minItems = minItems + self.maxItems = maxItems self.properties = properties self.requiredProperties = requiredProperties } @@ -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. @@ -327,6 +347,8 @@ extension Schema: Encodable { case nullable case enumValues = "enum" case items + case minItems + case maxItems case properties case requiredProperties = "required" } diff --git a/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift b/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift index 23f85e8bdbd..ddb49b43186 100644 --- a/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift +++ b/FirebaseVertexAI/Tests/Unit/GenerationConfigTests.swift @@ -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(), ]) @@ -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" } From 643d75ef7cf23908d5a76417fe1c718254d93595 Mon Sep 17 00:00:00 2001 From: Andrew Heard Date: Mon, 7 Apr 2025 18:55:51 -0400 Subject: [PATCH 2/2] Add PR# to CHANGELOG --- FirebaseVertexAI/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/FirebaseVertexAI/CHANGELOG.md b/FirebaseVertexAI/CHANGELOG.md index 8ed0e34b2f9..0c3236917ec 100644 --- a/FirebaseVertexAI/CHANGELOG.md +++ b/FirebaseVertexAI/CHANGELOG.md @@ -1,6 +1,6 @@ # Unreleased -- [added] Added support for specifiying the minimum and maximum number of items - (`minItems` / `maxItems`) to generate in an array `Schema`. +- [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