X Tutup
Skip to content

fix(chatd): continue tool execution on FinishReasonLength#22858

Open
mafredri wants to merge 2 commits intomainfrom
fix/chatd-finish-reason-length
Open

fix(chatd): continue tool execution on FinishReasonLength#22858
mafredri wants to merge 2 commits intomainfrom
fix/chatd-finish-reason-length

Conversation

@mafredri
Copy link
Member

@mafredri mafredri commented Mar 9, 2026

Bug

When the LLM hits MaxOutputTokens (32k default), it returns FinishReasonLength instead of FinishReasonToolCalls. If complete tool calls had already been emitted before the stream was truncated, shouldContinue was false. The tools were never executed and the loop exited with nil. The chat went to waiting status with no error visible to the user, who had to manually prompt it to continue.

This became much more frequent after compaction re-entry was introduced (#22640), which adds summary messages to the context, pushing the model closer to its output token limit during tool-heavy work.

Fix

Accept FinishReasonLength alongside FinishReasonToolCalls in the shouldContinue check so that emitted tool calls are always executed regardless of why the stream ended.

Test

TestRun_ToolCallsWithFinishReasonLength mirrors the existing TestRun_MultiStepToolExecution test but uses FinishReasonLength on step 0. Verifies:

  • Stream is called twice (tool-call step + follow-up)
  • Both steps are persisted
  • The follow-up prompt contains the tool result from step 0

RED/GREEN confirmed: test fails without the fix (expected: 2, actual: 1), passes with it.

When the LLM hits MaxOutputTokens it returns FinishReasonLength
instead of FinishReasonToolCalls. If complete tool calls were
already emitted, the loop silently exited without executing them
and the chat went to "waiting" with no error. The user had to
manually prompt it to continue.

Accept FinishReasonLength alongside FinishReasonToolCalls in the
shouldContinue check so that emitted tool calls are always
executed regardless of why the stream ended.
@mafredri mafredri force-pushed the fix/chatd-finish-reason-length branch from 6f47c09 to a802d50 Compare March 9, 2026 17:39
… calls

Verify that when the model returns FinishReasonLength but emits no
tool calls (text-only truncated response), the loop stops after a
single step. This complements TestRun_ToolCallsWithFinishReasonLength
which covers the positive case.
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

X Tutup