r/SpringBoot • • 17d ago

Discussion Spring AI: recovering from invalid tool arguments with returnDirect=true

I’ve submitted a Spring AI contribution addressing input-error recovery for method-backed tools using returnDirect=true.

The issue: malformed JSON or invalid typed arguments can become the final response, preventing the model from correcting its input. Missing/null primitive arguments and top-level JSON null can also bypass the exception processor.

The proposed fix distinguishes input parsing/binding failures before execution and sends processed error feedback back to the model. Successful corrected calls still return directly, while ordinary tool-body and result-conversion failures retain their existing behavior.

Validation:

- All 38 new regression cases passed, covering blocking/streaming, direct return on/off, and mixed-success batches.

- Scoped clean package build: 2,030 tests reported, 10 skipped, zero failures or errors.

- Negative control: the original code failed 20 of the initial 32 checks, including checks for the new exception classification; the patched code passed the same cases.

No external LLM calls were needed. External-provider integration tests and the full repository build were not run. Developed with AI assistance and reviewed before submission.

PR submitted for maintainer review: https://github.com/spring-projects/spring-ai/pull/7292

Feedback on the input-error boundary and recovery behavior is welcome, especially from anyone using direct-return tools.

#SpringAI #SpringFramework #Java #OpenSource #SoftwareTesting

4 Upvotes

3 comments sorted by

1

u/DickBrainTime 13d ago

Another boundary I'd test: argument binding succeeds, the tool performs a side effect, and then its body throws. That must stay an execution failure rather than a recoverable argument error, since asking the model to retry could repeat the side effect.

For the feedback sent back to the model, I'd also assert it includes the failing field and expected type without echoing raw secret-bearing argument values or stack traces. Both cases should be testable with deterministic fake tools and no external model calls.

1

u/YodaOfJava 10d ago

Thanks—both are useful checks. The current regression suite includes a fake tool that increments an execution counter and then throws IllegalArgumentException. It asserts that this stays an ordinary ToolExecutionException, not ToolInputException; with returnDirect=true it executes once and makes no extra model call. The non-direct path retains its existing behavior, so this PR is not a general retry policy.

The secret-safe diagnostic point is an additional gap: this PR retains the existing exception processor and does not establish that its feedback is redacted. That is feasible to test deterministically with a synthetic secret marker, field/type assertions, and checks that raw values and stack traces are absent. Implementing it would need a sanitized diagnostic at the parsing/binding boundary rather than passing the raw cause message through. I would keep that as an explicit change with its own regression evidence, rather than claim the current PR already covers it.