Skip to content

Commit 22ace25

Browse files
Improved refdocs for Schema (#6420)
The improvements include: - Clarify that `integer` and `float` types are hints to the model, and the returned data could potentially overflow the respective native types. - Include missing parameters for some of the functions. - Add examples for `enumerations` and `obj`. --------- Co-authored-by: rachelsaunders <[email protected]>
1 parent 5687f6d commit 22ace25

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

firebase-vertexai/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Unreleased
22
* [fixed] Fixed issue where Firebase App Check error tokens were unintentionally missing from the requests. (#6409)
3-
3+
* [fixed] Clarified in the documentation that `Schema.integer` and `Schema.float` only provide hints to the model. (#6420)
44

55
# 16.0.1
66
* [fixed] Fixed issue where authorization headers weren't correctly formatted and were ignored by the backend. (#6400)

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/type/Schema.kt

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ public abstract class StringFormat private constructor(internal val value: Strin
2020
public class Custom(value: String) : StringFormat(value)
2121
}
2222

23-
/** Represents a schema */
23+
/**
24+
* Definition of a data type.
25+
*
26+
* These types can be objects, but also primitives and arrays. Represents a select subset of an
27+
* [OpenAPI 3.0 schema object](https://spec.openapis.org/oas/v3.0.3#schema).
28+
*
29+
* **Note:** While optional, including a `description` field in your `Schema` is strongly
30+
* encouraged. The more information the model has about what it's expected to generate, the better
31+
* the results.
32+
*/
2433
public class Schema
2534
internal constructor(
2635
public val type: String,
@@ -34,7 +43,12 @@ internal constructor(
3443
) {
3544

3645
public companion object {
37-
/** Returns a schema for a boolean */
46+
/**
47+
* Returns a [Schema] representing a boolean value.
48+
*
49+
* @param description An optional description of what the boolean should contain or represent.
50+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
51+
*/
3852
@JvmStatic
3953
@JvmOverloads
4054
public fun boolean(description: String? = null, nullable: Boolean = false): Schema =
@@ -45,10 +59,14 @@ internal constructor(
4559
)
4660

4761
/**
48-
* Returns a schema for a 32-bit integer number
62+
* Returns a [Schema] for a 32-bit signed integer number.
4963
*
50-
* @param description: The description of what the parameter should contain or represent
51-
* @param nullable: Whether null is a valid value for this schema
64+
* **Important:** This [Schema] provides a hint to the model that it should generate a 32-bit
65+
* integer, but only guarantees that the value will be an integer. Therefore it's *possible*
66+
* that decoding it as an `Int` variable (or `int` in Java) could overflow.
67+
*
68+
* @param description An optional description of what the integer should contain or represent.
69+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
5270
*/
5371
@JvmStatic
5472
@JvmName("numInt")
@@ -62,10 +80,10 @@ internal constructor(
6280
)
6381

6482
/**
65-
* Returns a schema for a 64-bit integer number
83+
* Returns a [Schema] for a 64-bit signed integer number.
6684
*
67-
* @param description: The description of what the parameter should contain or represent
68-
* @param nullable: Whether null is a valid value for this schema
85+
* @param description An optional description of what the number should contain or represent.
86+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
6987
*/
7088
@JvmStatic
7189
@JvmName("numLong")
@@ -78,10 +96,10 @@ internal constructor(
7896
)
7997

8098
/**
81-
* Returns a schema for a floating point number
99+
* Returns a [Schema] for a double-precision floating-point number.
82100
*
83-
* @param description: The description of what the parameter should contain or represent
84-
* @param nullable: Whether null is a valid value for this schema
101+
* @param description An optional description of what the number should contain or represent.
102+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
85103
*/
86104
@JvmStatic
87105
@JvmName("numDouble")
@@ -90,10 +108,15 @@ internal constructor(
90108
Schema(description = description, nullable = nullable, type = "NUMBER", format = "double")
91109

92110
/**
93-
* Returns a schema for a floating point number
111+
* Returns a [Schema] for a single-precision floating-point number.
112+
*
113+
* **Important:** This [Schema] provides a hint to the model that it should generate a
114+
* single-precision floating-point number, but only guarantees that the value will be a number.
115+
* Therefore it's *possible* that decoding it as a `Float` variable (or `float` in Java) could
116+
* overflow.
94117
*
95-
* @param description: The description of what the parameter should contain or represent
96-
* @param nullable: Whether null is a valid value for this schema
118+
* @param description An optional description of what the number should contain or represent.
119+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
97120
*/
98121
@JvmStatic
99122
@JvmName("numFloat")
@@ -102,11 +125,11 @@ internal constructor(
102125
Schema(description = description, nullable = nullable, type = "NUMBER", format = "float")
103126

104127
/**
105-
* Returns a schema for a string
128+
* Returns a [Schema] for a string.
106129
*
107-
* @param description: The description of what the parameter should contain or represent
108-
* @param nullable: Whether null is a valid value for this schema
109-
* @param format: The pattern that values need to adhere to
130+
* @param description An optional description of what the string should contain or represent.
131+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
132+
* @param format An optional pattern that values need to adhere to.
110133
*/
111134
@JvmStatic
112135
@JvmName("str")
@@ -124,11 +147,25 @@ internal constructor(
124147
)
125148

126149
/**
127-
* Returns a schema for a complex object. In a function, it will be returned as a [JSONObject].
150+
* Returns a [Schema] for a complex data type.
151+
*
152+
* This schema instructs the model to produce data of type object, which has keys of type
153+
* `String` and values of type [Schema].
128154
*
129-
* @param properties: The map of the object's fields to their schema
130-
* @param description: The description of what the parameter should contain or represent
131-
* @param nullable: Whether null is a valid value for this schema
155+
* **Example:** A `city` could be represented with the following object `Schema`.
156+
* ```
157+
* Schema.obj(mapOf(
158+
* "name" to Schema.string(),
159+
* "population" to Schema.integer()
160+
* ))
161+
* ```
162+
*
163+
* @param properties The map of the object's property names to their [Schema]s.
164+
* @param optionalProperties The list of optional properties. They must correspond to the keys
165+
* provided in the `properties` map. By default it's empty, signaling the model that all
166+
* properties are to be included.
167+
* @param description An optional description of what the object represents.
168+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
132169
*/
133170
@JvmStatic
134171
@JvmOverloads
@@ -153,11 +190,11 @@ internal constructor(
153190
}
154191

155192
/**
156-
* Returns a schema for an array.
193+
* Returns a [Schema] for an array.
157194
*
158-
* @param items: The schema of the elements of this array
159-
* @param description: The description of what the parameter should contain or represent
160-
* @param nullable: Whether null is a valid value for this schema
195+
* @param items The [Schema] of the elements stored in the array.
196+
* @param description An optional description of what the array represents.
197+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
161198
*/
162199
@JvmStatic
163200
@JvmOverloads
@@ -174,11 +211,17 @@ internal constructor(
174211
)
175212

176213
/**
177-
* Returns a schema for an enumeration
214+
* Returns a [Schema] for an enumeration.
215+
*
216+
* For example, the cardinal directions can be represented as:
217+
*
218+
* ```
219+
* Schema.enumeration(listOf("north", "east", "south", "west"), "Cardinal directions")
220+
* ```
178221
*
179-
* @param values: The list of valid values for this enumeration
180-
* @param description: The description of what the parameter should contain or represent
181-
* @param nullable: Whether null is a valid value for this schema
222+
* @param values The list of valid values for this enumeration
223+
* @param description The description of what the parameter should contain or represent
224+
* @param nullable Indicates whether the value can be `null`. Defaults to `false`.
182225
*/
183226
@JvmStatic
184227
@JvmOverloads

0 commit comments

Comments
 (0)