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

Fix NIM chat template downgrade (#997)

Ali Khokhar47ddedcada2d
diff --git a/providers/nvidia_nim/retry.py b/providers/nvidia_nim/retry.py
index 3b105fd0..335a0b6d 100644
--- a/providers/nvidia_nim/retry.py
+++ b/providers/nvidia_nim/retry.py
@@ -11,8 +11,8 @@ def clone_body_without_reasoning_budget(body: dict[str, Any]) -> dict[str, Any]
def clone_body_without_chat_template(body: dict[str, Any]) -> dict[str, Any] | None:
- """Clone a request body and strip only chat_template."""
- return _clone_strip_extra_body(body, _strip_chat_template_field)
+ """Clone a request body and strip NIM chat-template control fields."""
+ return _clone_strip_extra_body(body, _strip_chat_template_fields)
def clone_body_without_reasoning_content(
@@ -51,8 +51,11 @@ def _strip_reasoning_budget_fields(extra_body: dict[str, Any]) -> bool:
return removed
-def _strip_chat_template_field(extra_body: dict[str, Any]) -> bool:
- return extra_body.pop("chat_template", None) is not None
+def _strip_chat_template_fields(extra_body: dict[str, Any]) -> bool:
+ removed = extra_body.pop("chat_template", None) is not None
+ if extra_body.pop("chat_template_kwargs", None) is not None:
+ removed = True
+ return removed
def _strip_message_reasoning_content(body: dict[str, Any]) -> bool:
diff --git a/pyproject.toml b/pyproject.toml
index f294f105..db759cdd 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "free-claude-code"
-version = "3.4.2"
+version = "3.4.3"
description = "Middleware between Claude Code CLI (Anthropic API) and NVIDIA NIM"
readme = "README.md"
requires-python = ">=3.14.0"
diff --git a/tests/providers/test_nvidia_nim.py b/tests/providers/test_nvidia_nim.py
index 83467f8d..82a19a74 100644
--- a/tests/providers/test_nvidia_nim.py
+++ b/tests/providers/test_nvidia_nim.py
@@ -428,12 +428,58 @@ async def test_stream_response_retries_without_chat_template(provider_config):
assert "reasoning_budget" not in first_extra
assert "chat_template" not in second_extra
- assert second_extra["chat_template_kwargs"] == {
+ assert "chat_template_kwargs" not in second_extra
+ assert "reasoning_budget" not in second_extra
+
+ event_text = "".join(events)
+ assert "event: error" not in event_text
+ assert "OK" in event_text
+
+
+async def test_stream_response_retries_without_chat_template_kwargs_issue_993(
+ provider_config,
+):
+ provider = NvidiaNimProvider(provider_config, nim_settings=NimSettings())
+ req = MockRequest(model="mistralai/mistral-small-4-119b-2603")
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(content="OK", reasoning_content=""),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=2)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ first_error = _make_bad_request_error(
+ "chat_template is not supported for Mistral tokenizers."
+ )
+
+ with patch.object(
+ provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [first_error, mock_stream()]
+
+ events = [e async for e in provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+
+ first_extra = mock_create.call_args_list[0].kwargs["extra_body"]
+ second_kwargs = mock_create.call_args_list[1].kwargs
+
+ assert "chat_template" not in first_extra
+ assert first_extra["chat_template_kwargs"] == {
"thinking": True,
"enable_thinking": True,
"reasoning_budget": 100,
}
- assert "reasoning_budget" not in second_extra
+ second_extra = second_kwargs.get("extra_body") or {}
+ assert "chat_template" not in second_extra
+ assert "chat_template_kwargs" not in second_extra
event_text = "".join(events)
assert "event: error" not in event_text
diff --git a/tests/providers/test_nvidia_nim_request.py b/tests/providers/test_nvidia_nim_request.py
index 276d5464..22acbd0c 100644
--- a/tests/providers/test_nvidia_nim_request.py
+++ b/tests/providers/test_nvidia_nim_request.py
@@ -318,13 +318,39 @@ class TestBuildRequestBody:
assert cloned is not None
assert "chat_template" not in cloned["extra_body"]
- assert cloned["extra_body"]["chat_template_kwargs"] == {
+ assert "chat_template_kwargs" not in cloned["extra_body"]
+ assert cloned["extra_body"]["ignore_eos"] is False
+ assert body["extra_body"]["chat_template"] == "custom_template"
+ assert body["extra_body"]["chat_template_kwargs"] == {
"thinking": True,
"enable_thinking": True,
"reasoning_budget": 100,
}
+
+ def test_clone_body_without_chat_template_kwargs_only(self):
+ body = {
+ "model": "test",
+ "extra_body": {
+ "chat_template_kwargs": {
+ "thinking": True,
+ "enable_thinking": True,
+ "reasoning_budget": 100,
+ },
+ "ignore_eos": False,
+ },
+ }
+
+ cloned = clone_body_without_chat_template(body)
+
+ assert cloned is not None
+ assert "chat_template" not in cloned["extra_body"]
+ assert "chat_template_kwargs" not in cloned["extra_body"]
assert cloned["extra_body"]["ignore_eos"] is False
- assert body["extra_body"]["chat_template"] == "custom_template"
+
+ def test_clone_body_without_chat_template_returns_none_when_unchanged(self):
+ body = {"model": "test", "extra_body": {"ignore_eos": False}}
+
+ assert clone_body_without_chat_template(body) is None
def test_no_chat_template_kwargs_when_thinking_disabled(self):
req = MagicMock()
diff --git a/uv.lock b/uv.lock
index 21eb51a9..7b8728f7 100644
--- a/uv.lock
+++ b/uv.lock
@@ -561,7 +561,7 @@ wheels = [
[[package]]
name = "free-claude-code"
-version = "3.4.2"
+version = "3.4.3"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },