spring-projects / spring-projects/spring-ai

Support empty ToolContext, or null values

Open
#6,545 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ChatClientRequestSpec.toolContext.toolContext(...) and MethodToolCallback.validateToolContextSupport(...), the two validation points identified in the report. Reproduce the null-value and empty-context cases, then inspect related tests before deciding the supported behavior. Done means optional tool context no longer requires fake data and the behavior is covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.