Skip to content

armgit / ECC

Public Imported from GitHub
0

The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.

fix(llm): strip leaked reasoning from self-hosted model output

FenjuFud6f20191def6
diff --git a/src/llm/providers/astraflow.py b/src/llm/providers/astraflow.py
index 1f7cceab9..5bf2433f3 100644
--- a/src/llm/providers/astraflow.py
+++ b/src/llm/providers/astraflow.py
@@ -16,6 +16,7 @@ from llm.core.interface import (
)
from llm.core.types import LLMInput, LLMOutput, ModelInfo, ProviderType, ToolCall
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
+from llm.providers.reasoning import strip_reasoning
ASTRAFLOW_BASE_URL = "https://api.umodelverse.ai/v1"
ASTRAFLOW_CN_BASE_URL = "https://api.modelverse.cn/v1"
@@ -104,7 +105,7 @@ class _AstraflowBaseProvider(LLMProvider):
}
return LLMOutput(
- content=choice.message.content or "",
+ content=strip_reasoning(choice.message.content or ""),
tool_calls=tool_calls,
model=response.model,
usage=usage,
diff --git a/src/llm/providers/atlas.py b/src/llm/providers/atlas.py
index b266bb614..ccb042055 100644
--- a/src/llm/providers/atlas.py
+++ b/src/llm/providers/atlas.py
@@ -16,6 +16,7 @@ from llm.core.interface import (
)
from llm.core.types import LLMInput, LLMOutput, ModelInfo, ProviderType, ToolCall
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
+from llm.providers.reasoning import strip_reasoning
ATLAS_BASE_URL = "https://api.atlascloud.ai/v1"
DEFAULT_ATLAS_MODEL = "deepseek-ai/deepseek-v4-pro"
@@ -121,7 +122,7 @@ class AtlasProvider(LLMProvider):
}
return LLMOutput(
- content=choice.message.content or "",
+ content=strip_reasoning(choice.message.content or ""),
tool_calls=tool_calls,
model=response.model,
usage=usage,
diff --git a/src/llm/providers/ollama.py b/src/llm/providers/ollama.py
index 58505ac7b..1cfc5120a 100644
--- a/src/llm/providers/ollama.py
+++ b/src/llm/providers/ollama.py
@@ -19,6 +19,7 @@ from llm.core.types import (
ProviderType,
ToolCall,
)
+from llm.providers.reasoning import strip_reasoning
class OllamaProvider(LLMProvider):
@@ -85,7 +86,7 @@ class OllamaProvider(LLMProvider):
with urllib.request.urlopen(req, timeout=60) as response:
result = json.loads(response.read().decode("utf-8"))
- content = result.get("message", {}).get("content", "")
+ content = strip_reasoning(result.get("message", {}).get("content", ""))
tool_calls = None
if result.get("message", {}).get("tool_calls"):
diff --git a/src/llm/providers/openai.py b/src/llm/providers/openai.py
index 0bf84a33d..22ab043ea 100644
--- a/src/llm/providers/openai.py
+++ b/src/llm/providers/openai.py
@@ -22,6 +22,7 @@ from llm.core.types import (
ToolCall,
)
from llm.providers.constants import EMPTY_FILTERED_RESPONSE_ERROR
+from llm.providers.reasoning import strip_reasoning
class OpenAIProvider(LLMProvider):
@@ -105,7 +106,7 @@ class OpenAIProvider(LLMProvider):
}
return LLMOutput(
- content=choice.message.content or "",
+ content=strip_reasoning(choice.message.content or ""),
tool_calls=tool_calls,
model=response.model,
usage=usage,
diff --git a/src/llm/providers/reasoning.py b/src/llm/providers/reasoning.py
new file mode 100644
index 000000000..497ab8cf4
--- /dev/null
+++ b/src/llm/providers/reasoning.py
@@ -0,0 +1,33 @@
+"""Helpers for reasoning text that self-hosted models leave in message content."""
+
+from __future__ import annotations
+
+THINK_OPEN = "<think>"
+THINK_CLOSE = "</think>"
+
+
+def strip_reasoning(content: str) -> str:
+ """Drop a leading reasoning block from model output.
+
+ Hosted APIs return reasoning in a separate field, but self-hosted reasoning
+ models (vLLM or llama.cpp without a reasoning parser, Ollama models without
+ a thinking template) put it in ``content``. It arrives in two shapes:
+
+ - ``<think>...</think>answer``
+ - ``...</think>answer`` when the chat template prefills the opening tag in
+ the generation prompt, so the model only ever emits the closing tag.
+
+ A ``<think>`` that does not open the output is treated as a literal and
+ left alone, as is unterminated reasoning (for example when ``max_tokens``
+ cut the model off before it reached an answer).
+ """
+ close = content.find(THINK_CLOSE)
+ if close == -1:
+ return content
+
+ head = content[:close]
+ opening = head.find(THINK_OPEN)
+ if opening != -1 and head[:opening].strip():
+ return content
+
+ return content[close + len(THINK_CLOSE) :].lstrip()
diff --git a/tests/test_reasoning.py b/tests/test_reasoning.py
new file mode 100644
index 000000000..8ff274908
--- /dev/null
+++ b/tests/test_reasoning.py
@@ -0,0 +1,54 @@
+import urllib.request
+from io import BytesIO
+from types import SimpleNamespace
+
+import pytest
+
+from llm.core.types import LLMInput, Message, Role
+from llm.providers.ollama import OllamaProvider
+from llm.providers.openai import OpenAIProvider
+from llm.providers.reasoning import strip_reasoning
+
+
+ ("content", "expected"),
+ [
+ ("<think>17 * 3 = 51.</think>51", "51"),
+ ("\n<think>\nplan\n</think>\n\nanswer", "answer"),
+ ("17 * 3 = 51.</think>51", "51"),
+ ("plain answer", "plain answer"),
+ ("", ""),
+ ("Wrap it in <think> and </think> tags.", "Wrap it in <think> and </think> tags."),
+ ("<think>cut off by max_tokens", "<think>cut off by max_tokens"),
+ ],
+)
+def test_strip_reasoning(content, expected):
+ assert strip_reasoning(content) == expected
+
+
+def test_openai_provider_strips_inline_reasoning():
+ provider = OpenAIProvider(api_key="test")
+ message = SimpleNamespace(content="<think>17 * 3 = 51.</think>51", tool_calls=None)
+ response = SimpleNamespace(
+ choices=[SimpleNamespace(message=message, finish_reason="stop")],
+ model="spark-x2.5",
+ usage=None,
+ )
+ provider.client = SimpleNamespace(
+ chat=SimpleNamespace(completions=SimpleNamespace(create=lambda **params: response))
+ )
+
+ output = provider.generate(LLMInput(messages=[Message(role=Role.USER, content="17*3?")]))
+
+ assert output.content == "51"
+
+
+def test_ollama_provider_strips_prefilled_reasoning(monkeypatch):
+ def fake_urlopen(request, timeout):
+ return BytesIO(b'{"message": {"content": "17 * 3 = 51.</think>51"}, "done_reason": "stop"}')
+
+ monkeypatch.setattr(urllib.request, "urlopen", fake_urlopen)
+
+ output = OllamaProvider().generate(LLMInput(messages=[Message(role=Role.USER, content="17*3?")]))
+
+ assert output.content == "51"