Skip to content

Commit c59b966

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: Improving validation in LlmAgent
PiperOrigin-RevId: 861164205
1 parent 5ba63f4 commit c59b966

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.adk.agents;
1818

19+
import static com.google.common.base.Preconditions.checkArgument;
1920
import static com.google.common.collect.ImmutableList.toImmutableList;
2021
import static java.util.stream.Collectors.joining;
2122

@@ -54,7 +55,6 @@
5455
import com.google.adk.models.Model;
5556
import com.google.adk.tools.BaseTool;
5657
import com.google.adk.tools.BaseToolset;
57-
import com.google.common.base.Preconditions;
5858
import com.google.common.collect.ImmutableList;
5959
import com.google.errorprone.annotations.CanIgnoreReturnValue;
6060
import com.google.genai.types.Content;
@@ -155,7 +155,7 @@ protected LlmAgent(Builder builder) {
155155
this.llmFlow = determineLlmFlow();
156156

157157
// Validate name not empty.
158-
Preconditions.checkArgument(!this.name().isEmpty(), "Agent name cannot be empty.");
158+
checkArgument(!this.name().isEmpty(), "Agent name cannot be empty.");
159159
}
160160

161161
/** Returns a {@link Builder} for {@link LlmAgent}. */
@@ -613,21 +613,29 @@ private static <B, A> ImmutableList<A> convertCallbacks(
613613
}
614614

615615
protected void validate() {
616-
this.disallowTransferToParent =
617-
this.disallowTransferToParent != null && this.disallowTransferToParent;
618-
this.disallowTransferToPeers =
619-
this.disallowTransferToPeers != null && this.disallowTransferToPeers;
616+
if (this.generateContentConfig != null) {
617+
checkArgument(
618+
this.generateContentConfig.tools().isEmpty(),
619+
"All tools must be set via LlmAgent.builder().tools().");
620+
checkArgument(
621+
this.generateContentConfig.systemInstruction().isEmpty(),
622+
"System instruction must be set via LlmAgent.builder().instruction().");
623+
checkArgument(
624+
this.generateContentConfig.responseSchema().isEmpty(),
625+
"Response schema must be set via LlmAgent.builder().outputSchema().");
626+
}
620627

621628
if (this.outputSchema != null) {
622-
if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
629+
if (Objects.equals(this.disallowTransferToParent, false)
630+
|| Objects.equals(this.disallowTransferToPeers, false)) {
623631
logger.warn(
624632
"Invalid config for agent {}: outputSchema cannot co-exist with agent transfer"
625633
+ " configurations. Setting disallowTransferToParent=true and"
626634
+ " disallowTransferToPeers=true.",
627635
this.name);
628-
this.disallowTransferToParent = true;
629-
this.disallowTransferToPeers = true;
630636
}
637+
this.disallowTransferToParent = true;
638+
this.disallowTransferToPeers = true;
631639

632640
if (this.subAgents != null && !this.subAgents.isEmpty()) {
633641
throw new IllegalArgumentException(
@@ -642,6 +650,11 @@ protected void validate() {
642650
+ this.name
643651
+ ": if outputSchema is set, tools must be empty.");
644652
}
653+
} else {
654+
this.disallowTransferToParent =
655+
this.disallowTransferToParent != null && this.disallowTransferToParent;
656+
this.disallowTransferToPeers =
657+
this.disallowTransferToPeers != null && this.disallowTransferToPeers;
645658
}
646659
}
647660

@@ -670,6 +683,10 @@ private void maybeSaveOutputToState(Event event) {
670683
.map(part -> part.text().orElse(""))
671684
.collect(joining());
672685

686+
if (rawResult.isBlank()) {
687+
return;
688+
}
689+
673690
Optional<Schema> outputSchema = outputSchema();
674691
if (outputSchema.isPresent()) {
675692
try {

0 commit comments

Comments
 (0)