spring-projects / spring-projects/spring-ai
ToolCallAdvisor causes infinite loop when a tool returns long descriptive text on invalid input (empty string parameter)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
When a tool is called with an empty string for a required parameter (e.g., inputData = ""), and the tool implementation returns a long descriptive “task prompt” instead of a concise error message, the ToolCallAdvisor enters an infinite loop. The model keeps calling the same tool with the same empty argument, consuming excessive tokens and never terminating.
Expected behavior
The ToolCallAdvisor should detect repeated identical tool calls (same name + same arguments) and break the loop after a configurable number of attempts, or at least allow the model to receive a clear error that stops the cycle.
Actual behavior
The advisor continues indefinitely, because the tool’s output (a long text that looks like a valid response) is interpreted as a normal tool result, and the model decides to call the same tool again with the same empty input.
Steps to Reproduce
- Define a tool that requires a non‑empty
inputDataparameter:@Tool(description = "Analyzes data") public String analyze(@ToolParam(required = true) String inputData) { if (inputData == null || inputData.trim().isEmpty()) { // ❌ Returning a long descriptive text return "## Role\nYou are an expert...\n## Task\nPlease provide data..."; } return "analysis result"; } - Configure
ChatClientwithToolCallAdvisor:var advisor = ToolCallAdvisor.builder() .toolCallingManager(toolCallingManager) .build(); var chatClient = ChatClient.builder(chatModel) .defaultAdvisors(advisor) .defaultTools(analyzeTool) .build(); - Ask a question that leads the model to call the tool with an empty
inputData(e.g., the model hallucinates the required data). - Observe infinite tool‑call loop in logs.
Environment
- Spring AI version:
2.0.0-M2 - Java version:
17 - LLM provider: Doubao / OpenAI (any)
- Spring Boot version:
3.2.x
Logs (excerpt)
2026-04-03 17:34:52.476 ... "tool_calls":[{"function":{"arguments":" {\"inputData\": \"\", ...", "name":"audioChronicleInsight"}}]
2026-04-03 17:34:52.600 ... Successful execution of tool: audioChronicleInsight
... (repeats many times with same empty inputData)
Possible workarounds
- Validate input in the tool and throw an exception or return a short, clear error message like
"ERROR: inputData cannot be empty. Please call searchAudioRecordings first." - Manually implement a loop counter in a custom
CallAdvisor.
Suggested improvements
- Provide a built‑in mechanism in
ToolCallAdvisorto limit the number of identical consecutive tool calls (same name + same arguments). - Add a configuration option like
.maxIdenticalToolCalls(int)(default 2 or 3). - Allow users to specify a
ToolCallValidatorthat can reject a tool call before execution.
Labels
type: bug
area: tool-calling
priority: high
status: triage-needed
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading ToolCallAdvisor and its builder, then trace how repeated tool calls are processed through the toolCallingManager. Define completion as detecting identical tool name and arguments, stopping after a configurable limit, and returning a clear error; verify the reproduced empty-input loop terminates.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100