Skip to content

Commit 28a8cd0

Browse files
Mateusz Krawieccopybara-github
authored andcommitted
chore!: remove deprecated Example processor
PiperOrigin-RevId: 884881559
1 parent fca43fb commit 28a8cd0

File tree

9 files changed

+73
-204
lines changed

9 files changed

+73
-204
lines changed

core/src/main/java/com/google/adk/agents/LlmAgent.java

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
import com.google.adk.agents.ConfigAgentUtils.ConfigurationException;
4646
import com.google.adk.codeexecutors.BaseCodeExecutor;
4747
import com.google.adk.events.Event;
48-
import com.google.adk.examples.BaseExampleProvider;
49-
import com.google.adk.examples.Example;
5048
import com.google.adk.flows.llmflows.AutoFlow;
5149
import com.google.adk.flows.llmflows.BaseLlmFlow;
5250
import com.google.adk.flows.llmflows.SingleFlow;
@@ -97,8 +95,6 @@ public enum IncludeContents {
9795
private final List<Object> toolsUnion;
9896
private final ImmutableList<BaseToolset> toolsets;
9997
private final Optional<GenerateContentConfig> generateContentConfig;
100-
// TODO: Remove exampleProvider field - examples should only be provided via ExampleTool
101-
private final Optional<BaseExampleProvider> exampleProvider;
10298
private final IncludeContents includeContents;
10399

104100
private final boolean planning;
@@ -132,7 +128,6 @@ protected LlmAgent(Builder builder) {
132128
this.globalInstruction =
133129
requireNonNullElse(builder.globalInstruction, new Instruction.Static(""));
134130
this.generateContentConfig = Optional.ofNullable(builder.generateContentConfig);
135-
this.exampleProvider = Optional.ofNullable(builder.exampleProvider);
136131
this.includeContents = requireNonNullElse(builder.includeContents, IncludeContents.DEFAULT);
137132
this.planning = builder.planning != null && builder.planning;
138133
this.maxSteps = Optional.ofNullable(builder.maxSteps);
@@ -180,7 +175,6 @@ public static class Builder extends BaseAgent.Builder<Builder> {
180175
private Instruction globalInstruction;
181176
private ImmutableList<Object> toolsUnion;
182177
private GenerateContentConfig generateContentConfig;
183-
private BaseExampleProvider exampleProvider;
184178
private IncludeContents includeContents;
185179
private Boolean planning;
186180
private Integer maxSteps;
@@ -253,26 +247,6 @@ public Builder generateContentConfig(GenerateContentConfig generateContentConfig
253247
return this;
254248
}
255249

256-
// TODO: Remove these example provider methods and only use ExampleTool for providing examples.
257-
// Direct example methods should be deprecated in favor of using ExampleTool consistently.
258-
@CanIgnoreReturnValue
259-
public Builder exampleProvider(BaseExampleProvider exampleProvider) {
260-
this.exampleProvider = exampleProvider;
261-
return this;
262-
}
263-
264-
@CanIgnoreReturnValue
265-
public Builder exampleProvider(List<Example> examples) {
266-
this.exampleProvider = (unused) -> examples;
267-
return this;
268-
}
269-
270-
@CanIgnoreReturnValue
271-
public Builder exampleProvider(Example... examples) {
272-
this.exampleProvider = (unused) -> ImmutableList.copyOf(examples);
273-
return this;
274-
}
275-
276250
@CanIgnoreReturnValue
277251
public Builder includeContents(IncludeContents includeContents) {
278252
this.includeContents = includeContents;
@@ -640,10 +614,18 @@ protected void validate() {
640614
+ " transfer.");
641615
}
642616
if (this.toolsUnion != null && !this.toolsUnion.isEmpty()) {
643-
throw new IllegalArgumentException(
644-
"Invalid config for agent "
645-
+ this.name
646-
+ ": if outputSchema is set, tools must be empty.");
617+
boolean hasOtherTools =
618+
this.toolsUnion.stream()
619+
.anyMatch(
620+
tool ->
621+
!(tool instanceof BaseTool baseTool)
622+
|| !baseTool.name().equals("example_tool"));
623+
if (hasOtherTools) {
624+
throw new IllegalArgumentException(
625+
"Invalid config for agent "
626+
+ this.name
627+
+ ": if outputSchema is set, tools must be empty.");
628+
}
647629
}
648630
}
649631
}
@@ -812,11 +794,6 @@ public Optional<GenerateContentConfig> generateContentConfig() {
812794
return generateContentConfig;
813795
}
814796

815-
// TODO: Remove this getter - examples should only be provided via ExampleTool
816-
public Optional<BaseExampleProvider> exampleProvider() {
817-
return exampleProvider;
818-
}
819-
820797
public IncludeContents includeContents() {
821798
return includeContents;
822799
}

core/src/main/java/com/google/adk/examples/ExampleUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public final class ExampleUtils {
6464
* @return string representation of the examples block.
6565
*/
6666
private static String convertExamplesToText(List<Example> examples) {
67+
if (examples.isEmpty()) {
68+
return "";
69+
}
6770
StringBuilder examplesStr = new StringBuilder();
6871

6972
// super header

core/src/main/java/com/google/adk/flows/llmflows/Examples.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

core/src/main/java/com/google/adk/flows/llmflows/SingleFlow.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class SingleFlow extends BaseLlmFlow {
3232
new Identity(),
3333
new Compaction(),
3434
new Contents(),
35-
new Examples(),
3635
CodeExecution.requestProcessor);
3736

3837
protected static final ImmutableList<ResponseProcessor> RESPONSE_PROCESSORS =

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public Completable processLlmRequest(
8585
return Completable.complete();
8686
}
8787

88-
llmRequestBuilder.appendInstructions(ImmutableList.of(examplesBlock));
88+
if (!examplesBlock.isEmpty()) {
89+
llmRequestBuilder.appendInstructions(ImmutableList.of(examplesBlock));
90+
}
8991
// Delegate to BaseTool to keep any declaration bookkeeping (none for this tool)
9092
return super.processLlmRequest(llmRequestBuilder, toolContext);
9193
}

core/src/test/java/com/google/adk/agents/LlmAgentTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.adk.agents.Callbacks.OnModelErrorCallback;
3636
import com.google.adk.agents.Callbacks.OnToolErrorCallback;
3737
import com.google.adk.events.Event;
38+
import com.google.adk.examples.Example;
3839
import com.google.adk.models.LlmRegistry;
3940
import com.google.adk.models.LlmRequest;
4041
import com.google.adk.models.LlmResponse;
@@ -46,6 +47,7 @@
4647
import com.google.adk.testing.TestUtils.EchoTool;
4748
import com.google.adk.tools.BaseTool;
4849
import com.google.adk.tools.BaseToolset;
50+
import com.google.adk.tools.ExampleTool;
4951
import com.google.common.collect.ImmutableList;
5052
import com.google.common.collect.ImmutableMap;
5153
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@@ -649,4 +651,30 @@ private SpanData findSpanByName(List<SpanData> spans, String name) {
649651
.findFirst()
650652
.orElseThrow(() -> new AssertionError("Span not found: " + name));
651653
}
654+
655+
@Test
656+
public void run_withExampleTool_doesNotAddFunctionDeclarations() {
657+
ExampleTool tool =
658+
ExampleTool.builder()
659+
.addExample(
660+
Example.builder()
661+
.input(Content.fromParts(Part.fromText("qin")))
662+
.output(ImmutableList.of(Content.fromParts(Part.fromText("qout"))))
663+
.build())
664+
.build();
665+
666+
Content modelContent = Content.fromParts(Part.fromText("Real LLM response"));
667+
TestLlm testLlm = createTestLlm(createLlmResponse(modelContent));
668+
LlmAgent agent = createTestAgentBuilder(testLlm).tools(tool).build();
669+
InvocationContext invocationContext = createInvocationContext(agent);
670+
671+
var unused = agent.runAsync(invocationContext).toList().blockingGet();
672+
673+
assertThat(testLlm.getRequests()).hasSize(1);
674+
LlmRequest request = testLlm.getRequests().get(0);
675+
676+
assertThat(request.config().isPresent()).isTrue();
677+
var config = request.config().get();
678+
assertThat(config.tools().isPresent()).isFalse();
679+
}
652680
}

core/src/test/java/com/google/adk/examples/ExampleUtilsTest.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,7 @@ public List<Example> getExamples(String query) {
4949
@Test
5050
public void buildFewShotFewShot_noExamples() {
5151
TestExampleProvider exampleProvider = new TestExampleProvider(ImmutableList.of());
52-
String expected =
53-
"""
54-
<EXAMPLES>
55-
Begin few-shot
56-
The following are examples of user queries and model responses using the available tools.
57-
58-
End few-shot
59-
Now, try to follow these examples and complete the following conversation
60-
<EXAMPLES>\
61-
""";
62-
assertThat(ExampleUtils.buildExampleSi(exampleProvider, "test query")).isEqualTo(expected);
52+
assertThat(ExampleUtils.buildExampleSi(exampleProvider, "test query")).isEmpty();
6353
}
6454

6555
@Test

core/src/test/java/com/google/adk/flows/llmflows/ExamplesTest.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,30 @@ static final class WrongTypeProviderHolder {
305305

306306
private WrongTypeProviderHolder() {}
307307
}
308+
309+
@Test
310+
public void declaration_isEmpty() {
311+
ExampleTool tool = ExampleTool.builder().build();
312+
assertThat(tool.declaration().isPresent()).isFalse();
313+
}
314+
315+
@Test
316+
public void processLlmRequest_doesNotAddFunctionDeclarations() {
317+
ExampleTool tool = ExampleTool.builder().addExample(makeExample("qin", "qout")).build();
318+
InvocationContext ctx = buildInvocationContext();
319+
LlmRequest.Builder builder = LlmRequest.builder().model("gemini-2.0-flash");
320+
321+
tool.processLlmRequest(builder, ToolContext.builder(ctx).build()).blockingAwait();
322+
LlmRequest updated = builder.build();
323+
324+
if (updated.config().isPresent()) {
325+
var config = updated.config().get();
326+
if (config.tools().isPresent()) {
327+
var tools = config.tools().get();
328+
boolean hasFunctionDeclarations =
329+
tools.stream().anyMatch(t -> t.functionDeclarations().isPresent());
330+
assertThat(hasFunctionDeclarations).isFalse();
331+
}
332+
}
333+
}
308334
}

0 commit comments

Comments
 (0)