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
4 changes: 3 additions & 1 deletion core/src/main/java/com/google/adk/models/GeminiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;

import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -128,7 +129,8 @@ public static LlmRequest sanitizeRequestForGeminiApi(LlmRequest llmRequest) {
*/
static List<Content> ensureModelResponse(List<Content> contents) {
// Last content must be from the user, otherwise the model won't respond.
if (contents.isEmpty() || !Iterables.getLast(contents).role().orElse("").equals("user")) {
if (contents.isEmpty()
|| !Ascii.equalsIgnoreCase(Iterables.getLast(contents).role().orElse(""), "user")) {
Content userContent =
Content.builder()
.parts(ImmutableList.of(Part.fromText(CONTINUE_OUTPUT_MESSAGE)))
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/java/com/google/adk/models/GeminiUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,17 @@ public void ensureModelResponse_userRoleIsLast_returnsSameList() {
assertThat(result).containsExactly(modelContent, userContent).inOrder();
}

@Test
public void ensureModelResponse_userRoleIsLastCaseInsensitive_returnsSameList() {
Content modelContent = Content.builder().role("model").build();
Content userContent = Content.builder().role("USER").build();
ImmutableList<Content> contents = ImmutableList.of(modelContent, userContent);

List<Content> result = GeminiUtil.ensureModelResponse(contents);

assertThat(result).containsExactly(modelContent, userContent).inOrder();
}

@Test
public void ensureModelResponse_lastContentIsNotUser_appendsContinueMessage() {
Content modelContent1 = Content.builder().role("model").build();
Expand Down