Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Dec 18, 2025

Related GitHub Issue

Closes: #10171

Description

This PR attempts to address Issue #10171. Feedback and guidance are welcome.

When switching from another provider (e.g., OpenAI) to DeepSeek reasoner mid-conversation, the DeepSeek API returns a 400 error: "Missing reasoning_content field in the assistant message at message index N."

This happens because:

  1. Messages from other providers in the conversation history do not have a reasoning_content field
  2. The DeepSeek API in thinking mode requires ALL assistant messages to have a reasoning_content field (even if empty)
  3. The current convertToR1Format() function preserves existing reasoning_content but does not add it to messages that lack it

Solution:

  • Added a new addEmptyReasoning option to convertToR1Format() in src/api/transform/r1-format.ts
  • When this option is true, adds reasoning_content: "" to all assistant messages that do not already have reasoning content
  • In src/api/providers/deepseek.ts, this option is enabled for thinking models (deepseek-reasoner)

Test Procedure

  • Added comprehensive unit tests covering:
    • Adding empty reasoning_content when addEmptyReasoning is true
    • Preserving existing reasoning_content (not replacing with empty)
    • Array content assistant messages
    • Assistant messages with tool_calls
    • Provider switch scenario simulation
    • Combined mergeToolResultText and addEmptyReasoning options
    • Merged consecutive assistant messages

Run tests:

cd src && npx vitest run api/transform/__tests__/r1-format.spec.ts
cd src && npx vitest run api/providers/__tests__/deepseek.spec.ts

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Documentation Updates

  • No documentation updates are required.

Additional Notes

This fix ensures backward compatibility since the addEmptyReasoning option defaults to false, and only affects DeepSeek reasoner models that require this field when switching providers mid-conversation.

When switching from another provider to DeepSeek reasoner mid-conversation,
the DeepSeek API requires all assistant messages to have a reasoning_content
field (even if empty). Previously, messages from other providers that lacked
this field would cause a 400 error.

This fix adds a new addEmptyReasoning option to convertToR1Format() that
adds an empty reasoning_content string to assistant messages that do not
already have reasoning content. The option is enabled for DeepSeek thinking
models (deepseek-reasoner).

Closes #10171
@roomote
Copy link
Contributor Author

roomote bot commented Dec 18, 2025

Rooviewer Clock   See task on Roo Cloud

Review complete. No issues found.

The implementation correctly addresses the DeepSeek API error when switching providers mid-conversation by adding empty reasoning_content to assistant messages that lack it. The code is well-documented, backward compatible, and has comprehensive test coverage (8 new test cases, all 28 tests passing).

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

[BUG] Switching deepseek providers during a conversation will result in an error

3 participants