Skip to content
Open
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: 4 additions & 0 deletions core/src/main/java/com/google/adk/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ public final boolean finalResponse() {
if (actions().skipSummarization().orElse(false) || hasPendingLongRunningToolCall()) {
return true;
}
// An error ends the turn unless the model still asked for tools; mirrors Python ADK.
if (errorCode().isPresent() && !partial().orElse(false) && functionCalls().isEmpty()) {
return true;
}
return functionCalls().isEmpty()
&& functionResponses().isEmpty()
&& !partial().orElse(false)
Expand Down
110 changes: 110 additions & 0 deletions core/src/test/java/com/google/adk/events/EventTest.java

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right now, nothing checks that the error code makes the difference. If you drop errorCode().isPresent() from the new condition, every test in EventTest still passes, because none verify that an error-free function-response event is not final.

Could you port test_is_final_response_with_function_response_is_not_final and test_is_final_response_with_trailing_code_result_is_not_final from adk-python? It would also be great to add a test showing that an error event ending with a code execution result is final.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 617dff4: ported test_is_final_response_with_function_response_is_not_final and test_is_final_response_with_trailing_code_result_is_not_final, and added finalResponse_isTrueForErrorEventWithTrailingCodeExecutionResult. Without errorCode().isPresent() the first two now fail.

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.genai.types.CodeExecutionResult;
import com.google.genai.types.Content;
import com.google.genai.types.FinishReason;
import com.google.genai.types.FunctionCall;
Expand Down Expand Up @@ -439,6 +440,115 @@ public void finalResponse_returnsTrueIfSkipSummarization() {
assertThat(event.finalResponse()).isTrue();
}

@Test
public void finalResponse_isTrueForErrorEvent() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(Content.fromParts(Part.fromText("hello")))
.errorCode(new FinishReason("SOME_ERROR"))
.errorMessage("Something went wrong")
.build();
assertThat(event.finalResponse()).isTrue();
}

@Test
public void finalResponse_isTrueForErrorEventWithFunctionResponse() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(
Content.fromParts(Part.fromFunctionResponse("tool", ImmutableMap.of("k", "v"))))
.errorCode(new FinishReason("SOME_ERROR"))
.errorMessage("Something went wrong")
.build();
assertThat(event.finalResponse()).isTrue();
}

@Test
public void finalResponse_isFalseForErrorEventWithFunctionCall() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(Content.fromParts(Part.fromFunctionCall("tool", ImmutableMap.of("k", "v"))))
.errorCode(new FinishReason("SOME_ERROR"))
.errorMessage("Something went wrong")
.build();
assertThat(event.finalResponse()).isFalse();
}

@Test
public void finalResponse_isFalseForPartialErrorEvent() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(Content.fromParts(Part.fromText("hello")))
.errorCode(new FinishReason("SOME_ERROR"))
.errorMessage("Something went wrong")
.partial(true)
.build();
assertThat(event.finalResponse()).isFalse();
}

@Test
public void finalResponse_isFalseForFunctionResponse() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(
Content.fromParts(Part.fromFunctionResponse("tool", ImmutableMap.of("k", "v"))))
.build();
assertThat(event.finalResponse()).isFalse();
}

@Test
public void finalResponse_isFalseForTrailingCodeExecutionResult() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(
Content.fromParts(
Part.fromText("hello"),
Part.builder()
.codeExecutionResult(
CodeExecutionResult.builder().outcome("OK").output("42").build())
.build()))
.build();
assertThat(event.finalResponse()).isFalse();
}

@Test
public void finalResponse_isTrueForErrorEventWithTrailingCodeExecutionResult() {
Event event =
Event.builder()
.id("e1")
.invocationId("i1")
.author("agent")
.content(
Content.fromParts(
Part.fromText("hello"),
Part.builder()
.codeExecutionResult(
CodeExecutionResult.builder().outcome("OK").output("42").build())
.build()))
.errorCode(new FinishReason("SOME_ERROR"))
.errorMessage("Something went wrong")
.build();
assertThat(event.finalResponse()).isTrue();
}

private static String eventJsonWithNodeInfo(String nodeInfoJson) {
return "{\"id\":\"event_id\",\"author\":\"agent\",\"nodeInfo\":" + nodeInfoJson + "}";
}
Expand Down
Loading