Skip to content

fix(agent): support returnDirect in default ReactAgent#4481

Open
gaozexun wants to merge 9 commits intoalibaba:mainfrom
gaozexun:codex/issue-4462-return-direct-submit
Open

fix(agent): support returnDirect in default ReactAgent#4481
gaozexun wants to merge 9 commits intoalibaba:mainfrom
gaozexun:codex/issue-4462-return-direct-submit

Conversation

@gaozexun
Copy link
Copy Markdown
Contributor

Describe what this PR does / why we need it
This PR fixes issue #4462 in the default ReactAgent execution flow.

AgentToolNode already writes the returnDirect completion signal into ToolResponseMessage.metadata, but the default ReactAgent does not consume that signal. As a result, after a returnDirect=true tool finishes, the agent may still route back to the model for an unnecessary second model call instead of returning the tool result directly.

This PR fixes that gap in ReactAgent while keeping ReturnDirectModelHook unchanged.

Does this pull request fix one issue?
Yes. Fixes #4462.

Describe how you did it
The patch is intentionally kept small and limited to the default ReactAgent path.

In ReactAgent.makeToolsToModelEdge(...), check whether the last ToolResponseMessage is marked as returnDirect.
If it is returnDirect, route directly to END instead of continuing the ReAct loop.
In ReactAgent.extractAssistantMessage(...), if the last message is a returnDirect ToolResponseMessage, convert it into the final AssistantMessage.
Introduce a small shared helper, ReturnDirectMessageSupport, to centralize:
detecting returnDirect from tool-response metadata
converting a ToolResponseMessage into the final assistant response
ReturnDirectModelHook is intentionally left unchanged, because explicit hook-based behavior already works as expected.

Describe how to verify it
Set a breakpoint at buildChatClientRequestSpec. Before the fix, it is hit twice, which means the LLM is called twice. After the fix, it is hit only once.

@gaozexun gaozexun changed the title Codex/issue 4462 return direct submit fix(agent-framework): support returnDirect in default ReactAgent Mar 29, 2026
@gaozexun gaozexun changed the title fix(agent-framework): support returnDirect in default ReactAgent fix(agent): support returnDirect in default ReactAgent Mar 29, 2026
@yuluo-yx yuluo-yx requested a review from Copilot March 31, 2026 15:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the default ReactAgent ReAct loop so that tools annotated with returnDirect=true can terminate execution immediately (skipping the extra, unnecessary second LLM call), matching the intended Spring AI returnDirect semantics.

Changes:

  • Detect returnDirect on the last ToolResponseMessage and route tools -> END instead of continuing to the model.
  • When a returnDirect ToolResponseMessage is the last message, convert it into the final AssistantMessage.
  • Introduce ReturnDirectMessageSupport helper for returnDirect detection and tool-response-to-assistant conversion.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java Adds default-agent handling for returnDirect to end the graph early and return the tool output as the final assistant message.
spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/hook/returndirect/ReturnDirectMessageSupport.java New shared helper for detecting returnDirect metadata and turning a ToolResponseMessage into an AssistantMessage.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gaozexun
Copy link
Copy Markdown
Contributor Author

gaozexun commented Apr 7, 2026

@yuluo-yx 我已经解决了 code review 评论,辛苦审核下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] spring ai alibba 中@Tool的参数returnDirect失效true或者false没有区别,但是spring ai中生效

2 participants