Skip to content

Commit b0a97c7

Browse files
committed
MethodToolCallback ToolExecutionException
- Handle the entire exception type when throwing ToolExecutionException Signed-off-by: Ilayaperumal Gopinathan <ilayaperumal.gopinathan@broadcom.com>
1 parent d4f22ce commit b0a97c7

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

spring-ai-model/src/main/java/org/springframework/ai/tool/method/MethodToolCallback.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,10 @@ private Map<String, Object> extractToolArguments(String toolInput) {
132132
return JsonParser.fromJson(toolInput, new TypeReference<>() {
133133
});
134134
}
135-
catch (IllegalStateException ex) {
136-
if (ex.getCause() instanceof JsonProcessingException jsonExp) {
137-
logger.warn("Conversion from JSON failed", ex);
138-
throw new ToolExecutionException(this.getToolDefinition(), jsonExp);
139-
}
140-
throw ex;
135+
catch (Exception ex) {
136+
logger.warn("Conversion from JSON failed", ex);
137+
Throwable cause = (ex.getCause() instanceof JsonProcessingException) ? ex.getCause() : ex;
138+
throw new ToolExecutionException(this.getToolDefinition(), cause);
141139
}
142140
}
143141

@@ -168,12 +166,10 @@ private Object buildTypedArgument(@Nullable Object value, Type type) {
168166
String json = JsonParser.toJson(value);
169167
return JsonParser.fromJson(json, type);
170168
}
171-
catch (IllegalStateException ex) {
172-
if (ex.getCause() instanceof JsonProcessingException jsonExp) {
173-
logger.warn("Conversion from JSON failed", ex);
174-
throw new ToolExecutionException(this.getToolDefinition(), jsonExp);
175-
}
176-
throw ex;
169+
catch (Exception ex) {
170+
logger.warn("Conversion from JSON failed", ex);
171+
Throwable cause = (ex.getCause() instanceof JsonProcessingException) ? ex.getCause() : ex;
172+
throw new ToolExecutionException(this.getToolDefinition(), cause);
177173
}
178174
}
179175

spring-ai-model/src/test/java/org/springframework/ai/tool/method/MethodToolCallbackExceptionHandlingTest.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/*
2-
* Copyright 2025 - 2025 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
1617
package org.springframework.ai.tool.method;
1718

1819
import java.util.List;

0 commit comments

Comments
 (0)