Skip to content

Conversation

@hannesrudolph
Copy link
Collaborator

@hannesrudolph hannesrudolph commented Dec 10, 2025

Summary

Implements proper interleaved thinking support for DeepSeek V3's thinking mode (deepseek-reasoner model) following the DeepSeek API documentation at https://api-docs.deepseek.com/guides/thinking_mode

Changes

  • Add thinking: { type: "enabled" } parameter for deepseek-reasoner model
  • Handle streaming reasoning_content from DeepSeek API that yields reasoning chunks
  • Add tool call conversion (Anthropic tool_use → OpenAI tool_calls format) for thinking mode
  • Add tool result conversion (Anthropic tool_result → OpenAI tool messages)
  • Extract reasoning from content blocks for API continuations (Task stores reasoning as content blocks)
  • Add getReasoningContent() method to accumulate and expose reasoning content
  • Add comprehensive tests for interleaved thinking mode (6 new tests in deepseek.spec.ts)
  • Add tests for tool call support (7 new tests in r1-format.spec.ts)

How It Works

Per DeepSeek's API documentation:

  1. Thinking mode enabled via thinking: { type: "enabled" } for deepseek-reasoner model
  2. During streaming: reasoning_content is yielded as reasoning chunks and accumulated
  3. Tool call flow: The model can think → call tools → receive results → continue thinking
  4. Within a turn: reasoning_content is preserved and passed back to the API for continuation
  5. Between turns: Reasoning content is cleared (new user question starts fresh)

Important

Implements interleaved thinking mode for DeepSeek reasoner model, handling reasoning content and tool calls, with comprehensive tests added.

  • Behavior:
    • Implements interleaved thinking mode for deepseek-reasoner model with thinking: { type: "enabled" } parameter.
    • Handles streaming reasoning_content and tool calls in DeepSeekHandler.
    • Resets reasoning content between turns and accumulates it within a turn.
  • Functions:
    • Adds getReasoningContent() in DeepSeekHandler to expose accumulated reasoning content.
    • Updates convertToR1Format() to handle reasoning_content and tool calls.
  • Tests:
    • Adds 6 tests for interleaved thinking mode in deepseek.spec.ts.
    • Adds 7 tests for tool call support in r1-format.spec.ts.

This description was created by Ellipsis for f03a5b6. You can customize this summary. It will automatically update as commits are pushed.

@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. Enhancement New feature or request labels Dec 10, 2025
@roomote
Copy link
Contributor

roomote bot commented Dec 10, 2025

Oroocle Clock   Follow along on Roo Cloud

Re-review complete. No new issues were identified for the latest commit.

  • No review issues identified in this pass
Previous reviews \n> - 09cdadf: [Review #1](https://app.roocode.com/cloud-jobs/tEr8Cpoh?utm_source=cloud-job-url&utm_medium=link&utm_campaign=previous-reviews) - f03a5b6: [Review #2](https://app.roocode.com/cloud-jobs/PjSjBCct?utm_source=cloud-job-url&utm_medium=link&utm_campaign=previous-reviews) - 08a03fe: [Review #3](https://app.roocode.com/cloud-jobs/nQZ63af4?utm_source=cloud-job-url&utm_medium=link&utm_campaign=previous-reviews) - cdad3e7: [Review #4](https://app.roocode.com/cloud-jobs/CEgPN06m?utm_source=cloud-job-url&utm_medium=link&utm_campaign=previous-reviews)

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 10, 2025
@bozoweed
Copy link

so needed too for kimi-k2-thinking

here is some more details on that tech

# Your tool implementation
def get_weather(city: str) -> dict:
    return {"weather": "Sunny"}

# Tool schema definition
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Retrieve current weather information. Call this when the user asks about the weather.",
        "parameters": {
            "type": "object",
            "required": ["city"],
            "properties": {
                "city": {
                    "type": "string",
                    "description": "Name of the city"
                }
            }
        }
    }
}]

# Map tool names to their implementations
tool_map = {
    "get_weather": get_weather
}

def tool_call_with_client(client: OpenAI, model_name: str):
    messages = [
        {"role": "system", "content": "You are Kimi, an AI assistant created by Moonshot AI."},
        {"role": "user", "content": "What's the weather like in Beijing today? Use the tool to check."}
    ]
    finish_reason = None
    while finish_reason is None or finish_reason == "tool_calls":
        completion = client.chat.completions.create(
            model=model_name,
            messages=messages,
            temperature=1.0,
            tools=tools,          # tool list defined above
            tool_choice="auto"
        )
        choice = completion.choices[0]
        finish_reason = choice.finish_reason
        if finish_reason == "tool_calls":
            messages.append(choice.message)
            for tool_call in choice.message.tool_calls:
                tool_call_name = tool_call.function.name
                tool_call_arguments = json.loads(tool_call.function.arguments)
                tool_function = tool_map[tool_call_name]
                tool_result = tool_function(**tool_call_arguments)
                print("tool_result:", tool_result)
                
                messages.append({
                    "role": "tool",
                    "tool_call_id": tool_call.id,
                    "name": tool_call_name,
                    "content": json.dumps(tool_result)
                })
    print("-" * 100)
    print(choice.message.content)

@hannesrudolph hannesrudolph marked this pull request as draft December 11, 2025 19:05
@hannesrudolph hannesrudolph moved this from Triage to PR [Draft / In Progress] in Roo Code Roadmap Dec 12, 2025
@hannesrudolph hannesrudolph added PR - Draft / In Progress and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Dec 12, 2025
…oner

- Add thinking parameter support for deepseek-reasoner model
- Handle streaming reasoning_content from DeepSeek API
- Add tool call conversion (tool_use -> tool_calls) for thinking mode
- Add tool result conversion (tool_result -> tool messages)
- Extract reasoning from content blocks for API continuations
- Add getReasoningContent() method for accumulated reasoning
- Add comprehensive tests for interleaved thinking mode
Enable reasoning_content to be passed back to DeepSeek API during tool
call continuations within the same turn. This is required for DeepSeek's
interleaved thinking mode to work correctly with native tool calls.

See: https://api-docs.deepseek.com/guides/thinking_mode

- Add preserveReasoning: true to deepseek-reasoner model
- Add tests verifying preserveReasoning flag
…g merge

- Change OpenAiHandler.client from private to protected to allow
  DeepSeekHandler to access it properly without @ts-ignore
- Remove unused getClient() method from DeepSeekHandler
- Fix r1-format.ts to use finalReasoning (includes extracted reasoning
  from content blocks) instead of reasoningContent (top-level only)
  when merging assistant messages
@hannesrudolph hannesrudolph force-pushed the feat/deepseek-interleaved-thinking branch from 09cdadf to 200b1b4 Compare December 13, 2025 00:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement New feature or request PR - Draft / In Progress size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

Status: PR [Draft / In Progress]

Development

Successfully merging this pull request may close these issues.

3 participants