spring-projects / spring-projects/spring-ai

Allow Tool Calls that use returnDirect to return the result object without serialisation

Open
#3,191 3 comments 0 reactions 1 assignee View on GitHub

@tzolov is already working on this.

Since May 16, 2025.

tool calling
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Currently, when doing tool calls, the response is a converted to JSON so it can be sent back to the model to process the result. This works for the normal tool calling flow, but is overly restrictive when using the return-direct functionality.

In some of my tool calls, I use returnDirect and create complex objects that are not easily serialisable. It would be great if we could return the actual object when "direct returning" the result of a tool call.

Expected Behavior

When performing a tool call with return direct, the result object is stored within the ToolResponse item contained within ToolResponseMessage.

e.g.

public record ToolResponse(
  String id, String name, String responseData, @Nullable Object responseObj
) {}

Would be happy to just use an Object and let the user cast as needed, or some generic way to implement this.

Current Behavior

Tool calls that return-direct must return an object that can be serialised to JSON, even if not practical.

Context

I've hit this within a chatbot I'm building that needs to integrate with a third-party. It's possible to create intermediate objects to delay creating the un-serialisable objects until I have control of the code-flow, but it's a lot of ceremony.

I've taken to using AtomicRefs within ToolContext in the meantime, but obviously it's not ideal!

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.