Skip to content

fix: Handling default implementation of ToolCallback#call(String,ToolContext) #3784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ private InternalToolExecutionResult executeToolCall(Prompt prompt, AssistantMess
.observe(() -> {
String toolResult;
try {
toolResult = toolCallback.call(toolInputArguments, toolContext);
if (toolContext != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never be null. The toolContext cromes from buildToolContext, which never returns null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. This can never be null and I will remove this unnecessary check.

toolResult = toolCallback.call(toolInputArguments, toolContext);
}
else {
toolResult = toolCallback.call(toolInputArguments);
}
}
catch (ToolExecutionException ex) {
toolResult = this.toolExecutionExceptionProcessor.process(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.springframework.ai.tool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.definition.ToolDefinition;
import org.springframework.ai.tool.metadata.ToolMetadata;
Expand All @@ -29,6 +32,8 @@
*/
public interface ToolCallback {

Logger logger = LoggerFactory.getLogger(ToolCallback.class);

/**
* Definition used by the AI model to determine when and how to call the tool.
*/
Expand All @@ -53,7 +58,8 @@ default ToolMetadata getToolMetadata() {
*/
default String call(String toolInput, @Nullable ToolContext toolContext) {
if (toolContext != null && !toolContext.getContext().isEmpty()) {
throw new UnsupportedOperationException("Tool context is not supported!");
logger.debug(
"Using default implementation of ToolCallback#call(String,ToolContext) and toolContext will be ignored.");
}
return call(toolInput);
}
Expand Down