Skip to content

stream: retry Gemini MALFORMED_FUNCTION_CALL empty turns with a nudge - #406

Merged
FMXExpress merged 2 commits into
mainfrom
claude/gemini-malformed-retry
Jul 1, 2026
Merged

FMXExpress merged 2 commits into
mainfrom
claude/gemini-malformed-retry

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

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_CALL when it emits a function call it can't itself serialise — classically a single oversized argument (a whole inline HTML file crammed into write_file.content). The candidate carries no text and no usable functionCall, so the turn is empty. But IsEmptyTurn's finish-reason whitelist only covered stop / '' / end_turn:

Result := (R.Content = '') and (Length(R.ToolCalls) = 0)
      and ((R.FinishReason = 'stop') or (R.FinishReason = '') or (R.FinishReason = 'end_turn'));

So MALFORMED_FUNCTION_CALL fell through, ChatWithEmptyRetry skipped it, and RunToolLoop treated 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

  1. IsEmptyTurn recognises MALFORMED_FUNCTION_CALL as a retryable empty turn.
  2. Because a bare re-send of a deterministically malformed oversized call just reproduces it, the retry appends a one-shot corrective nudge: stop narrating, re-issue one well-formed call, and build large files incrementally (write_file stub → 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, where StreamReliability (default EmptyRetryAttempts=2) is already wired.

Tests

stream_reliability_tests extended: MALFORMED_FUNCTION_CALL with 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-reliability green.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LGQKz579j1ZnDRwmr6h1V6


Generated by Claude Code

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +272 to +273
'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 ' +

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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
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.

2 participants