Current Behavior
As of Spring AI 2.0.0, ToolContext cannot contain null values or be empty.
A null tool context map value fails with an IllegalArgumentException in ChatClientRequestSpec.toolContext.toolContext(...).
Empty tool context fails during tool execution with an IllegalArgumentException in MethodToolCallback.validateToolContextSupport(...).
So when I have to handle optional data (an "orderNumber" in the next example), I have to add some fake data ("bar" under the "foo" key) to prevent an exception :
// Build the context passed to the tools
Map<String, Object> toolContext = new HashMap<>();
if (orderNumber != null) {
toolContext.put("orderNumber", orderNumber);
} else {
toolContext.put("foo", "bar"); // Workaround (tool call will fail if the tool context is empty)
}
// Execute the request
return chatClient.prompt(request).toolContext(toolContext).call().content();
Suggested changes
What are the reasons of these limitations ?
To simplify application code, could you support empty tool context (or else, as a second choice, null values) ?
Current Behavior
As of Spring AI 2.0.0, ToolContext cannot contain null values or be empty.
A null tool context map value fails with an
IllegalArgumentExceptioninChatClientRequestSpec.toolContext.toolContext(...).Empty tool context fails during tool execution with an
IllegalArgumentExceptioninMethodToolCallback.validateToolContextSupport(...).So when I have to handle optional data (an "orderNumber" in the next example), I have to add some fake data ("bar" under the "foo" key) to prevent an exception :
Suggested changes
What are the reasons of these limitations ?
To simplify application code, could you support empty tool context (or else, as a second choice, null values) ?