From 4ae567bb4bd8b929e9cb71d67d76a496b89e1495 Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Wed, 2 Dec 2026 02:44:14 -0800 Subject: [PATCH] fix(tools): read AgentTool's result from the run's last content and error PiperOrigin-RevId: 1034286306 --- .../java/com/google/adk/tools/AgentTool.java | 40 +++++---- .../com/google/adk/tools/AgentToolTest.java | 84 ++++++++++++++++++- 2 files changed, 104 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/com/google/adk/tools/AgentTool.java b/core/src/main/java/com/google/adk/tools/AgentTool.java index 4aaff5f51..0b2a6af31 100644 --- a/core/src/main/java/com/google/adk/tools/AgentTool.java +++ b/core/src/main/java/com/google/adk/tools/AgentTool.java @@ -208,7 +208,7 @@ public Single> runAsync(Map args, ToolContex .instantSource(parentContext.instantSource()) .uuidProvider(parentContext.uuidProvider()) .build(); - // Follow the caller's RunConfig but run unary: only the last event becomes the result. + // Follow the caller's RunConfig but run unary, so the result is not read from partial chunks. RunConfig callerRunConfig = parentContext.runConfig(); RunConfig runConfig = callerRunConfig.streamingMode() == StreamingMode.NONE @@ -226,21 +226,14 @@ public Single> runAsync(Map args, ToolContex updateState(event.actions().stateDelta(), toolContext.state()); } }) - .lastElement() - .map(Optional::of) - .defaultIfEmpty(Optional.empty()) - .map(optionalLastEvent -> { - if (optionalLastEvent.isEmpty()) { - return ImmutableMap.of(); - } - Event lastEvent = optionalLastEvent.get(); - Optional outputText = lastEvent.content().map(Content::text); - - // A run that ends with an error and no text reports the error, as in ADK Python, so the - // model sees the failure instead of an empty result. - String errorMessage = lastEvent.errorMessage().orElse(""); - if (outputText.orElse("").isEmpty() && !errorMessage.isEmpty()) { - return ImmutableMap.of("result", errorMessage); + .reduce(RunOutcome.EMPTY, RunOutcome::with) + .map(outcome -> { + Optional outputText = outcome.lastContent().map(Content::text); + + // A run that ends without text reports its last error, as in ADK Python, so the model + // sees the failure instead of an empty result. + if (outputText.orElse("").isEmpty() && !outcome.lastErrorMessage().isEmpty()) { + return ImmutableMap.of("result", outcome.lastErrorMessage()); } if (outputText.isEmpty()) { return ImmutableMap.of(); @@ -262,6 +255,21 @@ public Single> runAsync(Map args, ToolContex }); } + /** + * The content of the run's last event that has content, and the run's last non-empty error + * message. As in ADK Python and Kotlin, a later event without content, such as a state-only event + * from an after-agent callback, does not hide them. + */ + private record RunOutcome(Optional lastContent, String lastErrorMessage) { + static final RunOutcome EMPTY = new RunOutcome(Optional.empty(), ""); + + RunOutcome with(Event event) { + return new RunOutcome( + event.content().or(() -> lastContent), + event.errorMessage().filter(message -> !message.isEmpty()).orElse(lastErrorMessage)); + } + } + /** * Updates the given state map with the state delta. * diff --git a/core/src/test/java/com/google/adk/tools/AgentToolTest.java b/core/src/test/java/com/google/adk/tools/AgentToolTest.java index 9b53ea298..87411a472 100644 --- a/core/src/test/java/com/google/adk/tools/AgentToolTest.java +++ b/core/src/test/java/com/google/adk/tools/AgentToolTest.java @@ -551,8 +551,7 @@ public void call_emptyErrorMessageWithoutText_returnsEmptyMap() throws Exception } @Test - public void call_textAfterEarlierError_withAfterAgentStateEvent_doesNotReturnError() - throws Exception { + public void call_textAfterEarlierError_withAfterAgentStateEvent_returnsText() throws Exception { Event failure = Event.builder() .id("failure") @@ -578,8 +577,85 @@ public void call_textAfterEarlierError_withAfterAgentStateEvent_doesNotReturnErr Map result = agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); - // The run's last event is the state-only one, which has neither text nor an error. - assertThat(result).isEmpty(); + // The state-only event the callback adds last does not hide the answer. + assertThat(result).containsExactly("result", "content for event answer"); + } + + @Test + public void call_errorWithAfterAgentStateEvent_returnsErrorMessage() throws Exception { + Event failure = + Event.builder() + .id("failure") + .invocationId("invocationId") + .author("remote_agent") + .errorMessage("Failed to communicate with the remote agent (A2AClientException)") + .build(); + AfterAgentCallback writesState = + callbackContext -> { + callbackContext.state().put("done", true); + return Maybe.empty(); + }; + BaseAgent testAgent = + new TestBaseAgent( + "remote_agent", + "agent description", + /* beforeAgentCallbacks= */ null, + ImmutableList.of(writesState), + () -> Flowable.just(failure)); + AgentTool agentTool = AgentTool.create(testAgent); + ToolContext toolContext = createToolContext(testAgent); + + Map result = + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); + + assertThat(result) + .containsExactly( + "result", "Failed to communicate with the remote agent (A2AClientException)"); + } + + @Test + public void call_severalErrors_returnsLastNonEmptyOne() throws Exception { + BaseAgent testAgent = + createSubAgent( + "agent_name", + errorEvent("first", "First failure"), + errorEvent("second", "Second failure"), + errorEvent("third", "")); + AgentTool agentTool = AgentTool.create(testAgent); + ToolContext toolContext = createToolContext(testAgent); + + Map result = + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); + + assertThat(result).containsExactly("result", "Second failure"); + } + + private static Event errorEvent(String id, String errorMessage) { + return Event.builder() + .id(id) + .invocationId("invocationId") + .author("agent_name") + .errorMessage(errorMessage) + .build(); + } + + @Test + public void call_errorAfterText_returnsText() throws Exception { + Event failure = + Event.builder() + .id("failure") + .invocationId("invocationId") + .author("agent_name") + .errorMessage("Transient failure") + .build(); + BaseAgent testAgent = createSubAgent("agent_name", createEvent("answer"), failure); + AgentTool agentTool = AgentTool.create(testAgent); + ToolContext toolContext = createToolContext(testAgent); + + Map result = + agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet(); + + assertThat(result).containsExactly("result", "content for event answer"); } @Test