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

Recover MiniMax-M3 native tool calls on NVIDIA NIM (#1246)

Ali Khokharcac2db5042a7
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index 73c73ab2..e2768d33 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -679,6 +679,14 @@ fallback retry when an upstream request rejects reasoning fields.
NIM reasoning budget control is also treated as a provider-owned best-effort
downgrade: if an upstream NIM deployment rejects explicit budget control, FCC
retries without the budget while preserving thinking enablement.
+NIM also owns response normalization for model-native tool markup exposed in
+chat-completion text. The normalizer recognizes the native protocol signature
+only when tools are declared, validates one complete tool block against the
+request schemas, and converts it into ordinary OpenAI tool-call deltas before
+the shared stream runner can commit visible text. Native structured tool-call
+deltas remain authoritative when both forms appear; incomplete or invalid
+native markup is a retryable upstream protocol failure rather than user-visible
+assistant text.
### Reasoning Ownership
diff --git a/pyproject.toml b/pyproject.toml
index dd4adbe3..2130730d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "free-claude-code"
-version = "4.12.1"
+version = "4.12.2"
description = "Local proxy connecting coding agents to OpenAI-compatible AI providers"
readme = "README.md"
requires-python = ">=3.14.0"
diff --git a/smoke/README.md b/smoke/README.md
index 6042d749..e0b0dc26 100644
--- a/smoke/README.md
+++ b/smoke/README.md
@@ -102,7 +102,7 @@ uv run pytest smoke/product -n 0 -s --tb=short
```powershell
$env:FCC_LIVE_SMOKE = "1"
$env:FCC_SMOKE_TARGETS = "nvidia_nim_cli"
-$env:FCC_SMOKE_NIM_MODELS = "z-ai/glm-5.2,moonshotai/kimi-k2.6,minimaxai/minimax-m2.7,nvidia/nemotron-3-super-120b-a12b,deepseek-ai/deepseek-v4-pro,deepseek-ai/deepseek-v4-flash"
+$env:FCC_SMOKE_NIM_MODELS = "z-ai/glm-5.2,moonshotai/kimi-k2.6,minimaxai/minimax-m2.7,minimaxai/minimax-m3,nvidia/nemotron-3-super-120b-a12b,deepseek-ai/deepseek-v4-pro,deepseek-ai/deepseek-v4-flash"
uv run pytest smoke/product -n 0 -s --tb=short
```
diff --git a/smoke/lib/config.py b/smoke/lib/config.py
index c2caf819..4fd77a53 100644
--- a/smoke/lib/config.py
+++ b/smoke/lib/config.py
@@ -78,6 +78,7 @@ NVIDIA_NIM_CLI_DEFAULT_MODELS: tuple[str, ...] = (
"z-ai/glm-5.2",
"moonshotai/kimi-k2.6",
"minimaxai/minimax-m2.7",
+ "minimaxai/minimax-m3",
"nvidia/nemotron-3-super-120b-a12b",
"deepseek-ai/deepseek-v4-pro",
"deepseek-ai/deepseek-v4-flash",
diff --git a/src/free_claude_code/core/anthropic/streaming/__init__.py b/src/free_claude_code/core/anthropic/streaming/__init__.py
index 280ddab2..afe31200 100644
--- a/src/free_claude_code/core/anthropic/streaming/__init__.py
+++ b/src/free_claude_code/core/anthropic/streaming/__init__.py
@@ -13,6 +13,7 @@ from .recovery import (
ToolSchema,
accept_tool_json_repair,
continuation_suffix,
+ make_response_recovery_body,
make_text_recovery_body,
make_tool_repair_body,
parse_complete_tool_input,
@@ -31,6 +32,7 @@ __all__ = [
"anthropic_terminal_failure_frame",
"continuation_suffix",
"format_sse_event",
+ "make_response_recovery_body",
"make_text_recovery_body",
"make_tool_repair_body",
"map_stop_reason",
diff --git a/src/free_claude_code/core/anthropic/streaming/recovery.py b/src/free_claude_code/core/anthropic/streaming/recovery.py
index 162fec06..68143304 100644
--- a/src/free_claude_code/core/anthropic/streaming/recovery.py
+++ b/src/free_claude_code/core/anthropic/streaming/recovery.py
@@ -126,9 +126,19 @@ def make_text_recovery_body(
partial_thinking: str = "",
) -> dict[str, Any]:
"""Build a text-only continuation request for an OpenAI-chat upstream."""
- recovery = deepcopy(body)
+ recovery = make_response_recovery_body(body, partial_text, partial_thinking)
recovery.pop("tools", None)
recovery.pop("tool_choice", None)
+ return recovery
+
+
+def make_response_recovery_body(
+ body: dict[str, Any],
+ partial_text: str,
+ partial_thinking: str = "",
+) -> dict[str, Any]:
+ """Build a continuation request that retains the original tool contract."""
+ recovery = deepcopy(body)
messages = _copied_messages(recovery)
if partial_text:
messages.append({"role": "assistant", "content": partial_text})
diff --git a/src/free_claude_code/providers/failure_policy.py b/src/free_claude_code/providers/failure_policy.py
index fe6743ff..2d33839e 100644
--- a/src/free_claude_code/providers/failure_policy.py
+++ b/src/free_claude_code/providers/failure_policy.py
@@ -48,6 +48,10 @@ class RetryableProviderProtocolError(RuntimeError):
"""A malformed upstream protocol result eligible for provider retry."""
+class RetryableToolProtocolError(RetryableProviderProtocolError):
+ """A malformed tool response whose continuation still requires tools."""
+
+
def classify_provider_failure(
exc: Exception,
*,
diff --git a/src/free_claude_code/providers/mistral/client.py b/src/free_claude_code/providers/mistral/client.py
index 503ce1a6..d0e7b93e 100644
--- a/src/free_claude_code/providers/mistral/client.py
+++ b/src/free_claude_code/providers/mistral/client.py
@@ -1,5 +1,6 @@
"""Mistral La Plateforme provider implementation (OpenAI-compatible chat completions)."""
+from collections.abc import Mapping
from typing import Any
from loguru import logger
@@ -69,5 +70,5 @@ class MistralProvider(OpenAIChatProvider):
)
return retry_body
- def _normalize_stream(self, stream: Any) -> Any:
+ def _normalize_stream(self, stream: Any, _body: Mapping[str, Any]) -> Any:
return normalize_mistral_stream(stream)
diff --git a/src/free_claude_code/providers/nvidia_nim/client.py b/src/free_claude_code/providers/nvidia_nim/client.py
index 252651ac..9cebd7a4 100644
--- a/src/free_claude_code/providers/nvidia_nim/client.py
+++ b/src/free_claude_code/providers/nvidia_nim/client.py
@@ -24,6 +24,7 @@ from free_claude_code.providers.openai_chat import (
OpenAIChatProvider,
)
+from .native_tool_stream import normalize_nim_native_tool_stream
from .request_options import NIM_REQUEST_POLICY, build_nim_request_body
from .retry import (
clone_body_without_chat_template,
@@ -80,6 +81,10 @@ class NvidiaNimProvider(OpenAIChatProvider):
"""Strip private request metadata before calling NVIDIA NIM."""
return body_without_nim_tool_argument_aliases(body)
+ def _normalize_stream(self, stream: Any, _body: Mapping[str, Any]) -> Any:
+ """Repair model-native MiniMax tool markup leaked by NVIDIA NIM."""
+ return normalize_nim_native_tool_stream(stream, _body)
+
def _tool_argument_aliases(self, body: dict[str, Any]) -> dict[str, dict[str, str]]:
"""Return NIM tool argument aliases captured while building this request."""
return nim_tool_argument_aliases_from_body(body)
diff --git a/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py
new file mode 100644
index 00000000..1c840b3c
--- /dev/null
+++ b/src/free_claude_code/providers/nvidia_nim/native_tool_stream.py
@@ -0,0 +1,667 @@
+"""Normalize native MiniMax-M3 tool markup exposed by NVIDIA NIM."""
+
+import json
+import uuid
+from collections.abc import AsyncIterator, Mapping, Sequence
+from dataclasses import dataclass
+from enum import StrEnum
+from types import SimpleNamespace
+from typing import Any
+
+import jsonschema
+
+from free_claude_code.providers.failure_policy import RetryableToolProtocolError
+from free_claude_code.providers.http import maybe_await_aclose
+
+_NAMESPACE = "]<]minimax[>["
+_TOOL_BLOCK_START = f"{_NAMESPACE}<tool_call>"
+_TOOL_BLOCK_END = f"{_NAMESPACE}</tool_call>"
+_INVOKE_START = f"{_NAMESPACE}<invoke"
+_INVOKE_END = f"{_NAMESPACE}</invoke>"
+_ELEMENT_START = f"{_NAMESPACE}<"
+_ELEMENT_END_START = f"{_NAMESPACE}</"
+_MIXED_TEXT_FIELD = "$text"
+_MAX_NATIVE_TOOL_BLOCK_CHARS = 4 * 1024 * 1024
+_MAX_NATIVE_ARGUMENT_DEPTH = 64
+
+
+class NimNativeToolProtocolError(RetryableToolProtocolError):
+ """NIM exposed malformed model-native tool markup instead of tool deltas."""
+
+
+@dataclass(frozen=True, slots=True)
+class _Element:
+ name: str
+ text: str
+ children: tuple[_Element, ...]
+
+
+@dataclass(frozen=True, slots=True)
+class _NativeToolCall:
+ index: int
+ name: str
+ arguments: dict[str, Any]
+
+
+class _FramingMode(StrEnum):
+ TEXT = "text"
+ TOOL_BLOCK = "tool_block"
+ AFTER_TOOL_BLOCK = "after_tool_block"
+ FINISHED = "finished"
+
+
+class _MiniMaxM3ToolFramer:
+ """Separate visible text from one namespace-delimited native tool block."""
+
+ def __init__(self) -> None:
+ self._mode = _FramingMode.TEXT
+ self._text_tail = ""
+ self._tool_tail = ""
+ self._tool_parts: list[str] = []
+ self._tool_length = 0
+ self._tool_block: str | None = None
+
+ @property
+ def tool_block(self) -> str | None:
+ return self._tool_block
+
+ def feed(self, text: str) -> str:
+ if self._mode is _FramingMode.FINISHED:
+ raise RuntimeError("MiniMax-M3 tool framer is already finished")
+ if not text:
+ return ""
+ if self._mode is _FramingMode.TOOL_BLOCK:
+ self._feed_tool_block(text)
+ return ""
+ if self._mode is _FramingMode.AFTER_TOOL_BLOCK:
+ self._validate_trailing_text(text)
+ return ""
+
+ candidate = "".join((self._text_tail, text))
+ marker_index = candidate.find(_TOOL_BLOCK_START)
+ if marker_index >= 0:
+ visible = candidate[:marker_index]
+ self._text_tail = ""
+ self._mode = _FramingMode.TOOL_BLOCK
+ self._feed_tool_block(candidate[marker_index + len(_TOOL_BLOCK_START) :])
+ return visible
+
+ held_length = _partial_marker_suffix_length(candidate, _TOOL_BLOCK_START)
+ if held_length:
+ visible = candidate[:-held_length]
+ self._text_tail = candidate[-held_length:]
+ return visible
+
+ self._text_tail = ""
+ return candidate
+
+ def finish(self, *, require_complete: bool) -> str:
+ if self._mode is _FramingMode.FINISHED:
+ return ""
+ if self._mode is _FramingMode.TEXT:
+ tail = self._text_tail
+ self._text_tail = ""
+ self._mode = _FramingMode.FINISHED
+ return tail
+ if self._mode is _FramingMode.TOOL_BLOCK and require_complete:
+ self._mode = _FramingMode.FINISHED
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an incomplete native MiniMax tool call."
+ )
+ self._tool_parts.clear()
+ self._tool_tail = ""
+ self._mode = _FramingMode.FINISHED
+ return ""
+
+ def _feed_tool_block(self, text: str) -> None:
+ candidate = "".join((self._tool_tail, text))
+ marker_index = candidate.find(_TOOL_BLOCK_END)
+ if marker_index < 0:
+ held_length = _partial_marker_suffix_length(candidate, _TOOL_BLOCK_END)
+ if held_length:
+ self._append_tool_fragment(candidate[:-held_length])
+ self._tool_tail = candidate[-held_length:]
+ else:
+ self._append_tool_fragment(candidate)
+ self._tool_tail = ""
+ return
+
+ self._append_tool_fragment(candidate[:marker_index])
+ self._tool_block = "".join(self._tool_parts)
+ self._tool_parts.clear()
+ self._tool_tail = ""
+ self._mode = _FramingMode.AFTER_TOOL_BLOCK
+ self._validate_trailing_text(candidate[marker_index + len(_TOOL_BLOCK_END) :])
+
+ def _append_tool_fragment(self, fragment: str) -> None:
+ if not fragment:
+ return
+ self._tool_length += len(fragment)
+ if self._tool_length > _MAX_NATIVE_TOOL_BLOCK_CHARS:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an oversized native MiniMax tool call."
+ )
+ self._tool_parts.append(fragment)
+
+ @staticmethod
+ def _validate_trailing_text(text: str) -> None:
+ if text.strip():
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned content after a native MiniMax tool call."
+ )
+
+
+def normalize_nim_native_tool_stream(
+ stream: Any,
+ body: Mapping[str, Any],
+) -> AsyncIterator[Any]:
+ """Convert leaked MiniMax-M3 markup into ordinary OpenAI tool-call chunks."""
+ schemas = _openai_tool_schemas(body)
+ if not schemas:
+ return stream
+
+ async def _iter() -> AsyncIterator[Any]:
+ framer = _MiniMaxM3ToolFramer()
+ structured_tool_calls_seen = False
+ finalized = False
+
+ try:
+ async for chunk in stream:
+ choices = getattr(chunk, "choices", None)
+ if not choices:
+ yield chunk
+ continue
+
+ choice = choices[0]
+ delta = getattr(choice, "delta", None)
+ if delta is None:
+ yield chunk
+ continue
+
+ native_tool_calls = getattr(delta, "tool_calls", None)
+ if _has_structured_tool_calls(native_tool_calls):
+ structured_tool_calls_seen = True
+
+ content = getattr(delta, "content", None)
+ safe_content = (
+ framer.feed(content) if isinstance(content, str) else content
+ )
+ finish_reason = getattr(choice, "finish_reason", None)
+ generated_calls: tuple[_NativeToolCall, ...] = ()
+ if finish_reason is not None:
+ tail = framer.finish(
+ require_complete=not structured_tool_calls_seen
+ )
+ finalized = True
+ safe_content = _join_optional_text(safe_content, tail)
+ if not structured_tool_calls_seen and framer.tool_block is not None:
+ generated_calls = _parse_tool_block(
+ framer.tool_block,
+ schemas,
+ )
+
+ normalized = _normalized_chunks(
+ chunk,
+ choice,
+ delta,
+ content=safe_content,
+ generated_calls=generated_calls,
+ terminal=finish_reason is not None,
+ )
+ for normalized_chunk in normalized:
+ yield normalized_chunk
+
+ if not finalized:
+ tail = framer.finish(require_complete=not structured_tool_calls_seen)
+ generated_calls = (
+ ()
+ if structured_tool_calls_seen or framer.tool_block is None
+ else _parse_tool_block(framer.tool_block, schemas)
+ )
+ if tail or generated_calls:
+ for normalized_chunk in _synthetic_chunks(
+ content=tail or None,
+ generated_calls=generated_calls,
+ ):
+ yield normalized_chunk
+ finally:
+ await maybe_await_aclose(stream)
+
+ return _iter()
+
+
+def _normalized_chunks(
+ source_chunk: Any,
+ source_choice: Any,
+ source_delta: Any,
+ *,
+ content: Any,
+ generated_calls: tuple[_NativeToolCall, ...],
+ terminal: bool,
+) -> list[Any]:
+ reasoning = getattr(source_delta, "reasoning_content", None)
+ structured_calls = getattr(source_delta, "tool_calls", None)
+ source_finish_reason = getattr(source_choice, "finish_reason", None)
+ source_usage = getattr(source_chunk, "usage", None)
+
+ chunks: list[Any] = []
+ has_source_payload = (
+ bool(content) or bool(reasoning) or _has_structured_tool_calls(structured_calls)
+ )
+ if has_source_payload or not generated_calls:
+ chunks.append(
+ _chunk(
+ content=content,
+ reasoning_content=reasoning,
+ tool_calls=structured_calls,
+ )
+ )
+
+ chunks.extend(_tool_call_chunk(call) for call in generated_calls)
+
+ if not chunks:
+ chunks.append(_chunk())
+
+ if terminal:
+ chunks[-1].choices[0].finish_reason = source_finish_reason
+ chunks[-1].usage = source_usage
+ return chunks
+
+
+def _synthetic_chunks(
+ *,
+ content: str | None,
+ generated_calls: tuple[_NativeToolCall, ...],
+) -> list[Any]:
+ chunks = [_chunk(content=content)] if content else []
+ chunks.extend(_tool_call_chunk(call) for call in generated_calls)
+ return chunks
+
+
+def _chunk(
+ *,
+ content: Any = None,
+ reasoning_content: Any = None,
+ tool_calls: Any = None,
+) -> Any:
+ return SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(
+ content=content,
+ reasoning_content=reasoning_content,
+ tool_calls=tool_calls,
+ ),
+ finish_reason=None,
+ )
+ ],
+ usage=None,
+ )
+
+
+def _tool_call_chunk(call: _NativeToolCall) -> Any:
+ tool_call = SimpleNamespace(
+ index=call.index,
+ id=f"call_nim_native_{uuid.uuid4().hex}",
+ function=SimpleNamespace(
+ name=call.name,
+ arguments=json.dumps(
+ call.arguments,
+ ensure_ascii=False,
+ separators=(",", ":"),
+ ),
+ ),
+ )
+ return _chunk(tool_calls=[tool_call])
+
+
+def _parse_tool_block(
+ block: str,
+ schemas: Mapping[str, dict[str, Any]],
+) -> tuple[_NativeToolCall, ...]:
+ cursor = 0
+ calls: list[_NativeToolCall] = []
+ while True:
+ cursor = _skip_whitespace(block, cursor)
+ if cursor == len(block):
+ break
+ name, body, cursor = _parse_invoke(block, cursor)
+ schema = schemas.get(name)
+ if schema is None:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned a native call for an undeclared tool."
+ )
+ arguments = _parse_arguments(body, schema)
+ _validate_arguments(name, arguments, schema)
+ calls.append(
+ _NativeToolCall(
+ index=len(calls),
+ name=name,
+ arguments=arguments,
+ )
+ )
+
+ if not calls:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an empty native MiniMax tool-call block."
+ )
+ return tuple(calls)
+
+
+def _parse_invoke(text: str, cursor: int) -> tuple[str, str, int]:
+ if not text.startswith(_INVOKE_START, cursor):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned malformed native MiniMax tool markup."
+ )
+ tag_end = text.find(">", cursor + len(_INVOKE_START))
+ if tag_end < 0:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an incomplete native MiniMax invoke tag."
+ )
+ attributes = text[cursor + len(_INVOKE_START) : tag_end]
+ name = _parse_invoke_name(attributes)
+ body_start = tag_end + 1
+ body_end = text.find(_INVOKE_END, body_start)
+ if body_end < 0:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an incomplete native MiniMax invoke block."
+ )
+ return name, text[body_start:body_end], body_end + len(_INVOKE_END)
+
+
+def _parse_invoke_name(attributes: str) -> str:
+ value = attributes.strip()
+ if not value.startswith("name"):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned a native MiniMax invoke without a tool name."
+ )
+ remainder = value[len("name") :].lstrip()
+ if not remainder.startswith("="):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned a malformed native MiniMax tool name."
+ )
+ remainder = remainder[1:].strip()
+ if not remainder:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an empty native MiniMax tool name."
+ )
+
+ quote = remainder[0]
+ if quote in {'"', "'"}:
+ if len(remainder) < 2 or not remainder.endswith(quote):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an unterminated native MiniMax tool name."
+ )
+ name = remainder[1:-1]
+ else:
+ if any(character.isspace() for character in remainder):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned malformed native MiniMax invoke attributes."
+ )
+ name = remainder
+
+ name = name.strip()
+ if not name:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an empty native MiniMax tool name."
+ )
+ return name
+
+
+def _parse_arguments(body: str, schema: dict[str, Any]) -> dict[str, Any]:
+ cursor = 0
+ elements: list[_Element] = []
+ while True:
+ cursor = _skip_whitespace(body, cursor)
+ if cursor == len(body):
+ break
+ element, cursor = _parse_element(body, cursor, depth=1)
+ elements.append(element)
+ return _elements_to_object(elements, schema)
+
+
+def _parse_element(text: str, cursor: int, *, depth: int) -> tuple[_Element, int]:
+ if depth > _MAX_NATIVE_ARGUMENT_DEPTH:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned excessively nested native MiniMax tool arguments."
+ )
+ if not text.startswith(_ELEMENT_START, cursor) or text.startswith(
+ _ELEMENT_END_START, cursor
+ ):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned malformed native MiniMax tool arguments."
+ )
+ name_start = cursor + len(_ELEMENT_START)
+ tag_end = text.find(">", name_start)
+ if tag_end < 0:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an incomplete native MiniMax argument tag."
+ )
+ name = text[name_start:tag_end]
+ if (
+ not name
+ or name.strip() != name
+ or any(character.isspace() for character in name)
+ ):
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an invalid native MiniMax argument name."
+ )
+
+ close_tag = f"{_ELEMENT_END_START}{name}>"
+ cursor = tag_end + 1
+ text_parts: list[str] = []
+ children: list[_Element] = []
+ while True:
+ marker = text.find(_NAMESPACE, cursor)
+ if marker < 0:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned an incomplete native MiniMax argument."
+ )
+ text_parts.append(text[cursor:marker])
+ if text.startswith(close_tag, marker):
+ cursor = marker + len(close_tag)
+ break
+ if text.startswith(_ELEMENT_START, marker) and not text.startswith(
+ _ELEMENT_END_START, marker
+ ):
+ child, cursor = _parse_element(text, marker, depth=depth + 1)
+ children.append(child)
+ continue
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned mismatched native MiniMax argument tags."
+ )
+
+ return (
+ _Element(
+ name=name,
+ text="".join(text_parts),
+ children=tuple(children),
+ ),
+ cursor,
+ )
+
+
+def _elements_to_object(
+ elements: Sequence[_Element],
+ schema: Mapping[str, Any],
+) -> dict[str, Any]:
+ properties = schema.get("properties")
+ property_schemas = properties if isinstance(properties, Mapping) else {}
+ additional = schema.get("additionalProperties")
+ output: dict[str, Any] = {}
+ for element in elements:
+ property_schema = property_schemas.get(element.name)
+ if not isinstance(property_schema, Mapping):
+ property_schema = additional if isinstance(additional, Mapping) else {}
+ value = _element_value(element, property_schema)
+ _append_object_value(output, element.name, value)
+ return output
+
+
+def _element_value(element: _Element, schema: Mapping[str, Any]) -> Any:
+ schema_type = _schema_type(schema)
+ if element.children:
+ if schema_type == "array":
+ item_schema = schema.get("items")
+ if not isinstance(item_schema, Mapping):
+ item_schema = {}
+ return [_element_value(child, item_schema) for child in element.children]
+
+ result = _elements_to_object(element.children, schema)
+ if element.text.strip():
+ mixed_name = _MIXED_TEXT_FIELD
+ while mixed_name in result:
+ mixed_name = f"${mixed_name}"
+ result[mixed_name] = element.text
+ return result
+
+ return _coerce_leaf(element.text, schema_type, schema)
+
+
+def _coerce_leaf(
+ value: str,
+ schema_type: str | None,
+ schema: Mapping[str, Any],
+) -> Any:
+ enum_values = schema.get("enum")
+ if isinstance(enum_values, list):
+ for enum_value in enum_values:
+ if str(enum_value) == value:
+ return enum_value
+ if "const" in schema and str(schema["const"]) == value:
+ return schema["const"]
+
+ stripped = value.strip()
+ try:
+ if schema_type == "integer":
+ return int(stripped)
+ if schema_type == "number":
+ number = json.loads(stripped)
+ if isinstance(number, int | float) and not isinstance(number, bool):
+ return number
+ raise ValueError
+ if schema_type == "boolean":
+ if stripped.lower() == "true":
+ return True
+ if stripped.lower() == "false":
+ return False
+ raise ValueError
+ if schema_type == "null":
+ if stripped.lower() == "null":
+ return None
+ raise ValueError
+ if schema_type == "array":
+ parsed = json.loads(stripped)
+ if isinstance(parsed, list):
+ return parsed
+ raise ValueError
+ if schema_type == "object":
+ parsed = json.loads(stripped)
+ if isinstance(parsed, dict):
+ return parsed
+ raise ValueError
+ except json.JSONDecodeError, ValueError:
+ raise NimNativeToolProtocolError(
+ "NVIDIA NIM returned a native MiniMax argument with the wrong type."
+ ) from None
+ return value
+
+
+def _schema_type(schema: Mapping[str, Any]) -> str | None:
+ declared = schema.get("type")
+ if isinstance(declared, str):
+ return declared
+ if isinstance(declared, list):
+ non_null = [
+ item for item in declared if isinstance(item, str) and item != "null"
+ ]
+ if len(non_null) == 1:
+ return non_null[0]
+
+ for keyword in ("oneOf", "anyOf"):
+ alternatives = schema.get(keyword)
+ if not isinstance(alternatives, list):
+ continue
+ types = {
+ nested_type
+ for alternative in alternatives
+ if isinstance(alternative, Mapping)
+ if (nested_type := _schema_type(alternative)) not in (None, "null")
+ }
+ if len(types) == 1:
+ return types.pop()
+ return None
+
+
+def _validate_arguments(
+ tool_name: str,
+ arguments: dict[str, Any],
+ schema: dict[str, Any],
+) -> None:
+ try:
+ validator_type = jsonschema.validators.validator_for(schema)
+ validator_type.check_schema(schema)
+ validator_type(schema).validate(arguments)
+ except jsonschema.exceptions.SchemaError:
+ return
+ except jsonschema.exceptions.ValidationError:
+ raise NimNativeToolProtocolError(
+ f"NVIDIA NIM returned schema-invalid arguments for tool {tool_name!r}."
+ ) from None
+
+
+def _openai_tool_schemas(body: Mapping[str, Any]) -> dict[str, dict[str, Any]]:
+ tools = body.get("tools")
+ if not isinstance(tools, list):
+ return {}
+
+ schemas: dict[str, dict[str, Any]] = {}
+ for tool in tools:
+ if not isinstance(tool, Mapping):
+ continue
+ function = tool.get("function")
+ if not isinstance(function, Mapping):
+ continue
+ name = function.get("name")
+ if not isinstance(name, str) or not name:
+ continue
+ parameters = function.get("parameters")
+ schemas[name] = dict(parameters) if isinstance(parameters, Mapping) else {}
+ return schemas
+
+
+def _append_object_value(output: dict[str, Any], name: str, value: Any) -> None:
+ if name not in output:
+ output[name] = value
+ return
+ existing = output[name]
+ if isinstance(existing, list):
+ existing.append(value)
+ else:
+ output[name] = [existing, value]
+
+
+def _skip_whitespace(text: str, cursor: int) -> int:
+ while cursor < len(text) and text[cursor].isspace():
+ cursor += 1
+ return cursor
+
+
+def _partial_marker_suffix_length(text: str, marker: str) -> int:
+ max_length = min(len(text), len(marker) - 1)
+ for length in range(max_length, 0, -1):
+ if marker.startswith(text[-length:]):
+ return length
+ return 0
+
+
+def _join_optional_text(first: Any, second: str) -> Any:
+ if not second:
+ return first
+ if isinstance(first, str):
+ return "".join((first, second))
+ return second
+
+
+def _has_structured_tool_calls(value: Any) -> bool:
+ return isinstance(value, list | tuple) and bool(value)
diff --git a/src/free_claude_code/providers/openai_chat/provider.py b/src/free_claude_code/providers/openai_chat/provider.py
index 12fbc741..606a2d3e 100644
--- a/src/free_claude_code/providers/openai_chat/provider.py
+++ b/src/free_claude_code/providers/openai_chat/provider.py
@@ -4,6 +4,7 @@ import asyncio
import sys
import uuid
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator, Mapping
+from dataclasses import dataclass
from typing import Any
import httpx
@@ -21,6 +22,7 @@ from free_claude_code.core.anthropic.streaming import (
AnthropicStreamLedger,
accept_tool_json_repair,
continuation_suffix,
+ make_response_recovery_body,
make_text_recovery_body,
make_tool_repair_body,
map_stop_reason,
@@ -37,6 +39,7 @@ from free_claude_code.providers.admission import (
)
from free_claude_code.providers.base import BaseProvider, ProviderConfig
from free_claude_code.providers.failure_policy import (
+ RetryableToolProtocolError,
classify_provider_failure,
underlying_provider_error,
)
@@ -57,6 +60,7 @@ from .profiles import OpenAIChatProfile
from .request_policy import build_openai_chat_request_body
from .tool_calls import (
OpenAIToolCallAssembler,
+ OpenAIToolCallCollector,
all_emitted_tools_complete,
has_committed_sse_output,
iter_heuristic_tool_use_sse,
@@ -73,6 +77,13 @@ from .usage import (
OpenAIAsyncCredentialProvider = Callable[[], Awaitable[str]]
+@dataclass(frozen=True, slots=True)
+class _CollectedRecoveryOutput:
+ text: str
+ thinking: str
+ tool_calls: tuple[dict[str, Any], ...]
+
+
class OpenAIChatProvider(BaseProvider):
"""OpenAI-compatible ``/chat/completions`` provider configured by a profile."""
@@ -206,7 +217,7 @@ class OpenAIChatProvider(BaseProvider):
**create_body,
stream=True,
)
- stream = self._normalize_stream(stream)
+ stream = self._normalize_stream(stream, body)
retain_attempt = True
return stream, body, attempt
except asyncio.CancelledError:
@@ -236,7 +247,7 @@ class OpenAIChatProvider(BaseProvider):
raise RuntimeError("provider retry session exhausted without a final error")
- def _normalize_stream(self, stream: Any) -> Any:
+ def _normalize_stream(self, stream: Any, _body: Mapping[str, Any]) -> Any:
"""Return the provider-specific stream view consumed by the base runner."""
return stream
@@ -718,25 +729,26 @@ class _OpenAIChatStreamRunner:
for event in recovery.flush():
yield event
- async def _collect_recovery_text(
+ async def _collect_recovery_output(
self,
body: dict[str, Any],
*,
include_reasoning: bool,
retry_session: ProviderRetrySession,
- ) -> tuple[str, str]:
- """Collect a complete text/reasoning continuation stream."""
+ ) -> _CollectedRecoveryOutput:
+ """Collect one complete buffered continuation response."""
last_error: Exception | None = None
while retry_session.can_attempt:
stream: Any | None = None
attempt: ProviderAttempt | None = None
try:
- stream, _, attempt = await self._provider._create_stream(
+ stream, accepted_body, attempt = await self._provider._create_stream(
body,
retry_session,
)
text_parts: list[str] = []
thinking_parts: list[str] = []
+ tool_calls = OpenAIToolCallCollector()
terminal_seen = False
async for chunk in stream:
if not attempt.accepted:
@@ -756,11 +768,30 @@ class _OpenAIChatStreamRunner:
content = getattr(delta, "content", None)
if isinstance(content, str) and content:
text_parts.append(content)
- if not terminal_seen:
+ native_tool_calls = getattr(delta, "tool_calls", None)
+ if isinstance(native_tool_calls, list | tuple):
+ for tool_call in native_tool_calls:
+ tool_calls.add(tool_call)
+
+ completed_tool_calls = tool_calls.completed_calls(
+ self._request,
+ tool_argument_aliases=self._provider._tool_argument_aliases(
+ accepted_body
+ ),
+ )
+ if tool_calls.has_calls and completed_tool_calls is None:
+ raise TruncatedProviderStreamError(
+ "Recovery stream ended with an incomplete tool call."
+ )
+ if not terminal_seen and not completed_tool_calls:
raise TruncatedProviderStreamError(
"Recovery stream ended without finish_reason."
)
- return "".join(text_parts), "".join(thinking_parts)
+ return _CollectedRecoveryOutput(
+ text="".join(text_parts),
+ thinking="".join(thinking_parts),
+ tool_calls=completed_tool_calls or (),
+ )
except Exception as error:
last_error = error
retryable = is_retryable_stream_error(error)
@@ -792,7 +823,7 @@ class _OpenAIChatStreamRunner:
await attempt.aclose()
if last_error is not None:
raise last_error
- return "", ""
+ return _CollectedRecoveryOutput(text="", thinking="", tool_calls=())
async def _recovery_events(
self,
@@ -843,14 +874,25 @@ class _OpenAIChatStreamRunner:
if not partial_text and not partial_thinking:
return None
- recovery_body = make_text_recovery_body(body, partial_text, partial_thinking)
- text, thinking = await self._collect_recovery_text(
+ if isinstance(error, RetryableToolProtocolError):
+ recovery_body = make_response_recovery_body(
+ body,
+ partial_text,
+ partial_thinking,
+ )
+ else:
+ recovery_body = make_text_recovery_body(
+ body,
+ partial_text,
+ partial_thinking,
+ )
+ recovered = await self._collect_recovery_output(
recovery_body,
include_reasoning=output_reasoning,
retry_session=retry_session,
)
- text_suffix = continuation_suffix(partial_text, text)
- thinking_suffix = continuation_suffix(partial_thinking, thinking)
+ text_suffix = continuation_suffix(partial_text, recovered.text)
+ thinking_suffix = continuation_suffix(partial_thinking, recovered.thinking)
events: list[str] = []
if thinking_suffix:
events.extend(ledger.ensure_thinking_block())
@@ -858,6 +900,10 @@ class _OpenAIChatStreamRunner:
if text_suffix:
events.extend(ledger.ensure_text_block())
events.append(ledger.emit_text_delta(text_suffix))
+ if recovered.tool_calls:
+ events.extend(ledger.close_content_blocks())
+ for tool_call in recovered.tool_calls:
+ events.extend(self._tool_calls.process_tool_call(tool_call, ledger))
if not events:
return None
events.extend(ledger.close_all_blocks())
@@ -913,14 +959,14 @@ class _OpenAIChatStreamRunner:
repair_attempt = 0
while retry_session.can_attempt:
repair_attempt += 1
- text, _ = await self._collect_recovery_text(
+ recovered = await self._collect_recovery_output(
recovery_body,
include_reasoning=False,
retry_session=retry_session,
)
repair = accept_tool_json_repair(
repair_prefix,
- text,
+ recovered.text,
tool_name=state.name,
schemas=schemas,
)
diff --git a/src/free_claude_code/providers/openai_chat/tool_calls.py b/src/free_claude_code/providers/openai_chat/tool_calls.py
index 7c3d7e3e..562a94ee 100644
--- a/src/free_claude_code/providers/openai_chat/tool_calls.py
+++ b/src/free_claude_code/providers/openai_chat/tool_calls.py
@@ -3,17 +3,103 @@
import json
import uuid
from collections.abc import Callable, Iterator
+from dataclasses import dataclass, field
from typing import Any
from free_claude_code.core.anthropic.models import MessagesRequest
from free_claude_code.core.anthropic.streaming import (
AnthropicStreamLedger,
+ parse_complete_tool_input,
tool_schemas_by_name,
)
RecordToolExtraContent = Callable[[str, dict[str, Any]], None]
+@dataclass(slots=True)
+class _CollectedToolCall:
+ index: int
+ tool_id: str | None = None
+ name: str = ""
+ argument_parts: list[str] = field(default_factory=list)
+ extra_content: dict[str, Any] | None = None
+
+
+class OpenAIToolCallCollector:
+ """Collect and validate one buffered OpenAI tool-call response."""
+
+ def __init__(self) -> None:
+ self._calls: dict[int, _CollectedToolCall] = {}
+
+ @property
+ def has_calls(self) -> bool:
+ return bool(self._calls)
+
+ def add(self, tool_call: Any) -> None:
+ """Add one SDK tool-call delta without emitting downstream state."""
+ raw_index = getattr(tool_call, "index", 0)
+ index = raw_index if isinstance(raw_index, int) and raw_index >= 0 else 0
+ state = self._calls.setdefault(index, _CollectedToolCall(index=index))
+
+ tool_id = getattr(tool_call, "id", None)
+ if tool_id:
+ state.tool_id = str(tool_id)
+
+ function = getattr(tool_call, "function", None)
+ incoming_name = getattr(function, "name", None)
+ if isinstance(incoming_name, str) and incoming_name:
+ state.name = _merge_tool_name(state.name, incoming_name)
+
+ arguments = getattr(function, "arguments", None)
+ if isinstance(arguments, str) and arguments:
+ state.argument_parts.append(arguments)
+
+ extra_content = tool_call_extra_content(tool_call)
+ if extra_content:
+ state.extra_content = extra_content
+
+ def completed_calls(
+ self,
+ request: MessagesRequest,
+ *,
+ tool_argument_aliases: dict[str, dict[str, str]] | None = None,
+ ) -> tuple[dict[str, Any], ...] | None:
+ """Return complete schema-valid calls, or None when output is incomplete."""
+ schemas = tool_schemas_by_name(request)
+ completed: list[dict[str, Any]] = []
+ for index in sorted(self._calls):
+ state = self._calls[index]
+ name = state.name.strip()
+ if not name or name not in schemas:
+ return None
+ arguments = "".join(state.argument_parts)
+ aliases = (
+ tool_argument_aliases.get(name, {})
+ if tool_argument_aliases is not None
+ else {}
+ )
+ if aliases:
+ restored = restore_tool_argument_aliases(arguments, aliases)
+ if restored is None:
+ return None
+ arguments = restored
+ if parse_complete_tool_input(arguments, name, schemas) is None:
+ return None
+
+ call: dict[str, Any] = {
+ "index": index,
+ "id": state.tool_id,
+ "function": {
+ "name": name,
+ "arguments": arguments,
+ },
+ }
+ if state.extra_content:
+ call["extra_content"] = state.extra_content
+ completed.append(call)
+ return tuple(completed)
+
+
def iter_heuristic_tool_use_sse(
ledger: AnthropicStreamLedger, tool_use: dict[str, Any]
) -> Iterator[str]:
@@ -252,34 +338,46 @@ class OpenAIToolCallAssembler:
def _restore_aliased_tool_arguments(
self, argument_json: str, aliases: dict[str, str]
) -> str | None:
- try:
- parsed = json.loads(argument_json)
- except json.JSONDecodeError:
- return None
- if not isinstance(parsed, dict):
- return argument_json
- restored = self._restore_aliased_tool_argument_value(parsed, aliases)
- return json.dumps(restored)
-
- def _restore_aliased_tool_argument_value(
- self, value: Any, aliases: dict[str, str]
- ) -> Any:
- if isinstance(value, dict):
- return {
- aliases.get(key, key): self._restore_aliased_tool_argument_value(
- item, aliases
- )
- for key, item in value.items()
- }
- if isinstance(value, list):
- return [
- self._restore_aliased_tool_argument_value(item, aliases)
- for item in value
- ]
- return value
+ return restore_tool_argument_aliases(argument_json, aliases)
def _record_tool_call_extra_content(
self, tool_call_id: str, extra_content: dict[str, Any]
) -> None:
if self._record_extra_content is not None:
self._record_extra_content(tool_call_id, extra_content)
+
+
+def restore_tool_argument_aliases(
+ argument_json: str,
+ aliases: dict[str, str],
+) -> str | None:
+ """Restore provider-private argument aliases in one complete JSON object."""
+ try:
+ parsed = json.loads(argument_json)
+ except json.JSONDecodeError:
+ return None
+ if not isinstance(parsed, dict):
+ return argument_json
+ return json.dumps(_restore_tool_argument_alias_value(parsed, aliases))
+
+
+def _restore_tool_argument_alias_value(
+ value: Any,
+ aliases: dict[str, str],
+) -> Any:
+ if isinstance(value, dict):
+ return {
+ aliases.get(key, key): _restore_tool_argument_alias_value(item, aliases)
+ for key, item in value.items()
+ }
+ if isinstance(value, list):
+ return [_restore_tool_argument_alias_value(item, aliases) for item in value]
+ return value
+
+
+def _merge_tool_name(existing: str, incoming: str) -> str:
+ if not existing or incoming.startswith(existing):
+ return incoming
+ if existing.startswith(incoming):
+ return existing
+ return "".join((existing, incoming))
diff --git a/src/free_claude_code/providers/stream_recovery.py b/src/free_claude_code/providers/stream_recovery.py
index 7d79ebe6..3fafd0a8 100644
--- a/src/free_claude_code/providers/stream_recovery.py
+++ b/src/free_claude_code/providers/stream_recovery.py
@@ -177,7 +177,7 @@ class RecoveryController:
def is_retryable_stream_error(exc: BaseException) -> bool:
"""Return whether one stream failure qualifies for retry or recovery."""
- if isinstance(exc, TruncatedProviderStreamError):
+ if isinstance(exc, RetryableProviderProtocolError):
return True
if isinstance(exc, ExecutionFailure):
return exc.retryable
diff --git a/tests/core/anthropic/test_stream_repair.py b/tests/core/anthropic/test_stream_repair.py
index eca8b102..9b42c665 100644
--- a/tests/core/anthropic/test_stream_repair.py
+++ b/tests/core/anthropic/test_stream_repair.py
@@ -4,6 +4,7 @@ from free_claude_code.core.anthropic.streaming import (
ToolSchema,
accept_tool_json_repair,
continuation_suffix,
+ make_response_recovery_body,
make_text_recovery_body,
make_tool_repair_body,
)
@@ -57,6 +58,7 @@ def test_recovery_bodies_do_not_own_transport_flags() -> None:
}
text_body = make_text_recovery_body(body, "partial")
+ response_body = make_response_recovery_body(body, "partial")
tool_body = make_tool_repair_body(
body,
tool_name="Echo",
@@ -65,6 +67,9 @@ def test_recovery_bodies_do_not_own_transport_flags() -> None:
)
assert "stream" not in text_body
+ assert "stream" not in response_body
assert "stream" not in tool_body
assert "tools" not in text_body
+ assert response_body["tools"] == body["tools"]
+ assert response_body["tool_choice"] == body["tool_choice"]
assert "tools" not in tool_body
diff --git a/tests/providers/test_nvidia_nim.py b/tests/providers/test_nvidia_nim.py
index bb36a699..7404f8df 100644
--- a/tests/providers/test_nvidia_nim.py
+++ b/tests/providers/test_nvidia_nim.py
@@ -14,6 +14,7 @@ from free_claude_code.providers.nvidia_nim import NvidiaNimProvider
from free_claude_code.providers.nvidia_nim.tool_schema import (
NIM_TOOL_ARGUMENT_ALIASES_KEY,
)
+from free_claude_code.providers.stream_recovery import RecoveryHoldbackBuffer
from tests.providers.request_factory import make_messages_request
from tests.providers.support import (
REASONING_OFF,
@@ -81,6 +82,27 @@ def _tool_call_chunk(
return mock_chunk
+def _content_chunk(
+ content,
+ *,
+ finish_reason=None,
+ reasoning_content=None,
+):
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content=content,
+ reasoning_content=reasoning_content,
+ tool_calls=None,
+ ),
+ finish_reason=finish_reason,
+ )
+ ]
+ mock_chunk.usage = None
+ return mock_chunk
+
+
def _make_bad_request_error(message: str) -> openai.BadRequestError:
response = Response(
status_code=400,
@@ -553,6 +575,213 @@ async def test_tool_call_stream(nim_provider):
assert "search" in starts[0]
+async def test_native_minimax_tool_markup_becomes_anthropic_tool_use(nim_provider):
+ req = make_request(
+ tools=[
+ tool(
+ "Read",
+ "Read one file",
+ {
+ "type": "object",
+ "properties": {"file_path": {"type": "string"}},
+ "required": ["file_path"],
+ },
+ )
+ ]
+ )
+ namespace = "]<]minimax[>["
+ raw = (
+ "I will read it."
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="Read">'
+ f"{namespace}<file_path>README.md{namespace}</file_path>"
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ )
+
+ async def mock_stream():
+ yield _content_chunk(raw, finish_reason="stop")
+
+ with patch.object(
+ nim_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in nim_provider.stream_response(req)]
+
+ event_text = "".join(events)
+ assert namespace not in event_text
+ assert "I will read it." in event_text
+ assert '"type": "tool_use"' in event_text
+ assert '"name": "Read"' in event_text
+ assert json.loads(_input_json_deltas(events)[0]) == {"file_path": "README.md"}
+
+
+async def test_native_minimax_tool_markup_restores_nim_argument_aliases(nim_provider):
+ req = make_request(
+ tools=[
+ tool(
+ "Grep",
+ "Search file contents",
+ {
+ "type": "object",
+ "properties": {
+ "pattern": {"type": "string"},
+ "type": {"type": "string"},
+ },
+ "required": ["pattern", "type"],
+ },
+ )
+ ]
+ )
+ namespace = "]<]minimax[>["
+ raw = (
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="Grep">'
+ f"{namespace}<pattern>needle{namespace}</pattern>"
+ f"{namespace}<_fcc_arg_type>py{namespace}</_fcc_arg_type>"
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ )
+
+ async def mock_stream():
+ yield _content_chunk(raw, finish_reason="tool_calls")
+
+ with patch.object(
+ nim_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in nim_provider.stream_response(req)]
+
+ assert json.loads(_input_json_deltas(events)[0]) == {
+ "pattern": "needle",
+ "type": "py",
+ }
+
+
+async def test_malformed_native_minimax_tool_call_retries_without_leaking(
+ nim_provider,
+):
+ req = make_request(
+ tools=[
+ tool(
+ "Read",
+ "Read one file",
+ {
+ "type": "object",
+ "properties": {"file_path": {"type": "string"}},
+ "required": ["file_path"],
+ },
+ )
+ ]
+ )
+ namespace = "]<]minimax[>["
+ malformed = (
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="">'
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ )
+ recovered = (
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="Read">'
+ f"{namespace}<file_path>README.md{namespace}</file_path>"
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ )
+
+ async def malformed_stream():
+ yield _content_chunk(malformed, finish_reason="stop")
+
+ async def recovered_stream():
+ yield _content_chunk(recovered, finish_reason="tool_calls")
+
+ with patch.object(
+ nim_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [malformed_stream(), recovered_stream()]
+
+ events = [event async for event in nim_provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+ event_text = "".join(events)
+ assert namespace not in event_text
+ assert '"name": "Read"' in event_text
+ assert json.loads(_input_json_deltas(events)[0]) == {"file_path": "README.md"}
+
+
+async def test_midstream_native_tool_suffix_failure_recovers_without_duplication(
+ nim_provider,
+):
+ req = make_request(
+ tools=[
+ tool(
+ "Read",
+ "Read one file",
+ {
+ "type": "object",
+ "properties": {"file_path": {"type": "string"}},
+ "required": ["file_path"],
+ },
+ )
+ ]
+ )
+ namespace = "]<]minimax[>["
+ malformed = (
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="Read">'
+ f"{namespace}<file_path>IGNORED.md{namespace}</file_path>"
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ "unexpected trailing content"
+ )
+ recovered = (
+ f"{namespace}<tool_call>"
+ f'{namespace}<invoke name="Read">'
+ f"{namespace}<file_path>README.md{namespace}</file_path>"
+ f"{namespace}</invoke>"
+ f"{namespace}</tool_call>"
+ )
+
+ async def malformed_stream():
+ yield _content_chunk("Starting the read. ")
+ yield _content_chunk(malformed, finish_reason="stop")
+
+ async def recovered_stream():
+ yield _content_chunk(recovered, finish_reason="tool_calls")
+
+ def immediate_holdback():
+ return RecoveryHoldbackBuffer(holdback_seconds=0.0)
+
+ with (
+ patch.object(
+ nim_provider._client.chat.completions,
+ "create",
+ new_callable=AsyncMock,
+ ) as mock_create,
+ patch(
+ "free_claude_code.providers.stream_recovery.RecoveryHoldbackBuffer",
+ side_effect=immediate_holdback,
+ ),
+ ):
+ mock_create.side_effect = [malformed_stream(), recovered_stream()]
+
+ events = [event async for event in nim_provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+ event_text = "".join(events)
+ assert namespace not in event_text
+ assert event_text.count("Starting the read.") == 1
+ assert '"name": "Read"' in event_text
+ assert json.loads(_input_json_deltas(events)[0]) == {"file_path": "README.md"}
+ assert "event: error" not in event_text
+
+
@pytest.mark.asyncio
async def test_stream_response_restores_aliased_tool_arguments(nim_provider):
"""NIM-safe argument aliases are restored before Anthropic SSE emission."""
diff --git a/tests/providers/test_nvidia_nim_native_tool_stream.py b/tests/providers/test_nvidia_nim_native_tool_stream.py
new file mode 100644
index 00000000..c4ae1582
--- /dev/null
+++ b/tests/providers/test_nvidia_nim_native_tool_stream.py
@@ -0,0 +1,475 @@
+import json
+from types import SimpleNamespace
+
+import pytest
+
+from free_claude_code.providers.nvidia_nim import native_tool_stream
+from free_claude_code.providers.nvidia_nim.native_tool_stream import (
+ NimNativeToolProtocolError,
+ normalize_nim_native_tool_stream,
+)
+
+NS = "]<]minimax[>["
+TOOL_START = f"{NS}<tool_call>"
+TOOL_END = f"{NS}</tool_call>"
+INVOKE_END = f"{NS}</invoke>"
+
+
+def _element(name: str, value: str) -> str:
+ return f"{NS}<{name}>{value}{NS}</{name}>"
+
+
+def _invoke(name: str, arguments: str) -> str:
+ return f'{NS}<invoke name="{name}">{arguments}{INVOKE_END}'
+
+
+def _tool_block(*invokes: str) -> str:
+ return f"{TOOL_START}\n{'\n'.join(invokes)}\n{TOOL_END}"
+
+
+def _chunk(
+ *,
+ content: str | None = None,
+ tool_calls: list[object] | None = None,
+ finish_reason: str | None = None,
+) -> SimpleNamespace:
+ return SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(
+ content=content,
+ reasoning_content=None,
+ tool_calls=tool_calls,
+ ),
+ finish_reason=finish_reason,
+ )
+ ],
+ usage=None,
+ )
+
+
+def _structured_call(name: str, arguments: str) -> SimpleNamespace:
+ return SimpleNamespace(
+ index=0,
+ id="call_structured",
+ function=SimpleNamespace(name=name, arguments=arguments),
+ )
+
+
+async def _stream(chunks: list[SimpleNamespace]):
+ for chunk in chunks:
+ yield chunk
+
+
+async def _normalize(
+ chunks: list[SimpleNamespace], body: dict
+) -> list[SimpleNamespace]:
+ return [
+ chunk async for chunk in normalize_nim_native_tool_stream(_stream(chunks), body)
+ ]
+
+
+def _body(*functions: dict) -> dict:
+ return {
+ "tools": [
+ {
+ "type": "function",
+ "function": function,
+ }
+ for function in functions
+ ]
+ }
+
+
+def _function(name: str, properties: dict, required: list[str]) -> dict:
+ return {
+ "name": name,
+ "parameters": {
+ "type": "object",
+ "properties": properties,
+ "required": required,
+ },
+ }
+
+
+def _content(chunks: list[SimpleNamespace]) -> str:
+ return "".join(
+ content
+ for chunk in chunks
+ if (content := getattr(chunk.choices[0].delta, "content", None)) is not None
+ )
+
+
+def _tool_calls(chunks: list[SimpleNamespace]) -> list[SimpleNamespace]:
+ calls: list[SimpleNamespace] = []
+ for chunk in chunks:
+ value = getattr(chunk.choices[0].delta, "tool_calls", None)
+ if isinstance(value, list):
+ calls.extend(call for call in value if isinstance(call, SimpleNamespace))
+ return calls
+
+
+def _nested_argument(depth: int) -> tuple[str, dict]:
+ value = "leaf"
+ schema: dict = {"type": "string"}
+ for index in reversed(range(depth)):
+ name = f"level_{index}"
+ value = _element(name, value)
+ schema = {
+ "type": "object",
+ "properties": {name: schema},
+ "required": [name],
+ }
+ return value, schema
+
+
+async def test_normalizer_preserves_ordinary_text_incrementally() -> None:
+ chunks = [
+ _chunk(content="Hello "),
+ _chunk(content="world", finish_reason="stop"),
+ ]
+
+ normalized = await _normalize(chunks, _body())
+
+ assert _content(normalized) == "Hello world"
+ assert not _tool_calls(normalized)
+ assert normalized[-1].choices[0].finish_reason == "stop"
+
+
+async def test_normalizer_decodes_tool_block_split_at_every_character() -> None:
+ raw = "Let me check. " + _tool_block(
+ _invoke(
+ "get_weather",
+ _element("city", "Seattle") + _element("days", "5"),
+ )
+ )
+ chunks = [_chunk(content=character) for character in raw]
+ chunks.append(_chunk(finish_reason="stop"))
+ body = _body(
+ _function(
+ "get_weather",
+ {
+ "city": {"type": "string"},
+ "days": {"type": "integer"},
+ },
+ ["city", "days"],
+ )
+ )
+
+ normalized = await _normalize(chunks, body)
+
+ assert _content(normalized) == "Let me check. "
+ assert NS not in _content(normalized)
+ calls = _tool_calls(normalized)
+ assert len(calls) == 1
+ assert calls[0].index == 0
+ assert calls[0].function.name == "get_weather"
+ assert json.loads(calls[0].function.arguments) == {
+ "city": "Seattle",
+ "days": 5,
+ }
+ assert normalized[-1].choices[0].finish_reason == "stop"
+
+
+async def test_normalizer_decodes_parallel_recursive_arguments_in_order() -> None:
+ shipping = _element(
+ "shipping",
+ _element("city", "Singapore") + _element("zip", "018956"),
+ )
+ items = _element(
+ "items",
+ _element(
+ "item",
+ _element("sku", "book-001") + _element("qty", "2"),
+ )
+ + _element(
+ "item",
+ _element("sku", "pen-007") + _element("qty", "5"),
+ ),
+ )
+ raw = _tool_block(
+ _invoke(
+ "create_order",
+ _element("urgent", "true") + shipping + items,
+ ),
+ _invoke("ping", _element("message", "ready")),
+ )
+ body = _body(
+ _function(
+ "create_order",
+ {
+ "urgent": {"type": "boolean"},
+ "shipping": {
+ "type": "object",
+ "properties": {
+ "city": {"type": "string"},
+ "zip": {"type": "integer"},
+ },
+ "required": ["city", "zip"],
+ },
+ "items": {
+ "type": "array",
+ "items": {
+ "type": "object",
+ "properties": {
+ "sku": {"type": "string"},
+ "qty": {"type": "integer"},
+ },
+ "required": ["sku", "qty"],
+ },
+ },
+ },
+ ["urgent", "shipping", "items"],
+ ),
+ _function("ping", {"message": {"type": "string"}}, ["message"]),
+ )
+
+ normalized = await _normalize(
+ [_chunk(content=raw, finish_reason="tool_calls")],
+ body,
+ )
+
+ calls = _tool_calls(normalized)
+ assert [call.index for call in calls] == [0, 1]
+ assert [call.function.name for call in calls] == ["create_order", "ping"]
+ assert json.loads(calls[0].function.arguments) == {
+ "urgent": True,
+ "shipping": {"city": "Singapore", "zip": 18956},
+ "items": [
+ {"sku": "book-001", "qty": 2},
+ {"sku": "pen-007", "qty": 5},
+ ],
+ }
+ assert json.loads(calls[1].function.arguments) == {"message": "ready"}
+
+
+async def test_normalizer_prefers_structured_calls_and_only_strips_raw_duplicate() -> (
+ None
+):
+ raw = "Checking. " + _tool_block(f'{NS}<invoke name="">{INVOKE_END}')
+ structured = _structured_call("Read", '{"file_path":"README.md"}')
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+
+ normalized = await _normalize(
+ [
+ _chunk(content=raw, tool_calls=[structured]),
+ _chunk(finish_reason="tool_calls"),
+ ],
+ body,
+ )
+
+ assert _content(normalized) == "Checking. "
+ assert _tool_calls(normalized) == [structured]
+ assert NS not in _content(normalized)
+
+
+async def test_normalizer_preserves_literal_xml_like_string_content() -> None:
+ literal = 'C:\\work\\a<b & "quoted">.txt'
+ raw = _tool_block(_invoke("Read", _element("file_path", literal)))
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+
+ normalized = await _normalize(
+ [_chunk(content=raw, finish_reason="tool_calls")],
+ body,
+ )
+
+ calls = _tool_calls(normalized)
+ assert json.loads(calls[0].function.arguments) == {"file_path": literal}
+
+
+ "raw",
+ [
+ TOOL_START,
+ f'{TOOL_START}{NS}<invoke name="Read">',
+ f'{TOOL_START}{NS}<invoke name="">{INVOKE_END}{TOOL_END}',
+ _tool_block(_invoke("Unknown", _element("path", "x"))),
+ _tool_block(_invoke("Read", _element("wrong", "x"))),
+ _tool_block(
+ _invoke(
+ "Read",
+ f"{NS}<file_path>x{NS}</wrong>",
+ )
+ ),
+ ],
+)
+async def test_normalizer_rejects_incomplete_or_invalid_native_calls(raw: str) -> None:
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+
+ with pytest.raises(NimNativeToolProtocolError):
+ await _normalize([_chunk(content=raw, finish_reason="stop")], body)
+
+
+async def test_normalizer_enforces_native_tool_block_size(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ invoke = _invoke("Read", _element("file_path", "README.md"))
+ block_body = f"\n{invoke}\n"
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+ monkeypatch.setattr(
+ native_tool_stream,
+ "_MAX_NATIVE_TOOL_BLOCK_CHARS",
+ len(block_body),
+ )
+
+ normalized = await _normalize(
+ [
+ _chunk(
+ content=f"{TOOL_START}{block_body}{TOOL_END}",
+ finish_reason="tool_calls",
+ )
+ ],
+ body,
+ )
+
+ assert len(_tool_calls(normalized)) == 1
+ with pytest.raises(NimNativeToolProtocolError, match="oversized"):
+ await _normalize(
+ [
+ _chunk(
+ content=f"{TOOL_START}{block_body}x{TOOL_END}",
+ finish_reason="tool_calls",
+ )
+ ],
+ body,
+ )
+
+
[email protected]("split_suffix", [False, True])
+async def test_normalizer_accepts_whitespace_after_native_tool_block(
+ split_suffix: bool,
+) -> None:
+ raw = _tool_block(_invoke("Read", _element("file_path", "README.md")))
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+ suffix = "\n \t"
+ chunks = (
+ [_chunk(content=raw), _chunk(content=suffix, finish_reason="tool_calls")]
+ if split_suffix
+ else [_chunk(content=f"{raw}{suffix}", finish_reason="tool_calls")]
+ )
+
+ normalized = await _normalize(chunks, body)
+
+ assert len(_tool_calls(normalized)) == 1
+
+
[email protected]("split_suffix", [False, True])
+ "suffix",
+ [
+ "unexpected",
+ _tool_block(_invoke("Read", _element("file_path", "SECOND.md"))),
+ ],
+)
+async def test_normalizer_rejects_content_after_native_tool_block(
+ suffix: str,
+ split_suffix: bool,
+) -> None:
+ raw = _tool_block(_invoke("Read", _element("file_path", "README.md")))
+ body = _body(
+ _function(
+ "Read",
+ {"file_path": {"type": "string"}},
+ ["file_path"],
+ )
+ )
+ chunks = (
+ [_chunk(content=raw), _chunk(content=suffix, finish_reason="tool_calls")]
+ if split_suffix
+ else [_chunk(content=f"{raw}{suffix}", finish_reason="tool_calls")]
+ )
+
+ with pytest.raises(NimNativeToolProtocolError, match="content after"):
+ await _normalize(chunks, body)
+
+
+async def test_normalizer_accepts_native_argument_nesting_at_limit(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ depth = 3
+ arguments, parameters = _nested_argument(depth)
+ monkeypatch.setattr(
+ native_tool_stream,
+ "_MAX_NATIVE_ARGUMENT_DEPTH",
+ depth,
+ )
+ raw = _tool_block(_invoke("deep_tool", arguments))
+ body = _body(
+ {
+ "name": "deep_tool",
+ "parameters": parameters,
+ }
+ )
+
+ normalized = await _normalize(
+ [_chunk(content=raw, finish_reason="tool_calls")],
+ body,
+ )
+
+ assert len(_tool_calls(normalized)) == 1
+
+
+async def test_normalizer_rejects_native_argument_nesting_above_limit(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ limit = 3
+ arguments, parameters = _nested_argument(limit + 1)
+ monkeypatch.setattr(
+ native_tool_stream,
+ "_MAX_NATIVE_ARGUMENT_DEPTH",
+ limit,
+ )
+ raw = _tool_block(_invoke("deep_tool", arguments))
+ body = _body(
+ {
+ "name": "deep_tool",
+ "parameters": parameters,
+ }
+ )
+
+ with pytest.raises(NimNativeToolProtocolError, match="excessively nested"):
+ await _normalize(
+ [_chunk(content=raw, finish_reason="tool_calls")],
+ body,
+ )
diff --git a/tests/providers/test_streaming_errors.py b/tests/providers/test_streaming_errors.py
index b84236e1..68443fed 100644
--- a/tests/providers/test_streaming_errors.py
+++ b/tests/providers/test_streaming_errors.py
@@ -14,6 +14,7 @@ from free_claude_code.core.anthropic.stream_contracts import (
)
from free_claude_code.core.anthropic.streaming import (
AnthropicStreamLedger,
+ make_response_recovery_body,
make_text_recovery_body,
)
from free_claude_code.core.failures import ExecutionFailure
@@ -51,6 +52,14 @@ class AsyncStreamMock:
raise self._error
+def _recovery_output(
+ text: str = "",
+ thinking: str = "",
+ tool_calls: tuple[dict, ...] = (),
+) -> SimpleNamespace:
+ return SimpleNamespace(text=text, thinking=thinking, tool_calls=tool_calls)
+
+
class ClosableAsyncStreamMock(AsyncStreamMock):
"""Async stream mock that records cleanup."""
@@ -817,9 +826,9 @@ class TestStreamingExceptionHandling:
),
patch.object(
_OpenAIChatStreamRunner,
- "_collect_recovery_text",
+ "_collect_recovery_output",
new_callable=AsyncMock,
- return_value=("world", ""),
+ return_value=_recovery_output(text="world"),
),
):
events = await _collect_stream(provider, request)
@@ -898,7 +907,7 @@ class TestStreamingExceptionHandling:
) as create,
pytest.raises(TruncatedProviderStreamError),
):
- await runner._collect_recovery_text(
+ await runner._collect_recovery_output(
{"messages": []},
include_reasoning=True,
retry_session=retry_session,
@@ -930,7 +939,7 @@ class TestStreamingExceptionHandling:
) as create,
pytest.raises(TimeoutError),
):
- await runner._collect_recovery_text(
+ await runner._collect_recovery_output(
{"messages": []},
include_reasoning=True,
retry_session=retry_session,
@@ -958,13 +967,15 @@ class TestStreamingExceptionHandling:
new_callable=AsyncMock,
return_value=stream,
):
- result = await runner._collect_recovery_text(
+ result = await runner._collect_recovery_output(
{"messages": []},
include_reasoning=True,
retry_session=retry_session,
)
- assert result == ("world", "")
+ assert result.text == "world"
+ assert result.thinking == ""
+ assert result.tool_calls == ()
assert stream.closed is True
@pytest.mark.asyncio
@@ -1000,13 +1011,15 @@ class TestStreamingExceptionHandling:
new_callable=AsyncMock,
side_effect=[rejected, recovered],
) as create:
- result = await runner._collect_recovery_text(
+ result = await runner._collect_recovery_output(
{"messages": []},
include_reasoning=True,
retry_session=retry_session,
)
- assert result == ("world", "")
+ assert result.text == "world"
+ assert result.thinking == ""
+ assert result.tool_calls == ()
assert create.await_count == 2
assert rejected.closed
assert recovered.closed
@@ -1037,6 +1050,22 @@ class TestStreamingExceptionHandling:
assert "hidden reasoning" in recovery_prompt["content"]
assert "reasoning_content" not in recovery_prompt
+ def test_tool_protocol_recovery_body_retains_tool_contract(self):
+ body = {
+ "messages": [{"role": "user", "content": "hello"}],
+ "tools": [{"type": "function", "function": {"name": "Read"}}],
+ "tool_choice": {"type": "function", "function": {"name": "Read"}},
+ }
+
+ recovery_body = make_response_recovery_body(body, "visible answer")
+
+ assert recovery_body["tools"] == body["tools"]
+ assert recovery_body["tool_choice"] == body["tool_choice"]
+ assert recovery_body["messages"][-2] == {
+ "role": "assistant",
+ "content": "visible answer",
+ }
+
@pytest.mark.asyncio
async def test_openai_text_recovery_passes_thinking_context(self):
"""OpenAI-chat recovery call sites seed emitted thinking in the prompt."""
@@ -1051,9 +1080,12 @@ class TestStreamingExceptionHandling:
with patch.object(
runner,
- "_collect_recovery_text",
+ "_collect_recovery_output",
new_callable=AsyncMock,
- return_value=("visible answer done", "hidden reasoning more"),
+ return_value=_recovery_output(
+ text="visible answer done",
+ thinking="hidden reasoning more",
+ ),
) as mock_collect:
retry_session = runner._provider._admission.new_retry_session()
events = await runner._recovery_events(
@@ -1109,7 +1141,7 @@ class TestStreamingExceptionHandling:
) as mock_create,
patch.object(
_OpenAIChatStreamRunner,
- "_collect_recovery_text",
+ "_collect_recovery_output",
new_callable=AsyncMock,
side_effect=TruncatedProviderStreamError(
"Recovery stream ended without finish_reason."
@@ -1167,9 +1199,9 @@ class TestStreamingExceptionHandling:
),
patch.object(
_OpenAIChatStreamRunner,
- "_collect_recovery_text",
+ "_collect_recovery_output",
new_callable=AsyncMock,
- return_value=('"ok"}', ""),
+ return_value=_recovery_output(text='"ok"}'),
),
):
events = await _collect_stream(provider, request)
diff --git a/uv.lock b/uv.lock
index 6ad556d1..27b6344f 100644
--- a/uv.lock
+++ b/uv.lock
@@ -620,7 +620,7 @@ wheels = [
[[package]]
name = "free-claude-code"
-version = "4.12.1"
+version = "4.12.2"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },