From 768997d61c53ce567e58a69950ba067308990135 Mon Sep 17 00:00:00 2001 From: Ilayaperumal Gopinathan Date: Thu, 10 Jul 2025 17:42:41 +0100 Subject: [PATCH] fix: Handling default implementation of ToolCallback#call(String,ToolContext) Context: Currently, the default implementation of ToolCallback#call(String,ToolContext) throws exception when an non-empty ToolContext is passed. This gives bad user experience when the application has multiple tool callbacks with one of them doesn't use toolcontext but the chat client passes non-empty toolcontext. For more information #https://github.com/spring-projects/spring-ai/issues/3389 - This fix removes the explicit exception when the toolcontext is non empty for the default implementation of call(String, ToolContext) - Instead, log info message that tool context will be ignored Resolves #3389 Signed-off-by: Ilayaperumal Gopinathan --- .../java/org/springframework/ai/tool/ToolCallback.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spring-ai-model/src/main/java/org/springframework/ai/tool/ToolCallback.java b/spring-ai-model/src/main/java/org/springframework/ai/tool/ToolCallback.java index e22037ca90b..73a672b49e1 100644 --- a/spring-ai-model/src/main/java/org/springframework/ai/tool/ToolCallback.java +++ b/spring-ai-model/src/main/java/org/springframework/ai/tool/ToolCallback.java @@ -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; @@ -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. */ @@ -53,7 +58,9 @@ 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.info("By default the tool context is not used, " + + "override the method 'call(String toolInput, ToolContext toolcontext)' to support the use of tool context." + + "Review the ToolCallback implementation for {}", getToolDefinition().name()); } return call(toolInput); }