Skip to content

fix(agent): stop losing reasoning streams and never commit an empty reply - #1675

Merged
ace331 merged 3 commits into
mainfrom
fix/reasoning-field
Nov 23, 2026
Merged

ace331 merged 3 commits into
mainfrom
fix/reasoning-field

Conversation

@ace331

@ace331 ace331 commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Context

A request to a vLLM-backed provider finished finish_reason=stop with an empty
body and 99 billed tokens. The service did not drop the response: the model
spent the whole turn thinking and stopped after the thought, and the client
never read that thought because the field is spelled delta.reasoning — which
the chat adapter did not know.

Server-side evidence (nginx 200s with 9.8 KB / 11.1 KB SSE bodies; a replay
with max_tokens=99 producing 46 frames / 99 tokens and no content) puts the
missing content in the reasoning stream. vLLM 0.19.1 DeltaMessage only has
reasoning; 0.33 marks reasoning_content deprecated and converges on it, as
does OpenRouter.

Changes

fix(llm): decode every spelling of the streamed reasoning field

The adapter read reasoning_content then thinking. It now takes the first
non-empty of reasoning, reasoning_content, thinking.

This also fixes a latent bug in the old chain: .or_else() ran before the
non-empty filter, so a delta carrying reasoning: "" plus a populated
reasoning_content discarded the fallback.

fix(agent): never commit an answer-less turn as completed

tool_calls.is_empty() used to return Ok(assistant_text) unconditionally, so
a step with no text and no tool call was committed as a completed run with an
empty reply. Two shapes reached that path: the model thought and stopped right
after the thought, or it produced nothing at all.

  • A step that did think is retried once with thinking switched off, through
    the scoped request controls (the session's configured level is untouched),
    plus a request-only instruction asking for the answer. The thought is not
    replayed: a reasoning-only assistant turn is rejected upstream.
  • A step still empty afterwards, and a step that never produced anything, ends
    the run as incomplete with reason empty_answer. StreamTruncation's
    unknown-reason fallback already renders it as [RESPONSE_UNCONFIRMED], so no
    client-side mapping changes.
  • The retry is one per model step and only when automatic retries are enabled
    (max_retries > 0); it never applies to fusion routing, whose own
    "exactly one fusion call" error is the better diagnosis.

Behaviour change worth a look

normal_reasoning_only_and_empty_replies_are_not_transport_errors asserted
that a stop with empty content is not incomplete. That is the behaviour this
PR deliberately overturns, so the test is rewritten as
empty_replies_end_incomplete_and_a_reasoning_only_one_is_retried with three
scenarios: truly empty → incomplete; reasoning-only then answered → completed,
with the retry instruction asserted and the thought absent from the replayed
request; reasoning-only twice → incomplete with the streamed thought still in
history.

Not changed

usage.prompt_tokens_details.cached_tokens is already read by chat_usage,
with prompt_cache_hit_tokens / cache_read_input_tokens fallbacks. A cache
read of 0 on this service comes from using the Anthropic /v1/messages path,
which vLLM 0.19.1 does not fill; the OpenAI path reports it.

Verification

  • cargo test -p future-agent --lib -- agent::run_loop — 183 passed
  • cargo test -p future-agent --lib -- llm::adapters::openai_chat — 31 passed
    (three new cases for the field spellings)
  • cargo test -p future-agent --lib -- fusion — 15 passed
  • cargo test -p future-agent --lib -- empty — 126 passed
  • rustfmt --check on the three touched files only; the repository-wide cargo fmt baseline is dirty and was left alone.

vLLM and OpenRouter stream chain-of-thought in `delta.reasoning`; vLLM 0.33
marks `reasoning_content` deprecated in favour of it. The chat adapter only
read `reasoning_content`/`thinking`, so a `reasoning` stream decoded as no
reasoning at all — a turn that spent its whole output on thinking arrived as
an empty reply.

The first non-empty of `reasoning`, `reasoning_content`, `thinking` now wins;
an empty higher-priority field no longer masks a populated fallback.
A model step that ended `stop` with no text and no tool call was returned as
a completed run with an empty reply. Two shapes reach that path: the model
thought and stopped right after the thought, or it produced nothing at all.

When the step did think, retry it once with thinking switched off (through
the scoped request controls, so the session's level is untouched) and an
explicit request for the answer. A step still empty afterwards — and a step
that never produced anything — ends the run as `incomplete` with reason
`empty_answer` ([RESPONSE_UNCONFIRMED]) instead of completing silently.
@ace331
ace331 force-pushed the fix/reasoning-field branch from 94f3409 to cf5fc52 Compare November 23, 2026 06:35
@ace331
ace331 enabled auto-merge (squash) November 23, 2026 06:37
@ace331
ace331 merged commit ef3b05b into main Nov 23, 2026
48 of 50 checks passed
@huichen
huichen deleted the fix/reasoning-field branch November 24, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant