Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public class GenerateContentResponse(
candidates.first().content.parts.filterIsInstance<FunctionCallPart>()
}

/**
* Convenience field to list all the [CodeExecutionResultPart]s in the response, if they exist.
*/
public val codeExecutionResults: List<CodeExecutionResultPart> by lazy {
candidates.first().content.parts.filterIsInstance<CodeExecutionResultPart>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Calling candidates.first() will throw a NoSuchElementException if the candidates list is empty. This can happen if the model returns no candidates in the response, which would cause a crash. To prevent this, you should safely access the first candidate using firstOrNull() and provide a default empty list using the elvis operator ?:.

Suggested change
candidates.first().content.parts.filterIsInstance<CodeExecutionResultPart>()
candidates.firstOrNull()?.content?.parts?.filterIsInstance<CodeExecutionResultPart>() ?: emptyList()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/** Convenience field to list all the [ExecutableCodePart]s in the response, if they exist. */
public val executableCodeList: List<ExecutableCodePart> by lazy {
candidates.first().content.parts.filterIsInstance<ExecutableCodePart>()
}

/**
* Convenience field representing all the [InlineDataPart]s in the first candidate, if they exist.
*
Expand Down
15 changes: 11 additions & 4 deletions firebase-ai/src/main/kotlin/com/google/firebase/ai/type/Tool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ import kotlinx.serialization.json.JsonObject
public class Tool
internal constructor(
internal val functionDeclarations: List<FunctionDeclaration>?,
internal val googleSearch: GoogleSearch?
internal val googleSearch: GoogleSearch?,
internal val codeExecution: JsonObject?,
) {
internal fun toInternal() =
Internal(
functionDeclarations?.map { it.toInternal() } ?: emptyList(),
googleSearch = this.googleSearch?.toInternal()
googleSearch = this.googleSearch?.toInternal(),
codeExecution = this.codeExecution
)
@Serializable
internal data class Internal(
Expand All @@ -49,7 +51,12 @@ internal constructor(
*/
@JvmStatic
public fun functionDeclarations(functionDeclarations: List<FunctionDeclaration>): Tool {
return Tool(functionDeclarations, null)
return Tool(functionDeclarations, null, null)
}

@JvmStatic
public fun codeExecution(): Tool {
return Tool(null, null, JsonObject(emptyMap()))
}

/**
Expand All @@ -70,7 +77,7 @@ internal constructor(
*/
@JvmStatic
public fun googleSearch(googleSearch: GoogleSearch = GoogleSearch()): Tool {
return Tool(null, googleSearch)
return Tool(null, googleSearch, null)
}
}
}
Loading