Skip to content

Conversation

kuntal1461
Copy link
Contributor

Summary
Fixes #4617 — When DeepSeekChatModel is forced to use a tool_choice with returnDirect=false,
the model repeatedly re-enters tool-call, causing an infinite loop.
This change resets tool_choice to auto after tool execution so the model can produce normal assistant text.

Changes

  • Reset tool_choice to auto after tool execution in both internalCall and internalStream.
  • Add DeepSeekChatModelToolChoiceResetTests to verify forced tool_choice behavior.

How to Reproduce

  1. Define a tool with returnDirect=false
  2. Force tool_choice to that tool
  3. Observe repeated tool invocation (without this fix)

Expected Behavior
Tool is invoked once; model returns normal text.

Environment

  • Observed on Spring AI 1.0.3
  • Fix applied against main (1.1.0-SNAPSHOT)

Tests
mvn -pl models/spring-ai-deepseek -Dtest=org.springframework.ai.deepseek.DeepSeekChatModelToolChoiceResetTests test

DCO Sign-off
Signed-off-by: Kuntal Maity kuntal.1461@gmail.com

* backed by {@link DeepSeekApi}.
*
* @author Geng Rong
* @last Updated By : @kuntal1461
Copy link
Contributor

Choose a reason for hiding this comment

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

keep same style? @author Geng Rong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jiafu1115 It is fixed . Please review once

@kuntal1461 kuntal1461 requested a review from jiafu1115 October 20, 2025 11:26
.build());
}
else {
// Reset tool choice to AUTO to prevent forcing repeated tool calls.
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicated code with 223. you can extract these duplicated line to seperate method.

…en returnDirect=false (spring-projects#4617)

Signed-off-by: Kuntal Maity <kuntal.1461@gmail.com>
…ed tool_choice reset validation (spring-projects#4617)

Signed-off-by: Kuntal Maity <kuntal.1461@gmail.com>
…en returnDirect=false (spring-projects#4617)

Signed-off-by: Kuntal Maity <kuntal.1461@gmail.com>
@kuntal1461 kuntal1461 force-pushed the fix/deepseekchatmodel-toolchoice-loop-4617 branch from c312b9c to d05c8dd Compare October 20, 2025 14:30
Signed-off-by: Kuntal Maity <kuntal.1461@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeepSeekChatModel forces tool_choice to be specified to trigger loop tool call

3 participants