Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -167,7 +167,11 @@ data class LlmResponse(
candidate?.finishReason ?: response.promptFeedback?.blockReason?.toFinishReason()

return LlmResponse(
content = candidate?.content,
// Keep content only when it has parts or the turn finished normally, matching Python ADK.
content =
candidate?.content?.takeIf {
it.parts.isNotEmpty() || candidate.finishReason == FinishReason.STOP
},
usageMetadata = response.usageMetadata,
finishReason = finishReason,
errorCode = finishReason?.takeIf { it != FinishReason.STOP }?.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.google.adk.kt.types.GenerateContentResponse
import com.google.adk.kt.types.PromptFeedback
import com.google.adk.kt.types.Role
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -89,6 +90,46 @@ class LlmResponseTest {
assertEquals("Safety filter triggered", llmResponse.errorMessage)
}

@Test
fun testContentlessCandidateHasNoContent() {
// No parts and not STOP: null content, not an empty Content (they serialize differently).
val response =
GenerateContentResponse(
candidates =
listOf(
Candidate(
content = Content(role = Role.MODEL, parts = emptyList()),
finishReason = FinishReason.SAFETY,
)
)
)

val llmResponse = LlmResponse.from(response)

assertNull(llmResponse.content)
}

@Test
fun testCreateStopWithEmptyPartsKeepsContent() {
// A candidate that finished normally keeps its (empty) content rather than dropping it.
val response =
GenerateContentResponse(
candidates =
listOf(
Candidate(
content = Content(role = Role.MODEL, parts = emptyList()),
finishReason = FinishReason.STOP,
)
)
)

val llmResponse = LlmResponse.from(response)

val content = assertNotNull(llmResponse.content)
assertEquals(0, content.parts.size)
assertNull(llmResponse.errorCode)
}

@Test
fun testCreateNoCandidates() {
val response =
Expand Down
Loading