Skip to content

Commit 2f851c7

Browse files
authored
Create the function declaration schema at construction time (#6302)
By creating the schema earlier, any errors when declaring the schema will be caught at the declaration site, which makes debugging much easier.
1 parent fe9d619 commit 2f851c7

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

firebase-vertexai/src/main/kotlin/com/google/firebase/vertexai/internal/util/conversions.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,7 @@ internal fun Tool.toInternal() =
153153
com.google.firebase.vertexai.common.client.Tool(functionDeclarations.map { it.toInternal() })
154154

155155
internal fun FunctionDeclaration.toInternal() =
156-
com.google.firebase.vertexai.common.client.FunctionDeclaration(
157-
name,
158-
description,
159-
Schema(
160-
properties = parameters.mapValues { it.value.toInternal() },
161-
required = parameters.keys.minus(optionalParameters.toSet()).toList(),
162-
type = "OBJECT",
163-
nullable = false,
164-
),
165-
)
156+
com.google.firebase.vertexai.common.client.FunctionDeclaration(name, "", schema.toInternal())
166157

167158
internal fun com.google.firebase.vertexai.type.Schema.toInternal(): Schema =
168159
Schema(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,7 @@ class FunctionDeclaration(
4141
val description: String,
4242
val parameters: Map<String, Schema>,
4343
val optionalParameters: List<String> = emptyList(),
44-
)
44+
) {
45+
internal val schema: Schema =
46+
Schema.obj(properties = parameters, optionalProperties = optionalParameters, nullable = false)
47+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,20 @@ internal constructor(
130130
optionalProperties: List<String> = emptyList(),
131131
description: String? = null,
132132
nullable: Boolean = false,
133-
) =
134-
Schema(
133+
): Schema {
134+
if (!properties.keys.containsAll(optionalProperties)) {
135+
throw IllegalArgumentException(
136+
"All optional properties must be present in properties. Missing: ${optionalProperties.minus(properties.keys)}"
137+
)
138+
}
139+
return Schema(
135140
description = description,
136141
nullable = nullable,
137142
properties = properties,
138143
required = properties.keys.minus(optionalProperties.toSet()).toList(),
139144
type = "OBJECT",
140145
)
146+
}
141147

142148
/**
143149
* Returns a schema for an array.

0 commit comments

Comments
 (0)