Skip to content

Commit e13b511

Browse files
authored
Merge pull request #1904 from ejstuart/fix-exception-in-tool
Fix exception rethrow in tool executor
2 parents 0d4a824 + 53b3c66 commit e13b511

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

core/deployment/src/test/java/io/quarkiverse/langchain4j/test/ToolExecutorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private ToolExecutor getToolExecutor(String methodName) {
292292
toolExecutor = new QuarkusToolExecutor(
293293
new QuarkusToolExecutor.Context(testTool, invokerClassName, methodCreateInfo.methodName(),
294294
methodCreateInfo.argumentMapperClassName(), methodCreateInfo.executionModel(),
295-
methodCreateInfo.returnBehavior()));
295+
methodCreateInfo.returnBehavior(), false));
296296
break;
297297
}
298298
}

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/ToolsRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void populateToolMetadata(Collection<Object> objectsWithTools, Lis
5353
QuarkusToolExecutor.Context executorContext = new QuarkusToolExecutor.Context(objectWithTool,
5454
invokerClassName, methodCreateInfo.methodName(),
5555
methodCreateInfo.argumentMapperClassName(), methodCreateInfo.executionModel(),
56-
methodCreateInfo.returnBehavior());
56+
methodCreateInfo.returnBehavior(), false);
5757
toolExecutors.put(toolSpecification.name(), toolExecutorFactory.create(executorContext));
5858
}
5959
}

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/devui/ChatJsonRPCService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public ChatJsonRPCService(@All List<ChatModel> models, // don't use ChatModel mo
117117
QuarkusToolExecutor.Context executorContext = new QuarkusToolExecutor.Context(objectWithTool,
118118
methodCreateInfo.invokerClassName(), methodCreateInfo.methodName(),
119119
methodCreateInfo.argumentMapperClassName(), methodCreateInfo.executionModel(),
120-
methodCreateInfo.returnBehavior());
120+
methodCreateInfo.returnBehavior(), false);
121121
toolExecutors.put(methodCreateInfo.toolSpecification().name(),
122122
toolExecutorFactory.create(executorContext));
123123
toolSpecifications.add(methodCreateInfo.toolSpecification());

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/tool/QuarkusToolExecutor.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import dev.langchain4j.agent.tool.ReturnBehavior;
1515
import dev.langchain4j.agent.tool.ToolExecutionRequest;
16+
import dev.langchain4j.exception.ToolExecutionException;
1617
import dev.langchain4j.internal.Json;
1718
import dev.langchain4j.invocation.InvocationContext;
1819
import dev.langchain4j.service.tool.ToolExecutionResult;
@@ -29,7 +30,8 @@ public class QuarkusToolExecutor implements ToolExecutor {
2930
private final Context context;
3031

3132
public record Context(Object tool, String toolInvokerName, String methodName, String argumentMapperClassName,
32-
ToolMethodCreateInfo.ExecutionModel executionModel, ReturnBehavior returnBehavior) {
33+
ToolMethodCreateInfo.ExecutionModel executionModel, ReturnBehavior returnBehavior,
34+
boolean propagateToolExecutionExceptions) {
3335
}
3436

3537
public interface Wrapper {
@@ -107,7 +109,7 @@ private ToolExecutionResult invoke(Object[] params, ToolInvoker invokerInstance)
107109
String result;
108110
if (invocationResult instanceof Uni<?>) { // TODO CS
109111
if (io.vertx.core.Context.isOnEventLoopThread()) {
110-
throw new IllegalStateException(
112+
throw new ToolExecutionException(
111113
"Cannot execute tools returning Uni on event loop thread due to a tool executor limitation");
112114
}
113115
result = handleResult(invokerInstance, ((Uni<?>) invocationResult).await().indefinitely());
@@ -116,12 +118,11 @@ private ToolExecutionResult invoke(Object[] params, ToolInvoker invokerInstance)
116118
}
117119
log.debugv("Tool execution result: {0}", result);
118120
return ToolExecutionResult.builder().result(invocationResult).resultText(result).build();
121+
} catch (ToolExecutionException e) {
122+
throw e;
119123
} catch (Exception e) {
120-
if (e instanceof IllegalArgumentException) {
121-
throw (IllegalArgumentException) e;
122-
}
123-
if (e instanceof IllegalStateException) {
124-
throw (IllegalStateException) e;
124+
if (context.propagateToolExecutionExceptions) {
125+
throw new ToolExecutionException(e);
125126
}
126127
log.error("Error while executing tool '" + context.tool.getClass() + "'", e);
127128
return ToolExecutionResult.builder().isError(true).resultText(e.getMessage()).build();

0 commit comments

Comments
 (0)