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
fix: Update AgentTool to drop thought parts from response
Before we silently assumed that the last event had a single part with the text in it. Now we return all the non-thought text.

PiperOrigin-RevId: 825848406
  • Loading branch information
Poggecci authored and copybara-github committed Oct 30, 2025
commit 2a86ae8122c93d1e31ab65e4fe89eda3ca256c72
7 changes: 1 addition & 6 deletions core/src/main/java/com/google/adk/tools/AgentTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,7 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
return ImmutableMap.of();
}
Event lastEvent = optionalLastEvent.get();
Optional<String> outputText =
lastEvent
.content()
.flatMap(Content::parts)
.filter(parts -> !parts.isEmpty())
.flatMap(parts -> parts.get(0).text());
Optional<String> outputText = lastEvent.content().map(Content::text);

if (outputText.isEmpty()) {
return ImmutableMap.of();
Expand Down
32 changes: 28 additions & 4 deletions core/src/test/java/com/google/adk/tools/AgentToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void call_withInputAndOutputSchema_successful() throws Exception {
}

@Test
public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Exception {
public void call_withoutSchema_returnsConcatenatedTextFromLastEvent() throws Exception {
LlmAgent testAgent =
createTestAgentBuilder(
createTestLlm(
Expand All @@ -240,8 +240,8 @@ public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Except
LlmResponse.builder()
.content(
Content.fromParts(
Part.fromText("First text part is returned"),
Part.fromText("This should be ignored")))
Part.fromText("First text part. "),
Part.fromText("Second text part.")))
.build())))
.name("agent name")
.description("agent description")
Expand All @@ -252,7 +252,31 @@ public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Except
Map<String, Object> result =
agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet();

assertThat(result).containsExactly("result", "First text part is returned");
assertThat(result).containsExactly("result", "First text part. Second text part.");
}

@Test
public void call_withThoughts_returnsOnlyNonThoughtText() throws Exception {
TestLlm testLlm =
createTestLlm(
LlmResponse.builder()
.content(
Content.builder()
.parts(
Part.fromText("Non-thought text 1. "),
Part.builder().text("This is a thought.").thought(true).build(),
Part.fromText("Non-thought text 2."))
.build())
.build());
LlmAgent testAgent =
createTestAgentBuilder(testLlm).name("agent name").description("agent description").build();
AgentTool agentTool = AgentTool.create(testAgent);
ToolContext toolContext = createToolContext(testAgent);

Map<String, Object> result =
agentTool.runAsync(ImmutableMap.of("request", "test"), toolContext).blockingGet();

assertThat(result).containsExactly("result", "Non-thought text 1. Non-thought text 2.");
}

@Test
Expand Down