Interface ToolArgumentsErrorHandler
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
ToolArgumentsExceptions thrown by a ToolExecutor.
There are two ways to handle errors:
1. Return a text message that will be sent back
to the LLM, allowing it to respond appropriately (for example, by correcting the error and retrying).
2. Throw an exception: this will stop the AI service flow.
Use ToolErrorContext.rawError() to access the raw error before cause-unwrapping
when deciding whether to throw.
- Since:
- 1.4.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ToolArgumentsErrorHandlerReturns a handler that rethrows the error, failing the AI Service invocation.handle(Throwable error, ToolErrorContext context) Handles an error that occurred during the parsing and preparation of tool arguments.static ToolArgumentsErrorHandlerReturns a handler that sends the message of the error to the LLM as the result of the tool execution, so that the LLM can correct the arguments and call the tool again.
-
Method Details
-
handle
Handles an error that occurred during the parsing and preparation of tool arguments.This method should either throw an exception or return a
ToolErrorHandlerResult.text(String), which will be sent to the LLM as the result of the tool execution.- Parameters:
error- The actual error that occurred (cause-unwrapped). UseToolErrorContext.rawError()for the error before unwrapping.context- The context in which the error occurred.- Returns:
- The result of error handling.
-
failInvocation
Returns a handler that rethrows the error, failing the AI Service invocation. Nothing about the error is sent to the LLM.- Since:
- 1.21.0
-
sendExceptionMessageToLlm
Returns a handler that sends the message of the error to the LLM as the result of the tool execution, so that the LLM can correct the arguments and call the tool again. The AI Service invocation continues.Argument errors usually originate from the LLM (malformed JSON, a missing field, a wrong type), and LLMs can typically fix them once they see what went wrong.
WARNING: this option can expose sensitive data. Most argument errors are produced by LangChain4j and describe the arguments the LLM itself generated, but an error can also come from your own code (for example, from a custom deserializer or a validation check inside a tool parameter type). Such a message reaches the LLM provider, is stored in the chat memory and can end up in the answer the user reads.
- Since:
- 1.21.0
-