Skip to content

Commit 3a8ba38

Browse files
committed
fix(llm): a refusal for missing reasoning_content is answered with an empty one
The OpenCode gateway routes deepseek-v4-pro to backends that disagree: one streamed no reasoning in 24 of 24 turns of a batch (23-24 of 24 in five others), the next refused the history that followed. A lead died on its fourth call with both earlier turns empty, after the passback fix. DeepSeek accepts an empty reasoning_content (the fix OpenCode adopted, anomalyco/opencode#24104). The first refusal naming the field fills every assistant message that lacks it and replays; the client keeps filling from then on. Asked of the provider, as tool_choice already is, so no other provider ever receives the field.
1 parent 21e3709 commit 3a8ba38

3 files changed

Lines changed: 125 additions & 17 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ sandboxed run and 1618 tests passing against the installed package.
5656
sub-agents included: the OpenCode gateway drops `reasoning_content` from non-streamed
5757
replies (0 of 24 turns on 2026-09-29, 24 of 24 streamed), which is why the sub-agents
5858
still died after the lead was fixed; an endpoint that refuses streaming is asked again
59-
without it. Sessions saved before this release have no reasoning to send.
59+
without it. And because the gateway routes one model to backends that disagree — one
60+
streams no reasoning at all, the next refuses the history that follows (a lead died on
61+
its fourth call, both earlier turns empty) — the first such refusal makes the client fill
62+
every missing `reasoning_content` with an empty string, which the rule accepts, replay
63+
the request, and keep doing so. No other provider ever sees the field, so sessions saved
64+
before this release, which carry no reasoning, continue too.
6065
- **A search ranks the same in every process.** Chroma persists its HNSW graph only every
6166
`sync_threshold` writes — 1000 by default — and replays whatever followed the last persist
6267
into the in-memory graph at every start, in an order that varies. Measured on 2026-09-22

‎helioai/core/llm/openai_compat.py‎

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ def to_openai_messages(messages: list[Message]) -> list[dict]:
9797
return out
9898

9999

100+
def _carry_reasoning(openai_messages: list[dict]) -> None:
101+
"""Give every assistant message a `reasoning_content`, empty when there was none."""
102+
for m in openai_messages:
103+
if m["role"] == "assistant" and "reasoning_content" not in m:
104+
m["reasoning_content"] = ""
105+
106+
100107
def to_openai_tools(tools: list[ToolDef]) -> list[dict]:
101108
"""Convert tool definitions to OpenAI function-calling schemas.
102109
@@ -270,6 +277,7 @@ def __init__(
270277
self._max_output_tokens = max_output_tokens
271278
self._temperature = temperature
272279
self._provider = provider
280+
self._reasoning_required = False
273281

274282
async def aclose(self) -> None:
275283
"""Close the httpx pool held by the SDK client."""
@@ -315,7 +323,7 @@ async def _chat_unstreamed(
315323
tool_choice: str,
316324
) -> Message:
317325
kwargs = self._request(messages, tools, system_prompt, tool_choice)
318-
response = await self._create(kwargs, tool_choice)
326+
response = await self._create(kwargs)
319327
reply = from_openai_response(response, self._provider)
320328
if not (reply.content or "").strip() and not reply.tool_calls:
321329
log.warning("%s empty turn, retrying once: %s", self._provider, self._model)
@@ -334,6 +342,8 @@ def _request(
334342
if system_prompt:
335343
openai_messages.append({"role": self._system_role, "content": system_prompt})
336344
openai_messages.extend(to_openai_messages(messages))
345+
if self._reasoning_required:
346+
_carry_reasoning(openai_messages)
337347

338348
kwargs: dict = {
339349
"model": self._model,
@@ -347,20 +357,36 @@ def _request(
347357
kwargs["tool_choice"] = tool_choice
348358
return kwargs
349359

350-
async def _create(self, kwargs: dict, tool_choice: str) -> Any:
351-
try:
352-
return await call_with_retry(lambda: self._client.chat.completions.create(**kwargs))
353-
except BadRequestError as e:
354-
# Forcing a tool call is a preference, never worth losing the turn over.
355-
# DeepSeek v4 in thinking mode rejects `required` outright ("Thinking mode
356-
# does not support this tool_choice"), which killed a sub-agent on its very
357-
# first turn. Whether a model accepts it depends on the model and its
358-
# reasoning mode, not on the provider, so it is asked rather than tabulated.
359-
if tool_choice == "auto" or "tool_choice" not in str(e):
360+
async def _create(self, kwargs: dict) -> Any:
361+
while True:
362+
try:
363+
return await call_with_retry(lambda: self._client.chat.completions.create(**kwargs))
364+
except BadRequestError as e:
365+
# DeepSeek in thinking mode wants `reasoning_content` on every earlier
366+
# assistant turn of a request with tools, and an empty string satisfies
367+
# it. HelioAI sends back what it received, but the OpenCode gateway
368+
# routes a model to backends that disagree: one streams no reasoning at
369+
# all (0 of 24 turns in one batch on 2026-09-29, 23–24 of 24 in five
370+
# others), the next rejects the history that follows — a lead died on its
371+
# fourth call with both earlier turns empty. Asked of the provider, not
372+
# tabulated: the first such refusal fills the missing fields and replays,
373+
# and this client fills them from then on.
374+
if "reasoning_content" in str(e) and not self._reasoning_required:
375+
log.warning("reasoning_content_required: %s", self._model)
376+
self._reasoning_required = True
377+
_carry_reasoning(kwargs["messages"])
378+
continue
379+
# Forcing a tool call is a preference, never worth losing the turn over.
380+
# DeepSeek v4 in thinking mode rejects `required` outright ("Thinking
381+
# mode does not support this tool_choice"), which killed a sub-agent on
382+
# its very first turn. Whether a model accepts it depends on the model
383+
# and its reasoning mode, not on the provider, so it is asked rather
384+
# than tabulated.
385+
if kwargs.get("tool_choice") == "required" and "tool_choice" in str(e):
386+
log.warning("tool_choice_rejected_falling_back_to_auto: %s", self._model)
387+
kwargs["tool_choice"] = "auto"
388+
continue
360389
raise
361-
log.warning("tool_choice_rejected_falling_back_to_auto: %s", self._model)
362-
kwargs["tool_choice"] = "auto"
363-
return await call_with_retry(lambda: self._client.chat.completions.create(**kwargs))
364390

365391
async def stream_chat(
366392
self,
@@ -396,7 +422,7 @@ async def stream_chat(
396422
kwargs["stream"] = True
397423
kwargs["stream_options"] = {"include_usage": True}
398424
try:
399-
stream = await self._create(kwargs, tool_choice)
425+
stream = await self._create(kwargs)
400426
except BadRequestError as e:
401427
if not _REFUSES_STREAMING.search(str(e)):
402428
raise

‎tests/test_llm_clients.py‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ class _Resp:
11121112
def json(self):
11131113
return {}
11141114

1115-
message = "Upstream request failed: [invalid_request_error] reasoning_content ..."
1115+
message = "Upstream request failed: [invalid_request_error] maximum context length exceeded"
11161116

11171117
async def create(**kwargs):
11181118
fake.calls.append(kwargs)
@@ -1122,3 +1122,80 @@ async def create(**kwargs):
11221122
with pytest.raises(BadRequestError):
11231123
await client.chat([Message(role="user", content="q")], [])
11241124
assert [c.get("stream") for c in fake.calls] == [True]
1125+
1126+
1127+
def _bad_request(message: str):
1128+
from openai import BadRequestError
1129+
1130+
class _Resp:
1131+
status_code = 400
1132+
headers: dict = {}
1133+
request = None
1134+
1135+
def json(self):
1136+
return {}
1137+
1138+
return BadRequestError(message, response=_Resp(), body=None)
1139+
1140+
1141+
_REASONING_400 = (
1142+
"Upstream request failed: [invalid_request_error] The `reasoning_content` in the "
1143+
"thinking mode must be passed back to the API."
1144+
)
1145+
1146+
1147+
async def test_a_refusal_for_missing_reasoning_fills_it_empty_and_replays():
1148+
"""The OpenCode gateway can stream a DeepSeek turn with no reasoning at all, then route
1149+
the next request to a backend that refuses it: a lead died on its fourth call. An
1150+
empty `reasoning_content` satisfies the rule."""
1151+
client, fake = _groq_client()
1152+
history = [
1153+
Message(role="user", content="q"),
1154+
Message(role="assistant", tool_calls=[ToolCall(id="c1", name="t", arguments={})]),
1155+
Message(role="tool", tool_call_id="c1", content="{}"),
1156+
Message(role="assistant", content="kept", reasoning="real"),
1157+
Message(role="user", content="q2"),
1158+
]
1159+
1160+
async def create(**kwargs):
1161+
fake.calls.append(kwargs)
1162+
assistants = [m for m in kwargs["messages"] if m["role"] == "assistant"]
1163+
if any("reasoning_content" not in m for m in assistants):
1164+
raise _bad_request(_REASONING_400)
1165+
return _respond(kwargs, _openai_response(content="ok"))
1166+
1167+
fake.completions.create = create
1168+
tools = [ToolDef(name="t", description="d")]
1169+
reply = await client.chat(history, tools)
1170+
1171+
assert reply.content == "ok" and len(fake.calls) == 2
1172+
sent = [
1173+
m.get("reasoning_content") for m in fake.calls[-1]["messages"] if m["role"] == "assistant"
1174+
]
1175+
assert sent == ["", "real"]
1176+
1177+
await client.chat(history, tools)
1178+
assert len(fake.calls) == 3, "once refused, the client fills the field up front"
1179+
1180+
1181+
async def test_a_second_refusal_for_reasoning_is_raised_not_looped():
1182+
client, fake = _groq_client()
1183+
1184+
async def create(**kwargs):
1185+
fake.calls.append(kwargs)
1186+
raise _bad_request(_REASONING_400)
1187+
1188+
fake.completions.create = create
1189+
history = [Message(role="user", content="q"), Message(role="assistant", content="a")]
1190+
with pytest.raises(Exception, match="reasoning_content"):
1191+
await client.chat(history, [ToolDef(name="t", description="d")])
1192+
assert len(fake.calls) == 2
1193+
1194+
1195+
async def test_no_provider_sees_an_empty_reasoning_field_before_asking_for_one():
1196+
client, fake = _groq_client()
1197+
await client.chat(
1198+
[Message(role="user", content="q"), Message(role="assistant", content="a")],
1199+
[ToolDef(name="t", description="d")],
1200+
)
1201+
assert "reasoning_content" not in fake.calls[-1]["messages"][1]

0 commit comments

Comments
 (0)