Skip to content

Commit 2a86ae8

Browse files
Poggeccicopybara-github
authored andcommitted
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
1 parent 179efbe commit 2a86ae8

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

core/src/main/java/com/google/adk/tools/AgentTool.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@ public Single<Map<String, Object>> runAsync(Map<String, Object> args, ToolContex
120120
return ImmutableMap.of();
121121
}
122122
Event lastEvent = optionalLastEvent.get();
123-
Optional<String> outputText =
124-
lastEvent
125-
.content()
126-
.flatMap(Content::parts)
127-
.filter(parts -> !parts.isEmpty())
128-
.flatMap(parts -> parts.get(0).text());
123+
Optional<String> outputText = lastEvent.content().map(Content::text);
129124

130125
if (outputText.isEmpty()) {
131126
return ImmutableMap.of();

core/src/test/java/com/google/adk/tools/AgentToolTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void call_withInputAndOutputSchema_successful() throws Exception {
227227
}
228228

229229
@Test
230-
public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Exception {
230+
public void call_withoutSchema_returnsConcatenatedTextFromLastEvent() throws Exception {
231231
LlmAgent testAgent =
232232
createTestAgentBuilder(
233233
createTestLlm(
@@ -240,8 +240,8 @@ public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Except
240240
LlmResponse.builder()
241241
.content(
242242
Content.fromParts(
243-
Part.fromText("First text part is returned"),
244-
Part.fromText("This should be ignored")))
243+
Part.fromText("First text part. "),
244+
Part.fromText("Second text part.")))
245245
.build())))
246246
.name("agent name")
247247
.description("agent description")
@@ -252,7 +252,31 @@ public void call_withoutSchema_returnsFirstTextPartFromLastEvent() throws Except
252252
Map<String, Object> result =
253253
agentTool.runAsync(ImmutableMap.of("request", "magic"), toolContext).blockingGet();
254254

255-
assertThat(result).containsExactly("result", "First text part is returned");
255+
assertThat(result).containsExactly("result", "First text part. Second text part.");
256+
}
257+
258+
@Test
259+
public void call_withThoughts_returnsOnlyNonThoughtText() throws Exception {
260+
TestLlm testLlm =
261+
createTestLlm(
262+
LlmResponse.builder()
263+
.content(
264+
Content.builder()
265+
.parts(
266+
Part.fromText("Non-thought text 1. "),
267+
Part.builder().text("This is a thought.").thought(true).build(),
268+
Part.fromText("Non-thought text 2."))
269+
.build())
270+
.build());
271+
LlmAgent testAgent =
272+
createTestAgentBuilder(testLlm).name("agent name").description("agent description").build();
273+
AgentTool agentTool = AgentTool.create(testAgent);
274+
ToolContext toolContext = createToolContext(testAgent);
275+
276+
Map<String, Object> result =
277+
agentTool.runAsync(ImmutableMap.of("request", "test"), toolContext).blockingGet();
278+
279+
assertThat(result).containsExactly("result", "Non-thought text 1. Non-thought text 2.");
256280
}
257281

258282
@Test

0 commit comments

Comments
 (0)