-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
fix: 修复claudecode使用deepseek-v4系列, 上游响应tool_use前没有thinking内容,导致后续对话报错 #4597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chi-cat
wants to merge
1
commit into
QuantumNous:main
Choose a base branch
from
chi-cat:fix_deepseek_thinking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package deepseek | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/QuantumNous/new-api/dto" | ||
| ) | ||
|
|
||
| // EnsureThinkingBeforeToolUse ensures that when thinking mode is enabled, | ||
| // every tool_use block in assistant messages has a preceding thinking block. | ||
| // DeepSeek V4 requires thinking blocks before tool_use when thinking is enabled. | ||
| func EnsureThinkingBeforeToolUse(req *dto.ClaudeRequest) { | ||
| if req == nil || len(req.Messages) == 0 { | ||
| return | ||
| } | ||
| // Only process deepseek-v4-* models | ||
| if !isDeepSeekV4(req.Model) { | ||
| return | ||
| } | ||
| // Thinking disabled? skip | ||
| if req.Thinking != nil && req.Thinking.Type == "disabled" { | ||
| return | ||
| } | ||
| for i := range req.Messages { | ||
| msg := &req.Messages[i] | ||
| if msg.Role != "assistant" { | ||
| continue | ||
| } | ||
| contentList, ok := msg.Content.([]any) | ||
| if !ok || len(contentList) == 0 { | ||
| continue | ||
| } | ||
| contentList, fixed := ensureThinkingInContentArray(contentList) | ||
| if fixed { | ||
| msg.Content = contentList | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // isDeepSeekV4 checks if the model name belongs to DeepSeek V4 series. | ||
| func isDeepSeekV4(modelName string) bool { | ||
| return strings.HasPrefix(modelName, "deepseek-v4-") | ||
| } | ||
|
|
||
| // ensureThinkingInContentArray inserts an empty thinking block at position 0 | ||
| // if the content array contains any tool_use without a thinking block as the first element. | ||
| // Returns the modified slice and whether modifications were made. | ||
| func ensureThinkingInContentArray(contentList []any) ([]any, bool) { | ||
| if len(contentList) == 0 { | ||
| return contentList, false | ||
| } | ||
| // Check if the first element is already a thinking block | ||
| if first, ok := contentList[0].(map[string]any); ok && first["type"] == "thinking" { | ||
| return contentList, false | ||
| } | ||
| // Check if there are any tool_use blocks | ||
| hasToolUse := false | ||
| for _, item := range contentList { | ||
| if m, ok := item.(map[string]any); ok && m["type"] == "tool_use" { | ||
| hasToolUse = true | ||
| break | ||
| } | ||
| } | ||
| if !hasToolUse { | ||
| return contentList, false | ||
| } | ||
| // Insert empty thinking block at the beginning | ||
| emptyStr := "" | ||
| thinkingBlock := map[string]any{ | ||
| "type": "thinking", | ||
| "thinking": emptyStr, | ||
| } | ||
| contentList = append(contentList, nil) | ||
| copy(contentList[1:], contentList[0:]) | ||
| contentList[0] = thinkingBlock | ||
| return contentList, true | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isDeepSeekV4and Ali'ssupportsAliAnthropicMessagesuse inconsistent matching strategies.isDeepSeekV4requiresHasPrefix("deepseek-v4-")(trailing dash), butsupportsAliAnthropicMessagesusesContains("deepseek-v4")(no dash). For all current model names (deepseek-v4-flash,deepseek-v4-pro) this is harmless — both checks pass. However, a model configured with the bare alias"deepseek-v4"would be routed through Ali's Anthropic messages path butEnsureThinkingBeforeToolUsewould silently no-op, leaving the thinking-before-tool-use gap unfixed.Consider aligning the two checks:
🛠 Proposed fix
func isDeepSeekV4(modelName string) bool { - return strings.HasPrefix(modelName, "deepseek-v4-") + return strings.HasPrefix(modelName, "deepseek-v4-") || modelName == "deepseek-v4" }📝 Committable suggestion
🤖 Prompt for AI Agents