Skip to content
Closed
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
35 changes: 26 additions & 9 deletions core/src/main/java/com/google/adk/agents/LlmAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.adk.agents;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -54,7 +55,6 @@
import com.google.adk.models.Model;
import com.google.adk.tools.BaseTool;
import com.google.adk.tools.BaseToolset;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.genai.types.Content;
Expand Down Expand Up @@ -155,7 +155,7 @@ protected LlmAgent(Builder builder) {
this.llmFlow = determineLlmFlow();

// Validate name not empty.
Preconditions.checkArgument(!this.name().isEmpty(), "Agent name cannot be empty.");
checkArgument(!this.name().isEmpty(), "Agent name cannot be empty.");
}

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

protected void validate() {
this.disallowTransferToParent =
this.disallowTransferToParent != null && this.disallowTransferToParent;
this.disallowTransferToPeers =
this.disallowTransferToPeers != null && this.disallowTransferToPeers;
if (this.generateContentConfig != null) {
checkArgument(
this.generateContentConfig.tools().isEmpty(),
"All tools must be set via LlmAgent.builder().tools().");
checkArgument(
this.generateContentConfig.systemInstruction().isEmpty(),
"System instruction must be set via LlmAgent.builder().instruction().");
checkArgument(
this.generateContentConfig.responseSchema().isEmpty(),
"Response schema must be set via LlmAgent.builder().outputSchema().");
}

if (this.outputSchema != null) {
if (!this.disallowTransferToParent || !this.disallowTransferToPeers) {
if (Objects.equals(this.disallowTransferToParent, false)
|| Objects.equals(this.disallowTransferToPeers, false)) {
logger.warn(
"Invalid config for agent {}: outputSchema cannot co-exist with agent transfer"
+ " configurations. Setting disallowTransferToParent=true and"
+ " disallowTransferToPeers=true.",
this.name);
this.disallowTransferToParent = true;
this.disallowTransferToPeers = true;
}
this.disallowTransferToParent = true;
this.disallowTransferToPeers = true;

if (this.subAgents != null && !this.subAgents.isEmpty()) {
throw new IllegalArgumentException(
Expand All @@ -642,6 +650,11 @@ protected void validate() {
+ this.name
+ ": if outputSchema is set, tools must be empty.");
}
} else {
this.disallowTransferToParent =
this.disallowTransferToParent != null && this.disallowTransferToParent;
this.disallowTransferToPeers =
this.disallowTransferToPeers != null && this.disallowTransferToPeers;
}
}

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

if (rawResult.isBlank()) {
return;
}

Optional<Schema> outputSchema = outputSchema();
if (outputSchema.isPresent()) {
try {
Expand Down