Skip to content

Commit 07ecf70

Browse files
Add title parameter to all static functions in Schema class (#14971)
1 parent 473733f commit 07ecf70

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

FirebaseAI/Sources/Types/Public/Schema.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -167,19 +167,21 @@ public final class Schema: Sendable {
167167
/// - Parameters:
168168
/// - description: An optional description of what the string should contain or represent; may
169169
/// use Markdown format.
170+
/// - title: An optional human-readable name/summary for the schema.
170171
/// - nullable: If `true`, instructs the model that it *may* generate `null` instead of a
171172
/// string; defaults to `false`, enforcing that a string value is generated.
172173
/// - format: An optional modifier describing the expected format of the string. Currently no
173174
/// formats are officially supported for strings but custom values may be specified using
174175
/// ``StringFormat/custom(_:)``, for example `.custom("email")` or `.custom("byte")`; these
175176
/// provide additional hints for how the model should respond but are not guaranteed to be
176177
/// adhered to.
177-
public static func string(description: String? = nil, nullable: Bool = false,
178-
format: StringFormat? = nil) -> Schema {
178+
public static func string(description: String? = nil, title: String? = nil,
179+
nullable: Bool = false, format: StringFormat? = nil) -> Schema {
179180
return self.init(
180181
type: .string,
181182
format: format?.rawValue,
182183
description: description,
184+
title: title,
183185
nullable: nullable
184186
)
185187
}
@@ -202,15 +204,17 @@ public final class Schema: Sendable {
202204
/// - values: The list of string values that may be generated by the model.
203205
/// - description: An optional description of what the `values` contain or represent; may use
204206
/// Markdown format.
207+
/// - title: An optional human-readable name/summary for the schema.
205208
/// - nullable: If `true`, instructs the model that it *may* generate `null` instead of one of
206209
/// the strings specified in `values`; defaults to `false`, enforcing that one of the string
207210
/// values is generated.
208211
public static func enumeration(values: [String], description: String? = nil,
209-
nullable: Bool = false) -> Schema {
212+
title: String? = nil, nullable: Bool = false) -> Schema {
210213
return self.init(
211214
type: .string,
212215
format: "enum",
213216
description: description,
217+
title: title,
214218
nullable: nullable,
215219
enumValues: values
216220
)
@@ -229,18 +233,20 @@ public final class Schema: Sendable {
229233
/// - Parameters:
230234
/// - description: An optional description of what the number should contain or represent; may
231235
/// use Markdown format.
236+
/// - title: An optional human-readable name/summary for the schema.
232237
/// - nullable: If `true`, instructs the model that it may generate `null` instead of a number;
233238
/// defaults to `false`, enforcing that a number is generated.
234239
/// - minimum: If specified, instructs the model that the value should be greater than or
235240
/// equal to the specified minimum.
236241
/// - maximum: If specified, instructs the model that the value should be less than or equal
237242
/// to the specified maximum.
238-
public static func float(description: String? = nil, nullable: Bool = false,
243+
public static func float(description: String? = nil, title: String? = nil, nullable: Bool = false,
239244
minimum: Float? = nil, maximum: Float? = nil) -> Schema {
240245
return self.init(
241246
type: .number,
242247
format: "float",
243248
description: description,
249+
title: title,
244250
nullable: nullable,
245251
minimum: minimum.map { Double($0) },
246252
maximum: maximum.map { Double($0) }
@@ -255,17 +261,20 @@ public final class Schema: Sendable {
255261
/// - Parameters:
256262
/// - description: An optional description of what the number should contain or represent; may
257263
/// use Markdown format.
264+
/// - title: An optional human-readable name/summary for the schema.
258265
/// - nullable: If `true`, instructs the model that it may return `null` instead of a number;
259266
/// defaults to `false`, enforcing that a number is returned.
260267
/// - minimum: If specified, instructs the model that the value should be greater than or
261268
/// equal to the specified minimum.
262269
/// - maximum: If specified, instructs the model that the value should be less than or equal
263270
/// to the specified maximum.
264-
public static func double(description: String? = nil, nullable: Bool = false,
271+
public static func double(description: String? = nil, title: String? = nil,
272+
nullable: Bool = false,
265273
minimum: Double? = nil, maximum: Double? = nil) -> Schema {
266274
return self.init(
267275
type: .number,
268276
description: description,
277+
title: title,
269278
nullable: nullable,
270279
minimum: minimum,
271280
maximum: maximum
@@ -287,6 +296,7 @@ public final class Schema: Sendable {
287296
/// - Parameters:
288297
/// - description: An optional description of what the integer should contain or represent; may
289298
/// use Markdown format.
299+
/// - title: An optional human-readable name/summary for the schema.
290300
/// - nullable: If `true`, instructs the model that it may return `null` instead of an integer;
291301
/// defaults to `false`, enforcing that an integer is returned.
292302
/// - format: An optional modifier describing the expected format of the integer. Currently the
@@ -296,13 +306,14 @@ public final class Schema: Sendable {
296306
/// equal to the specified minimum.
297307
/// - maximum: If specified, instructs the model that the value should be less than or equal
298308
/// to the specified maximum.
299-
public static func integer(description: String? = nil, nullable: Bool = false,
300-
format: IntegerFormat? = nil,
309+
public static func integer(description: String? = nil, title: String? = nil,
310+
nullable: Bool = false, format: IntegerFormat? = nil,
301311
minimum: Int? = nil, maximum: Int? = nil) -> Schema {
302312
return self.init(
303313
type: .integer,
304314
format: format?.rawValue,
305315
description: description,
316+
title: title,
306317
nullable: nullable.self,
307318
minimum: minimum.map { Double($0) },
308319
maximum: maximum.map { Double($0) }
@@ -317,10 +328,12 @@ public final class Schema: Sendable {
317328
/// - Parameters:
318329
/// - description: An optional description of what the boolean should contain or represent; may
319330
/// use Markdown format.
331+
/// - title: An optional human-readable name/summary for the schema.
320332
/// - nullable: If `true`, instructs the model that it may return `null` instead of a boolean;
321333
/// defaults to `false`, enforcing that a boolean is returned.
322-
public static func boolean(description: String? = nil, nullable: Bool = false) -> Schema {
323-
return self.init(type: .boolean, description: description, nullable: nullable)
334+
public static func boolean(description: String? = nil, title: String? = nil,
335+
nullable: Bool = false) -> Schema {
336+
return self.init(type: .boolean, description: description, title: title, nullable: nullable)
324337
}
325338

326339
/// Returns a `Schema` representing an array.
@@ -334,17 +347,20 @@ public final class Schema: Sendable {
334347
/// - items: The `Schema` of the elements that the array will hold.
335348
/// - description: An optional description of what the array should contain or represent; may
336349
/// use Markdown format.
350+
/// - title: An optional human-readable name/summary for the schema.
337351
/// - nullable: If `true`, instructs the model that it may return `null` instead of an array;
338352
/// defaults to `false`, enforcing that an array is returned.
339353
/// - minItems: Instructs the model to produce at least the specified minimum number of elements
340354
/// in the array; defaults to `nil`, meaning any number.
341355
/// - maxItems: Instructs the model to produce at most the specified maximum number of elements
342356
/// in the array.
343-
public static func array(items: Schema, description: String? = nil, nullable: Bool = false,
344-
minItems: Int? = nil, maxItems: Int? = nil) -> Schema {
357+
public static func array(items: Schema, description: String? = nil, title: String? = nil,
358+
nullable: Bool = false, minItems: Int? = nil,
359+
maxItems: Int? = nil) -> Schema {
345360
return self.init(
346361
type: .array,
347362
description: description,
363+
title: title,
348364
nullable: nullable,
349365
items: items,
350366
minItems: minItems,
@@ -384,7 +400,7 @@ public final class Schema: Sendable {
384400
/// generated JSON string. See ``propertyOrdering`` for details.
385401
/// - description: An optional description of what the object should contain or represent; may
386402
/// use Markdown format.
387-
/// - title: An optional human-readable name/summary for the object schema.
403+
/// - title: An optional human-readable name/summary for the schema.
388404
/// - nullable: If `true`, instructs the model that it may return `null` instead of an object;
389405
/// defaults to `false`, enforcing that an object is returned.
390406
public static func object(properties: [String: Schema], optionalProperties: [String] = [],

FirebaseAI/Tests/Unit/Types/SchemaTests.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ final class SchemaTests: XCTestCase {
4343

4444
func testEncodeSchema_string_allOptions() throws {
4545
let description = "Timestamp of the event."
46+
let title = "Event Timestamp"
4647
let format = Schema.StringFormat.custom("date-time")
47-
let schema = Schema.string(description: description, nullable: true, format: format)
48+
let schema = Schema.string(
49+
description: description,
50+
title: title,
51+
nullable: true,
52+
format: format
53+
)
4854

4955
let jsonData = try encoder.encode(schema)
5056

@@ -54,6 +60,7 @@ final class SchemaTests: XCTestCase {
5460
"description" : "\(description)",
5561
"format" : "date-time",
5662
"nullable" : true,
63+
"title": "\(title)",
5764
"type" : "STRING"
5865
}
5966
""")
@@ -85,7 +92,13 @@ final class SchemaTests: XCTestCase {
8592
func testEncodeSchema_enumeration_allOptions() throws {
8693
let values = ["NORTH", "SOUTH", "EAST", "WEST"]
8794
let description = "Compass directions."
88-
let schema = Schema.enumeration(values: values, description: description, nullable: true)
95+
let title = "Directions"
96+
let schema = Schema.enumeration(
97+
values: values,
98+
description: description,
99+
title: title,
100+
nullable: true
101+
)
89102

90103
let jsonData = try encoder.encode(schema)
91104

@@ -101,6 +114,7 @@ final class SchemaTests: XCTestCase {
101114
],
102115
"format" : "enum",
103116
"nullable" : true,
117+
"title": "\(title)",
104118
"type" : "STRING"
105119
}
106120
""")
@@ -125,10 +139,12 @@ final class SchemaTests: XCTestCase {
125139

126140
func testEncodeSchema_float_allOptions() throws {
127141
let description = "Temperature in Celsius."
142+
let title = "Temperature (°C)"
128143
let minimum: Float = -40.25
129144
let maximum: Float = 50.5
130145
let schema = Schema.float(
131146
description: description,
147+
title: title,
132148
nullable: true,
133149
minimum: minimum,
134150
maximum: maximum
@@ -144,6 +160,7 @@ final class SchemaTests: XCTestCase {
144160
"maximum" : \(maximum),
145161
"minimum" : \(minimum),
146162
"nullable" : true,
163+
"title": "\(title)",
147164
"type" : "NUMBER"
148165
}
149166
""")
@@ -167,10 +184,12 @@ final class SchemaTests: XCTestCase {
167184

168185
func testEncodeSchema_double_allOptions() throws {
169186
let description = "Account balance."
187+
let title = "Balance"
170188
let minimum = 0.01
171189
let maximum = 1_000_000.99
172190
let schema = Schema.double(
173191
description: description,
192+
title: title,
174193
nullable: true,
175194
minimum: minimum,
176195
maximum: maximum
@@ -185,6 +204,7 @@ final class SchemaTests: XCTestCase {
185204
"maximum" : \(maximum),
186205
"minimum" : \(minimum),
187206
"nullable" : true,
207+
"title": "\(title)",
188208
"type" : "NUMBER"
189209
}
190210
""")
@@ -208,11 +228,13 @@ final class SchemaTests: XCTestCase {
208228

209229
func testEncodeSchema_integer_allOptions() throws {
210230
let description = "User age."
231+
let title = "Age"
211232
let minimum = 0
212233
let maximum = 120
213234
let format = Schema.IntegerFormat.int32
214235
let schema = Schema.integer(
215236
description: description,
237+
title: title,
216238
nullable: true,
217239
format: format,
218240
minimum: minimum,
@@ -229,6 +251,7 @@ final class SchemaTests: XCTestCase {
229251
"maximum" : \(maximum),
230252
"minimum" : \(minimum),
231253
"nullable" : true,
254+
"title": "\(title)",
232255
"type" : "INTEGER"
233256
}
234257
""")
@@ -252,7 +275,8 @@ final class SchemaTests: XCTestCase {
252275

253276
func testEncodeSchema_boolean_allOptions() throws {
254277
let description = "Is the user an administrator?"
255-
let schema = Schema.boolean(description: description, nullable: true)
278+
let title = "Administrator Check"
279+
let schema = Schema.boolean(description: description, title: title, nullable: true)
256280

257281
let jsonData = try encoder.encode(schema)
258282

@@ -261,6 +285,7 @@ final class SchemaTests: XCTestCase {
261285
{
262286
"description" : "\(description)",
263287
"nullable" : true,
288+
"title": "\(title)",
264289
"type" : "BOOLEAN"
265290
}
266291
""")
@@ -290,11 +315,13 @@ final class SchemaTests: XCTestCase {
290315
func testEncodeSchema_array_allOptions() throws {
291316
let itemsSchema = Schema.integer(format: .int64)
292317
let description = "List of product IDs."
318+
let title = "Product IDs"
293319
let minItems = 1
294320
let maxItems = 10
295321
let schema = Schema.array(
296322
items: itemsSchema,
297323
description: description,
324+
title: title,
298325
nullable: true,
299326
minItems: minItems,
300327
maxItems: maxItems
@@ -314,6 +341,7 @@ final class SchemaTests: XCTestCase {
314341
"maxItems" : \(maxItems),
315342
"minItems" : \(minItems),
316343
"nullable" : true,
344+
"title": "\(title)",
317345
"type" : "ARRAY"
318346
}
319347
""")

0 commit comments

Comments
 (0)