Repository navigation
stream: retry Gemini MALFORMED_FUNCTION_CALL empty turns with a nudge - #406
Conversation
Gemini returns finish_reason=MALFORMED_FUNCTION_CALL when it emits a function call it can't serialise -- classically a single oversized argument (a whole inline HTML file crammed into fs_write.content). The candidate carries no text and no usable functionCall, so the turn is empty. IsEmptyTurn's finish-reason whitelist (stop / '' / end_turn) excluded it, so ChatWithEmptyRetry skipped it and RunToolLoop treated it as a clean stop -- the turn died with "(no content returned by the model)" and the user had to hand-type "continue". Repeatedly, for the same oversized write, so the deliverable never landed. Recognise MALFORMED_FUNCTION_CALL as a retryable empty turn. Because a bare re-send of a deterministically-malformed oversized call reproduces it, the retry appends a one-shot corrective user turn: stop narrating, re-issue one well-formed call, and write large files incrementally (fs_write stub -> fs_edit_hashline) rather than in a single call. Plain brownout empties (finish=stop) still get a clean no-nudge re-send. Test: MALFORMED empty is an empty turn (but not when it salvaged text); a malformed-then-content script recovers in 2 calls with the nudge appended to the retry history. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d605b9c7e9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| 'file, write a short first version with fs_write, then grow it in ' + | ||
| 'steps with fs_edit_hashline instead of emitting the whole file in a ' + |
There was a problem hiding this comment.
Avoid suggesting unavailable edit tools on retries
When the malformed-call retry runs in a --no-hashline / hashline_enabled:false serve or agent session, RegisterFSTools deliberately omits fs_edit_hashline, yet this nudge tells the model to recover by using that tool. In that environment the retry can steer Gemini toward a function that was not declared, so the deterministic malformed large-write failure is likely to repeat or turn into an invalid-tool call instead of recovering; the nudge should be based on the supplied Tools list or use only registered alternatives.
Useful? React with 👍 / 👎.
The MALFORMED_FUNCTION_CALL retry nudge hard-coded "write a short first
version with fs_write, then grow it in steps with fs_edit_hashline". In a
--no-hashline / hashline_enabled:false session RegisterFSTools omits the
hashline editor, so the nudge steered Gemini toward an undeclared tool --
turning the recovery into an invalid-tool call.
Base the how-to-write-a-large-file hint on the supplied Tools list: a new
FirstToolPresent picks the first registered writer (write_file / fs_write)
and incremental editor (append_file / edit_file / fs_edit_hashline) and
names only those. When no incremental editor is registered, fall back to
tool-agnostic guidance ("split the work into smaller tool calls"). The
core "re-issue ONE well-formed call, do not narrate" instruction is
unchanged and tool-independent.
Test extended: nudge names the advertised write_file/edit_file and never
an unadvertised tool; a no-editor Tools list yields the generic fallback.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
The bug
A
pasclaw serve+ web UI run with Gemini (gemini-3.5-flash) repeatedly stalled with(no content returned by the model; finish_reason=MALFORMED_FUNCTION_CALL), forcing the user to hand-type "continue" over and over while the deliverable never landed.Root cause: Gemini returns
finish_reason=MALFORMED_FUNCTION_CALLwhen it emits a function call it can't itself serialise — classically a single oversized argument (a whole inline HTML file crammed intowrite_file.content). The candidate carries no text and no usablefunctionCall, so the turn is empty. ButIsEmptyTurn's finish-reason whitelist only coveredstop/''/end_turn:So
MALFORMED_FUNCTION_CALLfell through,ChatWithEmptyRetryskipped it, andRunToolLooptreated it as a clean stop — the turn died with empty content. The empty-turn retry machinery (default 2 attempts) that exists precisely to recover this was being bypassed on a technicality.The fix
IsEmptyTurnrecognisesMALFORMED_FUNCTION_CALLas a retryable empty turn.write_filestub →edit_file/append_file) rather than in a single call. Plain brownout empties (finish=stop) still get a clean, no-nudge re-send.Both apply to the
pasclaw serve/ gateway path, whereStreamReliability(defaultEmptyRetryAttempts=2) is already wired.Tests
stream_reliability_testsextended:MALFORMED_FUNCTION_CALLwith no content/calls is an empty turn (but not when it salvaged text); a malformed-then-content script recovers in 2 calls with the nudge appended to the retry history. Full build +make test-stream-reliabilitygreen.🤖 Generated with Claude Code
https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6
Generated by Claude Code