Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

GeminiSearch: default 2048-token cap can truncate answers on the default Flash model, and finishReason is never checked

Open mefsnef opened this issue 1 week ago • 1 comments

Version

Measured on main at 94bd8f2. GeminiSearch.ts is byte-identical in the v8.761.0 release. Also measured on 7.40.4.

What is broken

GeminiSearch.ts never reads finishReason. With the default maxOutputTokens of 2048, which thinking tokens also consume, research-length answers can be cut mid-sentence, and the tool prints them as complete with exit 0. When thinking takes the whole cap, it prints "Gemini returned an empty response", which reads like a key or quota problem. On the default Flash model the truncation is intermittent: the same query can finish or be cut from one run to the next.

Separately, API errors print only error.message, dropping the HTTP code and error.status. On our key, depleted prepay credit came back as 429 RESOURCE_EXHAUSTED, so the status alone does not separate credit from rate limits; the message does. (That part can go in a separate change.)

Where (file:line)

LifeOS/install/LIFEOS/TOOLS/GeminiSearch.ts:94 (default maxTokens: 2048), :138-142 (reads parts, never finishReason), :191 (empty-response message), :134 (throws the message only).

Repro on a clean tree

Fresh checkout of main at 94bd8f2, run from the repo root, with an empty HOME and only GOOGLE_API_KEY in the environment:

Q="Summarize the current state of research on whether large language models improve student learning outcomes in K-12 classrooms, with evidence for and against."
for a in "" "--pro" "--max-tokens 16000" "--pro --max-tokens 16000"; do
  bun LifeOS/install/LIFEOS/TOOLS/GeminiSearch.ts --json $a "$Q" \
  | jq -c --arg a "$a" '{args:$a, model:.modelVersion, finish:.candidates[0].finishReason, thoughts:.usageMetadata.thoughtsTokenCount, answer:.usageMetadata.candidatesTokenCount}'
done

Observed:

{"args":"","model":"gemini-3.8-flash","finish":"MAX_TOKENS","thoughts":446,"answer":null}
{"args":"--pro","model":"gemini-4-argon","finish":"STOP","thoughts":null,"answer":1311}
{"args":"--max-tokens 16000","model":"gemini-3.8-flash","finish":"STOP","thoughts":1393,"answer":1667}
{"args":"--pro --max-tokens 16000","model":"gemini-4-argon","finish":"STOP","thoughts":null,"answer":1593}

Negative control

On unpatched code, with no finishReason check:

  • Clean main, default Flash (gemini-3.8-flash), cap 2048: MAX_TOKENS on 1 of 1 run. The answer would be printed as complete.
  • The same model, query and cap on a 7.40.4 install (same request body): MAX_TOKENS on 1 of 2 runs. The truncated run reported thinking 977 + answer 1,067 = 2,044 tokens. The other run finished with STOP at thinking 1,004 + answer 1,315 = 2,319, above the 2048 cap.
  • --pro on main (gemini-4-argon): STOP on 1 of 1 at the default cap. Argon reported no thoughtsTokenCount.
  • 7.40.4 with its default models (gemini-3.6-flash, gemini-3.1-pro-preview), 6 research-style queries each, grounding on: MAX_TOKENS on 12 of 12, each ending mid-sentence. With --max-tokens 16000, 4 of 4 returned STOP.

Token counts are reported as observed. Two oddities, not investigated:

  • The MAX_TOKENS run on main reported only 446 thinking tokens and no candidatesTokenCount.
  • One STOP run exceeded the nominal cap.

Neither changes the defect: nothing checks finishReason.

Suggested fix

Untested on main; tested on 7.40.4.

  • Warn on stderr when a candidate's finishReason is not STOP, including when it is missing, and when promptFeedback.blockReason is set.
  • Raise the default cap. Every finished answer in our control fit in about 4,800 total tokens, so 8192 covers them.

We run exactly this locally on 7.40.4. It was reviewed and live-tested: default calls stay quiet, --max-tokens 600 warns, exit codes are unchanged. The parse-before-status problem is already #2776.

Before submitting

  • [x] I searched open and closed issues for this defect. None covers truncation. #2911 (GeminiWatch) and #2776 are related, not duplicates.
  • [x] The repro runs against a clean tree of the version above, not against my modified install.
  • [x] I removed personal data from the pasted output — real names, absolute home paths, tokens, my own content.

mefsnef avatar Nov 23 '26 12:11 mefsnef

[!NOTE] 🤖 Kai (AI assistant) responding on behalf of Daniel Miessler

Thanks for the report. This is fixed in our tree today (a73832b891), and you're credited at the fix site and as a co-author on the commit. It ships in the next public release. Closing.

danielmiessler avatar Dec 01 '26 09:12 danielmiessler