Skip to content

fix: report truncation between Gemini MAX_TOKENS and Claude/OpenAI - #148

Merged
lich0821 merged 1 commit into
lich0821:masterfrom
pcbeingused333:fix/truncation-stop-reasons-gemini
Oct 23, 2026
Merged

lich0821 merged 1 commit into
lich0821:masterfrom
pcbeingused333:fix/truncation-stop-reasons-gemini

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

What

The Gemini converters never mapped a reply cut off by the token limit:

  • Gemini finishReason: "MAX_TOKENS" always became Claude stop_reason: "end_turn" (GeminiRespToClaude, GeminiStreamToClaude) or OpenAI finish_reason: "stop" (GeminiRespToOpenAI, GeminiStreamToOpenAI).
  • Claude stop_reason: "max_tokens" always became Gemini finishReason: "STOP" (ClaudeRespToGemini).

A client behind ccNexus therefore saw a truncated reply as a complete one.

These five now map MAX_TOKENS to max_tokens (Claude), length (OpenAI) and back (MAX_TOKENS). MAX_TOKENS wins over tool_use / tool_calls, since a call cut off mid-arguments is a truncated reply. The streaming converters from Claude and OpenAI to Gemini carry no finish reason at all and are not touched. The same fix for the Claude <-> OpenAI pair is a separate change.

Tests

New internal/transformer/convert/gemini_truncation_test.go, one test per converter:

  • TestGeminiRespToClaudeMaxTokensReportsMaxTokens and TestGeminiStreamToClaudeMaxTokensReportsMaxTokens
  • TestGeminiRespToOpenAIMaxTokensReportsLength and TestGeminiStreamToOpenAIMaxTokensReportsLength
  • TestClaudeRespToGeminiMaxTokensReportsMaxTokens

All five fail on master and pass now. go test ./internal/transformer/... and go vet ./internal/transformer/... pass.

This change was made with Claude Code; the tests above were run on the branch to check it.

The Gemini converters never mapped a reply cut off by the token limit.
Gemini finishReason MAX_TOKENS always became Claude end_turn or OpenAI stop,
and Claude max_tokens became Gemini STOP, so clients treated a truncated
reply as complete. Map them in the Gemini <-> Claude and Gemini -> OpenAI
converters.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@lich0821
lich0821 merged commit f059958 into lich0821:master Oct 23, 2026
6 checks passed
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