Skip to content

Support empty ToolContext, or null values #6545

Description

@fbeaufume

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) ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions