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): return GLM-5.3 reasoning only when thinking is enabled

Suliman Abdulrazzaqeae7b10c53db
diff --git a/c/openai_server.py b/c/openai_server.py
index cefaa0c4..c0df76da 100644
--- a/c/openai_server.py
+++ b/c/openai_server.py
@@ -5321,9 +5321,11 @@ class APIHandler(BaseHTTPRequestHandler):
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,
+ # starts with reasoning either way. Split it off so the answer never carries the
+ # reasoning or a literal </think>, but only return it as a thinking block when the
+ # client asked for thinking: the real API never sends one otherwise, and clients
+ # read the answer from content[0].
+ split_reasoning = enable_thinking or starts_in_reasoning(enable_thinking,
add_generation_prompt)
def blocks_and_stop(text, stats, tool_reply=None):
@@ -5332,10 +5334,10 @@ class APIHandler(BaseHTTPRequestHandler):
reasoning = ""
if ARCH == "inkling":
text, reasoning = split_inkling(text)
- elif reasoning_block:
+ elif split_reasoning:
reasoning, text = split_thinking_reply(text, enable_thinking,
add_generation_prompt)
- if reasoning_block:
+ if enable_thinking:
content.append({"type": "thinking", "thinking": reasoning,
"signature": ANTHROPIC_LOCAL_SIGNATURE})
calls = []
@@ -5425,10 +5427,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 reasoning_block else 0
- stream_state = {"thinking_closed": not reasoning_block,
- "text_started": not reasoning_block}
- if reasoning_block:
+ text_index = 1 if enable_thinking else 0
+ stream_state = {"thinking_closed": not enable_thinking,
+ "text_started": not enable_thinking}
+ if enable_thinking:
send_event("content_block_start", {"type": "content_block_start", "index": 0,
"content_block": {"type": "thinking", "thinking": "", "signature": ""}})
else:
@@ -5478,6 +5480,8 @@ class APIHandler(BaseHTTPRequestHandler):
state["buf"] = state["buf"][flush:]
def emit_thinking(chunk):
+ if not enable_thinking:
+ return # reasoning the client did not ask for
send_event("content_block_delta", {"type": "content_block_delta", "index": 0,
"delta": {"type": "thinking_delta", "thinking": chunk}})
diff --git a/c/tests/test_anthropic_messages.py b/c/tests/test_anthropic_messages.py
index cace48a4..6fc29839 100644
--- a/c/tests/test_anthropic_messages.py
+++ b/c/tests/test_anthropic_messages.py
@@ -345,19 +345,17 @@ class MessagesHTTPTest(unittest.TestCase):
{"type": "text", "text": "answer"},
])
- def test_glm53_reasoning_without_thinking_gets_its_own_block(self):
+ def test_glm53_reasoning_without_thinking_is_kept_out_of_the_answer(self):
"""GLM-5.3 opens <think> even with thinking off (#1278): the reasoning it writes
- must not be glued to the answer."""
+ must not be glued to the answer, and a request that did not ask for thinking
+ gets no thinking block, so content[0] is 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."},
- ])
+ self.assertEqual(payload["content"], [{"type": "text", "text": "Paris."}])
- def test_streamed_glm53_reasoning_without_thinking_gets_its_own_block(self):
+ def test_streamed_glm53_reasoning_without_thinking_is_kept_out_of_the_answer(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:
@@ -366,14 +364,24 @@ class MessagesHTTPTest(unittest.TestCase):
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")])
+ [(0, "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.")
+ {(0, "text_delta")})
+ self.assertEqual("".join(p["delta"]["text"] for p in deltas), "Paris.")
self.assertNotIn("</think>", raw)
+ self.assertNotIn("Let me think", raw)
+
+ def test_glm53_reasoning_with_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(thinking={"type": "enabled",
+ "budget_tokens": 1024})) 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_inkling_thinking_uses_inkling_content_markers(self):
self.engine.script = ("<|content_thinking|>reason", "ing<|content_text|>answer",)