Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

Preserve empty reasoning state across native tool turns (#1339)

Ali Khokhare0e45a6a6a68
diff --git a/pyproject.toml b/pyproject.toml
index f04584f7..a5cbeae1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "free-claude-code"
-version = "4.16.6"
+version = "4.16.7"
description = "Local proxy connecting coding agents to OpenAI-compatible AI providers"
readme = "README.md"
requires-python = ">=3.14.0"
diff --git a/src/free_claude_code/core/anthropic/conversion.py b/src/free_claude_code/core/anthropic/conversion.py
index 75a5adb1..7d72ee5e 100644
--- a/src/free_claude_code/core/anthropic/conversion.py
+++ b/src/free_claude_code/core/anthropic/conversion.py
@@ -574,6 +574,8 @@ class AnthropicToOpenAIConverter:
reasoning_replay=reasoning_replay,
)[0]
pre_msg["tool_calls"] = tool_calls
+ if reasoning_replay is ReasoningReplayMode.REASONING_CONTENT:
+ pre_msg.setdefault("reasoning_content", "")
if tool_calls and pre_msg.get("content") == " ":
pre_msg["content"] = ""
return _ToolTurnSegment(
diff --git a/tests/providers/support.py b/tests/providers/support.py
index 62926006..aa63ad6e 100644
--- a/tests/providers/support.py
+++ b/tests/providers/support.py
@@ -1,5 +1,10 @@
"""Provider test helpers with explicit admission ownership."""
+import json
+
+import httpx
+from openai import AsyncOpenAI
+
from free_claude_code.application.reasoning import client_reasoning_policy
from free_claude_code.core.anthropic.models import MessagesRequest
from free_claude_code.core.reasoning import ReasoningPolicy
@@ -15,6 +20,36 @@ REASONING_ON = ReasoningPolicy.on()
REASONING_OFF = ReasoningPolicy.off()
+async def capture_openai_chat_wire_body(body: dict) -> dict:
+ """Return the JSON body serialized by the OpenAI chat client."""
+ captured: list[dict] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ payload = json.loads(request.content)
+ assert isinstance(payload, dict)
+ captured.append(payload)
+ return httpx.Response(
+ 200,
+ headers={"content-type": "text/event-stream"},
+ text="data: [DONE]\n\n",
+ )
+
+ client = AsyncOpenAI(
+ api_key="test",
+ base_url="https://provider.invalid/v1",
+ http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
+ max_retries=0,
+ )
+ try:
+ stream = await client.chat.completions.create(**body, stream=True)
+ await stream.close()
+ finally:
+ await client.close()
+
+ assert len(captured) == 1
+ return captured[0]
+
+
def immediate_admission(
*,
provider_name: str = "TEST",
diff --git a/tests/providers/test_converter.py b/tests/providers/test_converter.py
index 61141515..e8ec9c80 100644
--- a/tests/providers/test_converter.py
+++ b/tests/providers/test_converter.py
@@ -641,6 +641,99 @@ def test_convert_assistant_tool_use_replays_empty_reasoning_content():
assert result[0]["tool_calls"][0]["id"] == "call_empty"
+def _convert_tool_turn(
+ assistant_content: list[MockBlock],
+ reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.REASONING_CONTENT,
+) -> list[dict]:
+ tool_ids = [
+ block.get("id")
+ for block in assistant_content
+ if block.get("type") == "tool_use"
+ ]
+ return AnthropicToOpenAIConverter.convert_messages(
+ [
+ MockMessage("assistant", assistant_content),
+ MockMessage(
+ "user",
+ [
+ MockBlock(
+ type="tool_result",
+ tool_use_id=tool_id,
+ content="result",
+ )
+ for tool_id in tool_ids
+ ],
+ ),
+ ],
+ reasoning_replay=reasoning_replay,
+ )
+
+
+def test_convert_assistant_tool_use_without_thinking_replays_empty_reasoning_content():
+ result = _convert_tool_turn(
+ [MockBlock(type="tool_use", id="call_missing", name="Read", input={})]
+ )
+
+ assert result[0]["content"] == ""
+ assert result[0]["reasoning_content"] == ""
+ assert result[0]["tool_calls"][0]["id"] == "call_missing"
+
+
+def test_convert_parallel_tool_use_without_thinking_replays_one_empty_reasoning_field():
+ result = _convert_tool_turn(
+ [
+ MockBlock(type="tool_use", id="call_1", name="Read", input={}),
+ MockBlock(type="tool_use", id="call_2", name="Glob", input={}),
+ ]
+ )
+
+ assert result[0]["reasoning_content"] == ""
+ assert [tool_call["id"] for tool_call in result[0]["tool_calls"]] == [
+ "call_1",
+ "call_2",
+ ]
+
+
+def test_convert_redacted_thinking_tool_use_replays_empty_reasoning_without_data():
+ result = _convert_tool_turn(
+ [
+ MockBlock(type="redacted_thinking", data="opaque-secret"),
+ MockBlock(type="tool_use", id="call_redacted", name="Read", input={}),
+ ]
+ )
+
+ assert result[0]["reasoning_content"] == ""
+ assert "opaque-secret" not in json.dumps(result)
+
+
+def test_convert_text_only_assistant_without_thinking_omits_reasoning_content():
+ result = AnthropicToOpenAIConverter.convert_messages(
+ [MockMessage("assistant", "Visible answer")],
+ reasoning_replay=ReasoningReplayMode.REASONING_CONTENT,
+ )
+
+ assert result == [{"role": "assistant", "content": "Visible answer"}]
+
+
+ "reasoning_replay",
+ [
+ ReasoningReplayMode.THINK_TAGS,
+ ReasoningReplayMode.REASONING,
+ ReasoningReplayMode.DISABLED,
+ ],
+)
+def test_convert_tool_use_without_thinking_does_not_change_other_replay_modes(
+ reasoning_replay: ReasoningReplayMode,
+) -> None:
+ result = _convert_tool_turn(
+ [MockBlock(type="tool_use", id="call_other", name="Read", input={})],
+ reasoning_replay,
+ )
+
+ assert "reasoning_content" not in result[0]
+
+
def test_convert_assistant_message_thinking_removed_when_disabled():
content = [
MockBlock(type="thinking", thinking="I need to calculate this."),
diff --git a/tests/providers/test_deepseek.py b/tests/providers/test_deepseek.py
index 001b04d5..f818ddb5 100644
--- a/tests/providers/test_deepseek.py
+++ b/tests/providers/test_deepseek.py
@@ -1,13 +1,10 @@
"""Tests for DeepSeek OpenAI-compatible Chat Completions provider."""
-import json
import logging
from types import SimpleNamespace
from unittest.mock import AsyncMock, patch
-import httpx
import pytest
-from openai import AsyncOpenAI
from free_claude_code.application.errors import InvalidRequestError
from free_claude_code.config.constants import ANTHROPIC_DEFAULT_MAX_OUTPUT_TOKENS
@@ -24,6 +21,7 @@ from free_claude_code.providers.deepseek import DeepSeekProvider
from tests.providers.support import (
REASONING_OFF,
REASONING_ON,
+ capture_openai_chat_wire_body,
immediate_admission,
reasoning_for,
)
@@ -44,36 +42,6 @@ def deepseek_provider(deepseek_config):
return DeepSeekProvider(deepseek_config, admission=immediate_admission())
-async def _capture_openai_wire_body(body: dict) -> dict:
- captured: list[dict] = []
-
- def handler(request: httpx.Request) -> httpx.Response:
- payload = json.loads(request.content)
- assert isinstance(payload, dict)
- captured.append(payload)
- return httpx.Response(
- 200,
- headers={"content-type": "text/event-stream"},
- text="data: [DONE]\n\n",
- )
-
- http_client = httpx.AsyncClient(transport=httpx.MockTransport(handler))
- client = AsyncOpenAI(
- api_key="test",
- base_url="https://deepseek.invalid",
- http_client=http_client,
- max_retries=0,
- )
- try:
- stream = await client.chat.completions.create(**body, stream=True)
- await stream.close()
- finally:
- await client.close()
-
- assert len(captured) == 1
- return captured[0]
-
-
def test_default_base_url_alias():
assert DEEPSEEK_DEFAULT_BASE == "https://api.deepseek.com"
@@ -866,8 +834,8 @@ async def test_wire_messages_keep_prefix_across_tool_thinking_fallback(
request, reasoning=reasoning_for(request)
)
- first_wire = await _capture_openai_wire_body(build(prefix_messages))
- continued_wire = await _capture_openai_wire_body(build(continued_messages))
+ first_wire = await capture_openai_chat_wire_body(build(prefix_messages))
+ continued_wire = await capture_openai_chat_wire_body(build(continued_messages))
first_messages = first_wire["messages"]
continued = continued_wire["messages"]
diff --git a/tests/providers/test_opencode.py b/tests/providers/test_opencode.py
index 4eeb9553..881c6e90 100644
--- a/tests/providers/test_opencode.py
+++ b/tests/providers/test_opencode.py
@@ -5,6 +5,7 @@ import pytest
from free_claude_code.core.anthropic.models import MessagesRequest
from free_claude_code.providers.base import ProviderConfig
from tests.providers.support import (
+ capture_openai_chat_wire_body,
immediate_admission,
profiled_provider,
reasoning_for,
@@ -12,7 +13,7 @@ from tests.providers.support import (
@pytest.mark.parametrize("provider_id", ["opencode", "opencode_go"])
-def test_build_request_body_preserves_empty_reasoning_content(
+def test_build_request_body_replays_tool_reasoning_natively(
provider_id: str,
) -> None:
provider = profiled_provider(
@@ -31,9 +32,30 @@ def test_build_request_body_preserves_empty_reasoning_content(
"messages": [
{
"role": "assistant",
- "content": "visible",
- "reasoning_content": "",
- }
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "I should inspect the file.",
+ "signature": "sig",
+ },
+ {
+ "type": "tool_use",
+ "id": "call_1",
+ "name": "Read",
+ "input": {"path": "README.md"},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_1",
+ "content": "file contents",
+ }
+ ],
+ },
],
"thinking": {"type": "enabled"},
}
@@ -41,15 +63,21 @@ def test_build_request_body_preserves_empty_reasoning_content(
body = provider._build_request_body(request, reasoning=reasoning_for(request))
- assert body["messages"][0] == {
- "role": "assistant",
- "content": "visible",
- "reasoning_content": "",
+ assistant = body["messages"][0]
+ assert assistant["content"] == ""
+ assert assistant["reasoning_content"] == "I should inspect the file."
+ assert "<think>" not in assistant["content"]
+ assert assistant["tool_calls"][0]["id"] == "call_1"
+ assert body["messages"][1] == {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "file contents",
}
@pytest.mark.parametrize("provider_id", ["opencode", "opencode_go"])
-def test_build_request_body_replays_tool_reasoning_natively(
+async def test_tool_only_history_sends_empty_reasoning_content_on_wire(
provider_id: str,
) -> None:
provider = profiled_provider(
@@ -69,17 +97,12 @@ def test_build_request_body_replays_tool_reasoning_natively(
{
"role": "assistant",
"content": [
- {
- "type": "thinking",
- "thinking": "I should inspect the file.",
- "signature": "sig",
- },
{
"type": "tool_use",
- "id": "call_1",
+ "id": "call_missing",
"name": "Read",
"input": {"path": "README.md"},
- },
+ }
],
},
{
@@ -87,25 +110,25 @@ def test_build_request_body_replays_tool_reasoning_natively(
"content": [
{
"type": "tool_result",
- "tool_use_id": "call_1",
+ "tool_use_id": "call_missing",
"content": "file contents",
}
],
},
],
- "thinking": {"type": "enabled"},
}
)
body = provider._build_request_body(request, reasoning=reasoning_for(request))
+ wire = await capture_openai_chat_wire_body(body)
- assistant = body["messages"][0]
+ assistant = wire["messages"][0]
assert assistant["content"] == ""
- assert assistant["reasoning_content"] == "I should inspect the file."
- assert "<think>" not in assistant["content"]
- assert assistant["tool_calls"][0]["id"] == "call_1"
- assert body["messages"][1] == {
+ assert assistant["reasoning_content"] == ""
+ assert assistant["tool_calls"][0]["id"] == "call_missing"
+ assert assistant["tool_calls"][0]["function"]["name"] == "Read"
+ assert wire["messages"][1] == {
"role": "tool",
- "tool_call_id": "call_1",
+ "tool_call_id": "call_missing",
"content": "file contents",
}
diff --git a/uv.lock b/uv.lock
index 042bfc56..513c244b 100644
--- a/uv.lock
+++ b/uv.lock
@@ -620,7 +620,7 @@ wheels = [
[[package]]
name = "free-claude-code"
-version = "4.16.6"
+version = "4.16.7"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },