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

Add Mistral reasoning fallback

Ali Khokhar62c0480eed32
diff --git a/.env.example b/.env.example
index c9119e8a..fe5910eb 100644
--- a/.env.example
+++ b/.env.example
@@ -105,6 +105,7 @@ MODEL="nvidia_nim/nvidia/nemotron-3-super-120b-a12b"
FCC_SMOKE_MODEL_NVIDIA_NIM=
FCC_SMOKE_MODEL_OPEN_ROUTER=
FCC_SMOKE_MODEL_MISTRAL=
+FCC_SMOKE_MODEL_MISTRAL_REASONING=
FCC_SMOKE_MODEL_MISTRAL_CODESTRAL=
FCC_SMOKE_MODEL_DEEPSEEK=
FCC_SMOKE_MODEL_LMSTUDIO=
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index a64b7799..4fc1d5ed 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -361,6 +361,10 @@ account-scoped Workers AI OpenAI-compatible Chat Completions endpoint for
Cloudflare-specific reasoning deltas stay in the Cloudflare provider client.
MiniMax uses its Anthropic-compatible Messages endpoint; its provider client owns
the MiniMax-specific `adaptive` thinking request shape.
+Mistral La Plateforme keeps its native `reasoning_effort` and thinking-chunk
+request/stream mapping inside
+[providers/mistral/reasoning.py](providers/mistral/reasoning.py), including its
+fallback retry when a selected Mistral model 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.
diff --git a/providers/mistral/client.py b/providers/mistral/client.py
index cb5a75d0..e9e75c8f 100644
--- a/providers/mistral/client.py
+++ b/providers/mistral/client.py
@@ -2,6 +2,8 @@
from typing import Any
+from loguru import logger
+
from providers.base import ProviderConfig
from providers.defaults import MISTRAL_DEFAULT_BASE
from providers.transports.openai_chat import (
@@ -10,6 +12,13 @@ from providers.transports.openai_chat import (
build_openai_chat_request_body,
)
+from .reasoning import (
+ apply_mistral_reasoning_request_shape,
+ clone_body_without_mistral_reasoning,
+ is_mistral_reasoning_rejection,
+ normalize_mistral_stream,
+)
+
_REQUEST_POLICY = OpenAIChatRequestPolicy(provider_name="MISTRAL")
@@ -27,8 +36,31 @@ class MistralProvider(OpenAIChatTransport):
def _build_request_body(
self, request: Any, thinking_enabled: bool | None = None
) -> dict:
- return build_openai_chat_request_body(
+ effective_thinking_enabled = self._is_thinking_enabled(
+ request, thinking_enabled
+ )
+ body = build_openai_chat_request_body(
request,
- thinking_enabled=self._is_thinking_enabled(request, thinking_enabled),
+ thinking_enabled=effective_thinking_enabled,
policy=_REQUEST_POLICY,
)
+ apply_mistral_reasoning_request_shape(
+ body, thinking_enabled=effective_thinking_enabled
+ )
+ return body
+
+ def _get_retry_request_body(self, error: Exception, body: dict) -> dict | None:
+ """Retry once without Mistral reasoning fields when a model rejects them."""
+ if not is_mistral_reasoning_rejection(error):
+ return None
+ retry_body = clone_body_without_mistral_reasoning(body)
+ if retry_body is None:
+ return None
+ logger.warning(
+ "MISTRAL_STREAM: retrying without reasoning after upstream rejection"
+ )
+ return retry_body
+
+ async def _create_stream(self, body: dict) -> tuple[Any, dict]:
+ stream, final_body = await super()._create_stream(body)
+ return normalize_mistral_stream(stream), final_body
diff --git a/providers/mistral/reasoning.py b/providers/mistral/reasoning.py
new file mode 100644
index 00000000..7112a8f8
--- /dev/null
+++ b/providers/mistral/reasoning.py
@@ -0,0 +1,408 @@
+"""Mistral La Plateforme reasoning compatibility helpers."""
+
+import json
+from collections.abc import AsyncIterator, Mapping, Sequence
+from copy import deepcopy
+from types import SimpleNamespace
+from typing import Any
+
+import openai
+
+from providers.transports.http import maybe_await_aclose
+
+MISTRAL_REASONING_EFFORT = "high"
+
+_REASONING_FIELD_NAMES = frozenset(
+ {
+ "reasoning_content",
+ "reasoning_effort",
+ "thinkchunk",
+ }
+)
+_REJECTION_WORDS = ("unsupported", "unknown", "invalid", "forbidden", "extra")
+
+
+def apply_mistral_reasoning_request_shape(
+ body: dict[str, Any], *, thinking_enabled: bool
+) -> None:
+ """Apply Mistral's native reasoning request shape in-place."""
+ if thinking_enabled:
+ body["reasoning_effort"] = MISTRAL_REASONING_EFFORT
+ else:
+ body.pop("reasoning_effort", None)
+
+ messages = body.get("messages")
+ if not isinstance(messages, list):
+ return
+
+ for message in messages:
+ if not isinstance(message, dict) or message.get("role") != "assistant":
+ continue
+ reasoning = _clean_text(message.pop("reasoning_content", None))
+ if thinking_enabled and reasoning:
+ message["content"] = _content_with_prepended_thinking(
+ message.get("content"), reasoning
+ )
+ elif not thinking_enabled:
+ message["content"] = _content_without_thinking(message.get("content"))
+
+
+def clone_body_without_mistral_reasoning(
+ body: dict[str, Any],
+) -> dict[str, Any] | None:
+ """Return a body clone with Mistral reasoning fields removed."""
+ cloned = deepcopy(body)
+ removed = cloned.pop("reasoning_effort", None) is not None
+
+ messages = cloned.get("messages")
+ if isinstance(messages, list):
+ for message in messages:
+ if not isinstance(message, dict):
+ continue
+ if message.pop("reasoning_content", None) is not None:
+ removed = True
+ content = message.get("content")
+ stripped_content, content_removed = _strip_mistral_thinking_content(content)
+ if content_removed:
+ message["content"] = stripped_content
+ removed = True
+
+ if not removed:
+ return None
+ return cloned
+
+
+def is_mistral_reasoning_rejection(error: Exception) -> bool:
+ """Return whether an upstream error rejects Mistral reasoning request fields."""
+ status_code = getattr(error, "status_code", None)
+ if not isinstance(error, openai.BadRequestError) and status_code not in (400, 422):
+ return False
+
+ error_body = getattr(error, "body", None)
+ if _contains_reasoning_rejection(error_body):
+ return True
+
+ error_text_parts = [str(error)]
+ response = getattr(error, "response", None)
+ if response is not None:
+ try:
+ response_text = response.text
+ except Exception:
+ response_text = None
+ if response_text:
+ if _contains_reasoning_rejection(_json_payload(response_text)):
+ return True
+ error_text_parts.append(response_text)
+
+ if error_body is not None:
+ error_text_parts.append(json.dumps(error_body, default=str))
+
+ return _reasoning_rejection_text(" ".join(error_text_parts))
+
+
+def normalize_mistral_stream(stream: Any) -> AsyncIterator[Any]:
+ """Yield OpenAI-chat chunks with Mistral native thinking chunks normalized."""
+
+ async def _iter() -> AsyncIterator[Any]:
+ try:
+ async for chunk in stream:
+ yield normalize_mistral_chunk(chunk)
+ finally:
+ await maybe_await_aclose(stream)
+
+ return _iter()
+
+
+def normalize_mistral_chunk(chunk: Any) -> Any:
+ """Normalize one Mistral OpenAI-compatible stream chunk."""
+ choices = getattr(chunk, "choices", None)
+ if not choices:
+ return chunk
+
+ normalized_choices: list[Any] = []
+ changed = False
+ for choice in choices:
+ delta = getattr(choice, "delta", None)
+ normalized_delta = _normalize_mistral_delta(delta)
+ if normalized_delta is delta:
+ normalized_choices.append(choice)
+ continue
+ changed = True
+ normalized_choices.append(
+ SimpleNamespace(
+ delta=normalized_delta,
+ finish_reason=getattr(choice, "finish_reason", None),
+ )
+ )
+
+ if not changed:
+ return chunk
+ return SimpleNamespace(
+ choices=normalized_choices,
+ usage=getattr(chunk, "usage", None),
+ )
+
+
+def _normalize_mistral_delta(delta: Any) -> Any:
+ if delta is None:
+ return delta
+
+ content = _field(delta, "content")
+ text, reasoning, changed = _split_mistral_content(content)
+ native_reasoning = _extract_native_reasoning(delta)
+ if native_reasoning:
+ reasoning = "\n".join(part for part in (reasoning, native_reasoning) if part)
+ if isinstance(content, str):
+ text = content
+ changed = True
+
+ existing_reasoning = _field(delta, "reasoning_content")
+ if isinstance(existing_reasoning, str) and existing_reasoning:
+ reasoning = "\n".join(part for part in (existing_reasoning, reasoning) if part)
+
+ if not changed:
+ return delta
+
+ return SimpleNamespace(
+ content=text,
+ reasoning_content=reasoning or None,
+ tool_calls=_field(delta, "tool_calls"),
+ )
+
+
+def _content_with_prepended_thinking(
+ content: Any, reasoning: str
+) -> list[dict[str, Any]]:
+ chunks = [_thinking_chunk(reasoning)]
+ chunks.extend(_content_to_mistral_text_chunks(content))
+ return chunks
+
+
+def _content_without_thinking(content: Any) -> Any:
+ stripped, _ = _strip_mistral_thinking_content(content)
+ return stripped
+
+
+def _strip_mistral_thinking_content(content: Any) -> tuple[Any, bool]:
+ if not _is_sequence(content):
+ return content, False
+
+ text_parts: list[str] = []
+ kept_chunks: list[Any] = []
+ removed = False
+ can_collapse_to_text = True
+
+ for chunk in content:
+ chunk_type = _chunk_type(chunk)
+ if _is_thinking_chunk_type(chunk_type):
+ removed = True
+ continue
+ if chunk_type == "text":
+ text = _chunk_text(chunk)
+ if text:
+ text_parts.append(text)
+ continue
+ kept_chunks.append(chunk)
+ can_collapse_to_text = False
+
+ if can_collapse_to_text:
+ return "".join(text_parts), removed
+ return kept_chunks, removed
+
+
+def _content_to_mistral_text_chunks(content: Any) -> list[dict[str, Any]]:
+ if isinstance(content, str):
+ return [_text_chunk(content)] if content else []
+ if not _is_sequence(content):
+ text = _clean_text(content)
+ return [_text_chunk(text)] if text else []
+
+ chunks: list[dict[str, Any]] = []
+ for chunk in content:
+ chunk_type = _chunk_type(chunk)
+ if _is_thinking_chunk_type(chunk_type):
+ continue
+ text = _chunk_text(chunk)
+ if text:
+ chunks.append(_text_chunk(text))
+ return chunks
+
+
+def _split_mistral_content(content: Any) -> tuple[str | None, str | None, bool]:
+ if not _is_sequence(content):
+ return None, None, False
+
+ text_parts: list[str] = []
+ reasoning_parts: list[str] = []
+ for chunk in content:
+ chunk_type = _chunk_type(chunk)
+ if _is_thinking_chunk_type(chunk_type):
+ reasoning = _chunk_reasoning(chunk)
+ if reasoning:
+ reasoning_parts.append(reasoning)
+ continue
+ text = _chunk_text(chunk)
+ if text:
+ text_parts.append(text)
+
+ return (
+ "".join(text_parts) or None,
+ "".join(reasoning_parts) or None,
+ bool(text_parts or reasoning_parts),
+ )
+
+
+def _extract_native_reasoning(delta: Any) -> str | None:
+ for name in ("thinking", "reasoning"):
+ value = _field(delta, name)
+ text = _chunk_reasoning(value)
+ if text:
+ return text
+ return None
+
+
+def _contains_reasoning_rejection(value: Any) -> bool:
+ if value is None:
+ return False
+ if isinstance(value, Mapping):
+ for key, item in value.items():
+ key_text = str(key).lower()
+ if key_text in _REASONING_FIELD_NAMES:
+ return True
+ if key_text in {"loc", "param"} and _is_reasoning_field_path(item):
+ return True
+ if _contains_reasoning_rejection(item):
+ return True
+ return False
+ if _is_sequence(value):
+ if _is_reasoning_field_path(value):
+ return True
+ return any(_contains_reasoning_rejection(item) for item in value)
+ if isinstance(value, str):
+ return _reasoning_rejection_text(value)
+ return False
+
+
+def _is_reasoning_field_path(value: Any) -> bool:
+ if not _is_sequence(value):
+ return False
+ parts = [str(part).lower() for part in value]
+ if any(part in _REASONING_FIELD_NAMES for part in parts):
+ return True
+ return "thinking" in parts and bool(
+ {"body", "messages", "assistant", "content"} & set(parts)
+ )
+
+
+def _reasoning_rejection_text(value: str) -> bool:
+ lowered = value.lower()
+ if "reasoning input" in lowered and any(
+ phrase in lowered
+ for phrase in (
+ "not enabled",
+ "not supported",
+ "unsupported",
+ "disabled",
+ )
+ ):
+ return True
+ if any(field in lowered for field in _REASONING_FIELD_NAMES):
+ return True
+ if "thinking" not in lowered or not any(
+ word in lowered for word in _REJECTION_WORDS
+ ):
+ return False
+ return any(
+ marker in lowered
+ for marker in (
+ "body.messages",
+ "assistant.thinking",
+ "messages.assistant",
+ "content.thinking",
+ "field: thinking",
+ "field 'thinking'",
+ 'field "thinking"',
+ "property: thinking",
+ "property 'thinking'",
+ 'property "thinking"',
+ "parameter: thinking",
+ "parameter 'thinking'",
+ 'parameter "thinking"',
+ "param: thinking",
+ "param 'thinking'",
+ 'param "thinking"',
+ )
+ )
+
+
+def _json_payload(value: str) -> Any:
+ try:
+ return json.loads(value)
+ except json.JSONDecodeError:
+ return None
+
+
+def _thinking_chunk(reasoning: str) -> dict[str, Any]:
+ return {
+ "type": "thinking",
+ "thinking": [_text_chunk(reasoning)],
+ }
+
+
+def _text_chunk(text: str) -> dict[str, str]:
+ return {"type": "text", "text": text}
+
+
+def _chunk_reasoning(chunk: Any) -> str | None:
+ if isinstance(chunk, str):
+ return chunk or None
+ thinking = _field(chunk, "thinking")
+ if thinking is not None:
+ text = _chunk_text(thinking)
+ if text:
+ return text
+ text = _chunk_text(chunk)
+ return text or None
+
+
+def _chunk_text(chunk: Any) -> str:
+ if isinstance(chunk, str):
+ return chunk
+ if _is_sequence(chunk):
+ return "".join(_chunk_text(part) for part in chunk)
+ text = _field(chunk, "text")
+ if isinstance(text, str):
+ return text
+ content = _field(chunk, "content")
+ if isinstance(content, str):
+ return content
+ return ""
+
+
+def _chunk_type(chunk: Any) -> str | None:
+ value = _field(chunk, "type")
+ if isinstance(value, str):
+ return value
+ return None
+
+
+def _is_thinking_chunk_type(chunk_type: str | None) -> bool:
+ return chunk_type in {"thinking", "reasoning"}
+
+
+def _clean_text(value: Any) -> str | None:
+ if isinstance(value, str):
+ return value if value else None
+ return None
+
+
+def _field(item: Any, name: str) -> Any:
+ if isinstance(item, Mapping):
+ return item.get(name)
+ return getattr(item, name, None)
+
+
+def _is_sequence(value: Any) -> bool:
+ return isinstance(value, Sequence) and not isinstance(
+ value, str | bytes | bytearray
+ )
diff --git a/pyproject.toml b/pyproject.toml
index db759cdd..859e5247 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project]
name = "free-claude-code"
-version = "3.4.3"
+version = "3.4.5"
description = "Middleware between Claude Code CLI (Anthropic API) and NVIDIA NIM"
readme = "README.md"
requires-python = ">=3.14.0"
diff --git a/smoke/README.md b/smoke/README.md
index bcb28094..9ac7030a 100644
--- a/smoke/README.md
+++ b/smoke/README.md
@@ -127,7 +127,8 @@ uv run pytest smoke/product -n 0 -s --tb=short
- `FCC_SMOKE_TARGETS`: comma-separated targets, or `all`.
- `FCC_SMOKE_PROVIDER_MATRIX`: comma-separated provider prefixes to require.
- `FCC_SMOKE_MODEL_NVIDIA_NIM`, `FCC_SMOKE_MODEL_OPEN_ROUTER`,
- `FCC_SMOKE_MODEL_MISTRAL`, `FCC_SMOKE_MODEL_MISTRAL_CODESTRAL`,
+ `FCC_SMOKE_MODEL_MISTRAL`, `FCC_SMOKE_MODEL_MISTRAL_REASONING`,
+ `FCC_SMOKE_MODEL_MISTRAL_CODESTRAL`,
`FCC_SMOKE_MODEL_DEEPSEEK`, `FCC_SMOKE_MODEL_KIMI`,
`FCC_SMOKE_MODEL_WAFER`, `FCC_SMOKE_MODEL_MINIMAX`,
`FCC_SMOKE_MODEL_OPENCODE`, `FCC_SMOKE_MODEL_OPENCODE_GO`,
@@ -137,6 +138,8 @@ uv run pytest smoke/product -n 0 -s --tb=short
`FCC_SMOKE_MODEL_LLAMACPP`, `FCC_SMOKE_MODEL_OLLAMA`: optional per-provider
smoke model overrides. Values may include the provider prefix or just the model
name for that provider.
+- `FCC_SMOKE_MODEL_MISTRAL_REASONING`: optional override for the dedicated
+ Mistral native reasoning smoke, default `mistral/mistral-medium-3-5`.
- `FCC_SMOKE_NIM_MODELS`: optional comma-separated NVIDIA NIM CLI matrix models
that replace the default characterization set.
- `FCC_SMOKE_NIM_EXTRA_MODELS`: optional comma-separated NVIDIA NIM CLI matrix
diff --git a/smoke/lib/config.py b/smoke/lib/config.py
index c44d32a9..ea6c07a8 100644
--- a/smoke/lib/config.py
+++ b/smoke/lib/config.py
@@ -64,6 +64,7 @@ PROVIDER_SMOKE_DEFAULT_MODELS: dict[str, str] = {
"cerebras": "cerebras/llama3.1-8b",
"cloudflare": "cloudflare/@cf/moonshotai/kimi-k2.6",
}
+MISTRAL_REASONING_SMOKE_DEFAULT_MODEL = "mistral/mistral-medium-3-5"
NVIDIA_NIM_CLI_DEFAULT_MODELS: tuple[str, ...] = (
"z-ai/glm-5.2",
@@ -216,6 +217,21 @@ class SmokeConfig:
for full_model, source in openrouter_free_cli_model_refs().items()
]
+ def mistral_reasoning_smoke_model(self) -> ProviderModel | None:
+ """Return a Mistral model expected to accept native reasoning input."""
+ if self.provider_matrix and "mistral" not in self.provider_matrix:
+ return None
+ if not self.has_provider_configuration("mistral"):
+ return None
+ override_env = "FCC_SMOKE_MODEL_MISTRAL_REASONING"
+ if override := os.getenv(override_env):
+ full_model = _normalize_provider_model("mistral", override)
+ source = override_env
+ else:
+ full_model = MISTRAL_REASONING_SMOKE_DEFAULT_MODEL
+ source = "mistral_reasoning_default"
+ return ProviderModel(provider="mistral", full_model=full_model, source=source)
+
def _include_provider_in_smoke(
self, provider: str, mapped_providers: set[str]
) -> bool:
diff --git a/smoke/product/test_provider_product_live.py b/smoke/product/test_provider_product_live.py
index e8bd9668..2a9853fe 100644
--- a/smoke/product/test_provider_product_live.py
+++ b/smoke/product/test_provider_product_live.py
@@ -105,6 +105,30 @@ def test_provider_reasoning_tool_continuation_e2e(
)
+def test_mistral_native_reasoning_model_e2e(smoke_config: SmokeConfig) -> None:
+ provider_model = smoke_config.mistral_reasoning_smoke_model()
+ if provider_model is None:
+ pytest.skip("missing_env: mistral is not configured")
+
+ payload = {
+ "model": "claude-opus-4-7",
+ "max_tokens": 256,
+ "messages": [{"role": "user", "content": "Reply with one short sentence."}],
+ "thinking": {"type": "adaptive"},
+ }
+ with _server_for_provider(
+ smoke_config, provider_model, "mistral-native-reasoning"
+ ) as server:
+ turn = ConversationDriver(server, smoke_config).stream(payload)
+
+ _assert_provider_product_stream(turn.events)
+ event_text = "\n".join(event.raw for event in turn.events)
+ assert "thinking_delta" in event_text, (
+ f"{provider_model.source}={provider_model.full_model} completed without "
+ "native Mistral thinking output"
+ )
+
+
@pytest.mark.smoke_target("rate_limit")
def test_provider_disconnect_e2e(
smoke_config: SmokeConfig, provider_model: ProviderModel
diff --git a/tests/contracts/test_smoke_config.py b/tests/contracts/test_smoke_config.py
index 9d21ce2c..bda5569f 100644
--- a/tests/contracts/test_smoke_config.py
+++ b/tests/contracts/test_smoke_config.py
@@ -9,6 +9,7 @@ from smoke.conftest import (
from smoke.lib.config import (
ALL_TARGETS,
DEFAULT_TARGETS,
+ MISTRAL_REASONING_SMOKE_DEFAULT_MODEL,
NVIDIA_NIM_CLI_DEFAULT_MODELS,
OPENROUTER_FREE_CLI_DEFAULT_MODELS,
OPT_IN_TARGETS,
@@ -337,6 +338,43 @@ def test_provider_smoke_model_override_rejects_wrong_provider_prefix(
raise AssertionError("expected wrong provider prefix to fail")
+def test_mistral_reasoning_smoke_uses_reasoning_default(monkeypatch) -> None:
+ monkeypatch.delenv("FCC_SMOKE_MODEL_MISTRAL_REASONING", raising=False)
+ config = _smoke_config(
+ settings=_settings(mistral_api_key="mistral-key", ollama_base_url="")
+ )
+
+ model = config.mistral_reasoning_smoke_model()
+
+ assert model is not None
+ assert model.provider == "mistral"
+ assert model.full_model == MISTRAL_REASONING_SMOKE_DEFAULT_MODEL
+ assert model.source == "mistral_reasoning_default"
+
+
+def test_mistral_reasoning_smoke_accepts_override(monkeypatch) -> None:
+ monkeypatch.setenv("FCC_SMOKE_MODEL_MISTRAL_REASONING", "mistral-medium-3-5")
+ config = _smoke_config(
+ settings=_settings(mistral_api_key="mistral-key", ollama_base_url="")
+ )
+
+ model = config.mistral_reasoning_smoke_model()
+
+ assert model is not None
+ assert model.full_model == "mistral/mistral-medium-3-5"
+ assert model.source == "FCC_SMOKE_MODEL_MISTRAL_REASONING"
+
+
+def test_mistral_reasoning_smoke_respects_provider_matrix(monkeypatch) -> None:
+ monkeypatch.delenv("FCC_SMOKE_MODEL_MISTRAL_REASONING", raising=False)
+ config = _smoke_config(
+ settings=_settings(mistral_api_key="mistral-key", ollama_base_url=""),
+ provider_matrix=frozenset({"deepseek"}),
+ )
+
+ assert config.mistral_reasoning_smoke_model() is None
+
+
def test_provider_smoke_matrix_filters_provider_catalog(monkeypatch) -> None:
monkeypatch.delenv("FCC_SMOKE_MODEL_DEEPSEEK", raising=False)
config = _smoke_config(
diff --git a/tests/providers/test_mistral.py b/tests/providers/test_mistral.py
index 4dd8bcc1..ac6a8b17 100644
--- a/tests/providers/test_mistral.py
+++ b/tests/providers/test_mistral.py
@@ -1,18 +1,39 @@
"""Tests for Mistral La Plateforme provider."""
from contextlib import asynccontextmanager
+from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock, patch
+import openai
import pytest
+from httpx import Request, Response
from providers.base import ProviderConfig
from providers.mistral import MISTRAL_DEFAULT_BASE, MistralProvider
class MockMessage:
- def __init__(self, role, content):
+ def __init__(self, role, content, reasoning_content=None):
self.role = role
self.content = content
+ self.reasoning_content = reasoning_content
+
+
+class MockBlock:
+ def __init__(self, **kwargs):
+ for key, value in kwargs.items():
+ setattr(self, key, value)
+
+
+class MockTool:
+ def __init__(self):
+ self.name = "echo"
+ self.description = "Echo a value"
+ self.input_schema = {
+ "type": "object",
+ "properties": {"value": {"type": "string"}},
+ "required": ["value"],
+ }
class MockRequest:
@@ -25,6 +46,7 @@ class MockRequest:
self.system = "System prompt"
self.stop_sequences = None
self.tools = []
+ self.tool_choice = None
self.thinking = MagicMock()
self.thinking.enabled = True
for key, value in kwargs.items():
@@ -86,6 +108,60 @@ def test_build_request_body_basic(mistral_provider):
assert body["model"] == "devstral-small-latest"
assert body["messages"][0]["role"] == "system"
+ assert body["reasoning_effort"] == "high"
+
+
+def test_build_request_body_replays_prior_thinking_as_mistral_chunks(
+ mistral_provider,
+):
+ req = MockRequest(
+ system=None,
+ messages=[
+ MockMessage(
+ "assistant",
+ [
+ MockBlock(type="thinking", thinking="Need the tool."),
+ MockBlock(type="text", text="Calling the tool."),
+ MockBlock(
+ type="tool_use",
+ id="toolu_1",
+ name="echo",
+ input={"value": "x"},
+ ),
+ ],
+ )
+ ],
+ )
+
+ body = mistral_provider._build_request_body(req)
+
+ assistant = body["messages"][0]
+ assert "reasoning_content" not in assistant
+ assert assistant["content"] == [
+ {
+ "type": "thinking",
+ "thinking": [{"type": "text", "text": "Need the tool."}],
+ },
+ {"type": "text", "text": "Calling the tool."},
+ ]
+ assert assistant["tool_calls"][0]["id"] == "toolu_1"
+
+
+def test_build_request_body_preserves_tools_tool_choice_and_params(mistral_provider):
+ req = MockRequest(
+ tools=[MockTool()],
+ tool_choice={"type": "tool", "name": "echo"},
+ stop_sequences=["STOP"],
+ )
+
+ body = mistral_provider._build_request_body(req)
+
+ assert body["max_tokens"] == 100
+ assert body["temperature"] == 0.5
+ assert body["top_p"] == 0.9
+ assert body["stop"] == ["STOP"]
+ assert body["tools"][0]["function"]["name"] == "echo"
+ assert body["tool_choice"] == {"type": "function", "function": {"name": "echo"}}
def test_build_request_body_global_disable_blocks_reasoning_mapping():
@@ -102,8 +178,37 @@ def test_build_request_body_global_disable_blocks_reasoning_mapping():
req = MockRequest()
body = provider._build_request_body(req)
- roles = [m.get("role") for m in body.get("messages", [])]
- assert "assistant_reasoning_content" not in roles
+ assert "reasoning_effort" not in body
+ assert all("reasoning_content" not in m for m in body.get("messages", []))
+
+
+def test_build_request_body_thinking_disabled_strips_prior_mistral_thinking():
+ provider = MistralProvider(
+ ProviderConfig(
+ api_key="test_mistral_key",
+ base_url=MISTRAL_DEFAULT_BASE,
+ rate_limit=10,
+ rate_window=60,
+ enable_thinking=False,
+ )
+ )
+ req = MockRequest(
+ system=None,
+ messages=[
+ MockMessage(
+ "assistant",
+ [
+ MockBlock(type="thinking", thinking="Hidden."),
+ MockBlock(type="text", text="Visible."),
+ ],
+ )
+ ],
+ )
+
+ body = provider._build_request_body(req)
+
+ assert "reasoning_effort" not in body
+ assert body["messages"][0]["content"] == "Visible."
@pytest.mark.asyncio
@@ -172,6 +277,474 @@ async def test_stream_response_reasoning_content(mistral_provider):
)
+async def test_stream_response_native_mistral_thinking_chunk(mistral_provider):
+ req = MockRequest()
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content=[
+ {
+ "type": "thinking",
+ "thinking": [{"type": "text", "text": "Native thought."}],
+ }
+ ],
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=2, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in mistral_provider.stream_response(req)]
+
+ assert any(
+ '"thinking_delta"' in event and "Native thought." in event for event in events
+ )
+
+
+async def test_stream_response_native_mistral_text_chunk(mistral_provider):
+ req = MockRequest()
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content=[{"type": "text", "text": "Native text."}],
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=2, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in mistral_provider.stream_response(req)]
+
+ assert any('"text_delta"' in event and "Native text." in event for event in events)
+
+
+async def test_stream_response_preserves_native_thinking_and_string_text(
+ mistral_provider,
+):
+ req = MockRequest()
+
+ mock_chunk = SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(
+ content="Visible token.",
+ thinking="Native thought.",
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ],
+ usage=MagicMock(completion_tokens=2, prompt_tokens=10),
+ )
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in mistral_provider.stream_response(req)]
+
+ event_text = "\n".join(events)
+ assert '"thinking_delta"' in event_text
+ assert "Native thought." in event_text
+ assert '"text_delta"' in event_text
+ assert "Visible token." in event_text
+
+
+async def test_stream_response_preserves_native_reasoning_and_string_text(
+ mistral_provider,
+):
+ req = MockRequest()
+
+ mock_chunk = SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ delta=SimpleNamespace(
+ content="Visible token.",
+ reasoning="Native reasoning.",
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ],
+ usage=MagicMock(completion_tokens=2, prompt_tokens=10),
+ )
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in mistral_provider.stream_response(req)]
+
+ event_text = "\n".join(events)
+ assert "Native reasoning." in event_text
+ assert "Visible token." in event_text
+
+
+async def test_stream_response_preserves_mixed_native_content_array(
+ mistral_provider,
+):
+ req = MockRequest()
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content=[
+ {
+ "type": "thinking",
+ "thinking": [{"type": "text", "text": "Native thought."}],
+ },
+ {"type": "text", "text": "Native text."},
+ ],
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=2, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [event async for event in mistral_provider.stream_response(req)]
+
+ event_text = "\n".join(events)
+ assert "Native thought." in event_text
+ assert "Native text." in event_text
+
+
+async def test_stream_response_suppresses_native_mistral_thinking_when_disabled(
+ mistral_provider,
+):
+ req = MockRequest()
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content=[
+ {
+ "type": "thinking",
+ "thinking": [{"type": "text", "text": "Hidden."}],
+ },
+ {"type": "text", "text": "Visible."},
+ ],
+ reasoning_content=None,
+ tool_calls=None,
+ ),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=2, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.return_value = mock_stream()
+
+ events = [
+ event
+ async for event in mistral_provider.stream_response(
+ req, thinking_enabled=False
+ )
+ ]
+
+ event_text = "\n".join(events)
+ assert "Hidden." not in event_text
+ assert "Visible." in event_text
+
+
+async def test_stream_response_retries_without_mistral_reasoning_on_rejection(
+ mistral_provider,
+):
+ req = MockRequest(
+ system=None,
+ messages=[
+ MockMessage(
+ "assistant",
+ [
+ MockBlock(type="thinking", thinking="Need the tool."),
+ MockBlock(
+ type="tool_use",
+ id="toolu_reasoning",
+ name="echo",
+ input={"value": "FCC_TOOL"},
+ ),
+ ],
+ )
+ ],
+ )
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(
+ content="Recovered", reasoning_content=None, tool_calls=None
+ ),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=5, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ error = _make_bad_request_error("Unsupported field: reasoning_effort")
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [error, mock_stream()]
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+ first_call = mock_create.await_args_list[0].kwargs
+ second_call = mock_create.await_args_list[1].kwargs
+ assert first_call["reasoning_effort"] == "high"
+ assert first_call["messages"][0]["content"][0]["type"] == "thinking"
+ assert "reasoning_effort" not in second_call
+ assert second_call["messages"][0]["content"] == ""
+ assert second_call["messages"][0]["tool_calls"][0]["id"] == "toolu_reasoning"
+ assert any("Recovered" in event for event in events)
+ assert any("message_stop" in event for event in events)
+
+
+async def test_stream_response_reasoning_retry_preserves_visible_text_and_tools(
+ mistral_provider,
+):
+ req = MockRequest(
+ system=None,
+ messages=[
+ MockMessage(
+ "assistant",
+ [
+ MockBlock(type="thinking", thinking="Need the tool."),
+ MockBlock(type="text", text="Visible history."),
+ MockBlock(
+ type="tool_use",
+ id="toolu_reasoning",
+ name="echo",
+ input={"value": "FCC_TOOL"},
+ ),
+ ],
+ )
+ ],
+ )
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(content="Recovered", reasoning_content=None),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=5, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ error = _make_bad_request_error("Unsupported field: reasoning_effort")
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [error, mock_stream()]
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ second_call = mock_create.await_args_list[1].kwargs
+ assert second_call["messages"][0]["content"] == "Visible history."
+ assert second_call["messages"][0]["tool_calls"][0]["id"] == "toolu_reasoning"
+ assert any("Recovered" in event for event in events)
+
+
+async def test_stream_response_retries_on_mistral_422_reasoning_rejection(
+ mistral_provider,
+):
+ req = MockRequest()
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(content="Recovered", reasoning_content=None),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=5, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ error = _StatusError(
+ "body.messages.assistant.thinking extra_forbidden",
+ status_code=422,
+ body={"detail": [{"loc": ["body", "reasoning_effort"]}]},
+ )
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [error, mock_stream()]
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+ assert "reasoning_effort" not in mock_create.await_args_list[1].kwargs
+ assert any("Recovered" in event for event in events)
+
+
+async def test_stream_response_retries_when_model_disables_reasoning_input(
+ mistral_provider,
+):
+ req = MockRequest(
+ system=None,
+ messages=[
+ MockMessage(
+ "assistant",
+ [
+ MockBlock(type="thinking", thinking="Need context."),
+ MockBlock(type="text", text="Visible history."),
+ ],
+ )
+ ],
+ )
+
+ mock_chunk = MagicMock()
+ mock_chunk.choices = [
+ MagicMock(
+ delta=MagicMock(content="Recovered", reasoning_content=None),
+ finish_reason="stop",
+ )
+ ]
+ mock_chunk.usage = MagicMock(completion_tokens=5, prompt_tokens=10)
+
+ async def mock_stream():
+ yield mock_chunk
+
+ error = _StatusError(
+ "Reasoning input is not enabled for this model",
+ status_code=400,
+ body={
+ "object": "error",
+ "message": "Reasoning input is not enabled for this model",
+ "type": "invalid_request_invalid_args",
+ "code": "3051",
+ },
+ )
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = [error, mock_stream()]
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ assert mock_create.await_count == 2
+ second_call = mock_create.await_args_list[1].kwargs
+ assert "reasoning_effort" not in second_call
+ assert second_call["messages"][0]["content"] == "Visible history."
+ assert any("Recovered" in event for event in events)
+
+
+async def test_stream_response_unrelated_bad_request_does_not_retry(mistral_provider):
+ req = MockRequest()
+ error = _make_bad_request_error("Unsupported field: top_k")
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = error
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ assert mock_create.await_count == 1
+ assert any("message_stop" in event for event in events)
+
+
+async def test_stream_response_generic_thinking_error_does_not_retry(
+ mistral_provider,
+):
+ req = MockRequest()
+ error = _make_bad_request_error("The model was thinking, but top_k is unsupported")
+
+ with patch.object(
+ mistral_provider._client.chat.completions, "create", new_callable=AsyncMock
+ ) as mock_create:
+ mock_create.side_effect = error
+
+ events = [e async for e in mistral_provider.stream_response(req)]
+
+ assert mock_create.await_count == 1
+ assert any("message_stop" in event for event in events)
+
+
+def test_retry_body_without_reasoning_returns_none(mistral_provider):
+ body = {"model": "x", "messages": [{"role": "user", "content": "hi"}]}
+
+ assert (
+ mistral_provider._get_retry_request_body(
+ _make_bad_request_error("Unsupported field: reasoning_effort"), body
+ )
+ is None
+ )
+
+
@pytest.mark.asyncio
async def test_cleanup(mistral_provider):
"""cleanup closes the OpenAI client."""
@@ -180,3 +753,17 @@ async def test_cleanup(mistral_provider):
await mistral_provider.cleanup()
mistral_provider._client.close.assert_called_once()
+
+
+def _make_bad_request_error(message: str) -> openai.BadRequestError:
+ request = Request("POST", "https://api.mistral.ai/v1/chat/completions")
+ response = Response(400, request=request)
+ body = {"error": {"message": message}}
+ return openai.BadRequestError(message, response=response, body=body)
+
+
+class _StatusError(Exception):
+ def __init__(self, message: str, *, status_code: int, body: dict):
+ super().__init__(message)
+ self.status_code = status_code
+ self.body = body
diff --git a/uv.lock b/uv.lock
index 7b8728f7..8186be4c 100644
--- a/uv.lock
+++ b/uv.lock
@@ -561,7 +561,7 @@ wheels = [
[[package]]
name = "free-claude-code"
-version = "3.4.3"
+version = "3.4.5"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },