Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

fix(api): keep GLM-5.3 reasoning out of the /v1/messages answer

Suliman Abdulrazzaqe0001c2c0ddb
diff --git a/c/openai_server.py b/c/openai_server.py
index dcdbc80a..cefaa0c4 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -5320,6 +5320,11 @@ class APIHandler(BaseHTTPRequestHandler):
if not isinstance(stream, bool):
raise APIError(400, "`stream` must be a boolean.", "stream")
message_id = "msg_" + uuid.uuid4().hex[:24]
+ # GLM-5.3 opens <think> in the prompt even with thinking off (#1278), so its reply
+ # starts with reasoning either way. Give that reasoning its own thinking block, as
+ # the OpenAI path gives it reasoning_content, instead of gluing it to the answer.
+ reasoning_block = enable_thinking or starts_in_reasoning(enable_thinking,
+ add_generation_prompt)
def blocks_and_stop(text, stats, tool_reply=None):
"""Split a finished reply into Anthropic content blocks + stop_reason."""
@@ -5327,10 +5332,10 @@ class APIHandler(BaseHTTPRequestHandler):
reasoning = ""
if ARCH == "inkling":
text, reasoning = split_inkling(text)
- elif enable_thinking:
+ elif reasoning_block:
reasoning, text = split_thinking_reply(text, enable_thinking,
add_generation_prompt)
- if enable_thinking:
+ if reasoning_block:
content.append({"type": "thinking", "thinking": reasoning,
"signature": ANTHROPIC_LOCAL_SIGNATURE})
calls = []
@@ -5420,10 +5425,10 @@ class APIHandler(BaseHTTPRequestHandler):
"id": message_id, "type": "message", "role": "assistant",
"model": self.server.model_id, "content": [], "stop_reason": None,
"stop_sequence": None, "usage": {"input_tokens": 0, "output_tokens": 0}}})
- text_index = 1 if enable_thinking else 0
- stream_state = {"thinking_closed": not enable_thinking,
- "text_started": not enable_thinking}
- if enable_thinking:
+ text_index = 1 if reasoning_block else 0
+ stream_state = {"thinking_closed": not reasoning_block,
+ "text_started": not reasoning_block}
+ if reasoning_block:
send_event("content_block_start", {"type": "content_block_start", "index": 0,
"content_block": {"type": "thinking", "thinking": "", "signature": ""}})
else:
diff --git a/c/tests/test_anthropic_messages.py b/c/tests/test_anthropic_messages.py
index 7d9bd6a5..cace48a4 100644
--- a/c/tests/test_anthropic_messages.py
+++ b/c/tests/test_anthropic_messages.py
@@ -345,6 +345,36 @@ class MessagesHTTPTest(unittest.TestCase):
{"type": "text", "text": "answer"},
])
+ def test_glm53_reasoning_without_thinking_gets_its_own_block(self):
+ """GLM-5.3 opens <think> even with thinking off (#1278): the reasoning it writes
+ must not be glued to the answer."""
+ self.engine.script = ("Let me think", "</think>", "Paris.")
+ with patch("openai_server.ARCH", "glm53"):
+ with self.post(self.base_body()) as response:
+ payload = json.load(response)
+ self.assertEqual(payload["content"], [
+ {"type": "thinking", "thinking": "Let me think", "signature": "colibri-local"},
+ {"type": "text", "text": "Paris."},
+ ])
+
+ def test_streamed_glm53_reasoning_without_thinking_gets_its_own_block(self):
+ self.engine.script = ("Let me think", "</think>", "Paris.")
+ with patch("openai_server.ARCH", "glm53"):
+ with self.post(self.base_body(stream=True)) as response:
+ raw = response.read().decode()
+ payloads = [json.loads(line[len("data: "):]) for line in raw.splitlines()
+ if line.startswith("data: ")]
+ starts = [p for p in payloads if p["type"] == "content_block_start"]
+ self.assertEqual([(p["index"], p["content_block"]["type"]) for p in starts],
+ [(0, "thinking"), (1, "text")])
+ deltas = [p for p in payloads if p["type"] == "content_block_delta"]
+ self.assertEqual({(p["index"], p["delta"]["type"]) for p in deltas},
+ {(0, "thinking_delta"), (0, "signature_delta"), (1, "text_delta")})
+ self.assertEqual("".join(p["delta"].get("thinking", "") for p in deltas),
+ "Let me think")
+ self.assertEqual("".join(p["delta"].get("text", "") for p in deltas), "Paris.")
+ self.assertNotIn("</think>", raw)
+
def test_inkling_thinking_uses_inkling_content_markers(self):
self.engine.script = ("<|content_thinking|>reason", "ing<|content_text|>answer",)
with patch("openai_server.ARCH", "inkling"):