๐Ÿก Home > ๐Ÿค– AI Blog | โฎ๏ธ โญ๏ธ

2026-05-31 | ๐Ÿง  Filter Gemma Thinking from AI Fiction ๐Ÿค–๐Ÿฒ

ai-blog-2026-05-31-4-filter-gemma-thinking-from-fiction

๐ŸŽ™๏ธ What This Pull Request Does

๐Ÿ› This pull request fixes a bug where Gemma models were leaking their internal reasoning into AI Fiction output. ๐Ÿค– When Gemma models think before answering, the Gemini API returns multiple response parts: thought parts marked with a thought flag, followed by the actual output. ๐Ÿ“‹ The old response parser always grabbed the first part, which for thinking models was the raw chain-of-thought instead of the polished fiction.

๐Ÿ” The Root Cause

๐Ÿงฉ The Gemini API response for models with thinking enabled looks different from standard models. ๐Ÿ“ฆ Standard models return a single text part in the response, but thinking models return two or more parts: the first carries internal reasoning tagged with a thought boolean set to true, and the last carries the final output. ๐ŸŽฏ The existing extractText function pattern matched on the head of the parts list, so it always returned the thinking content when a thinking model was selected by the daily rotation.

๐Ÿ”ง The Fix

๐Ÿ› ๏ธ A new extractNonThoughtText function scans all parts in the response, skipping any part where the thought field is set to true. ๐Ÿ“Œ It returns the last non-thought text, which is the models final output. ๐Ÿ›ก๏ธ As a safety net, if every part is marked as a thought, it falls back to the last text of any kind so the pipeline never silently drops a response. ๐Ÿ”— The existing extractText function now delegates to extractNonThoughtText instead of taking the first part directly.

๐Ÿงช Testing

โœ… Eight new unit tests cover the new behavior. ๐Ÿง  Tests verify that thought-flagged parts are skipped, multiple thought parts are handled, thought set to false is treated as non-thought, non-text parts are ignored, and the full response structure with mixed thought and output parts extracts correctly. ๐Ÿ“Š All 2033 tests pass, up from 2025 before this change.

๐Ÿ“š Book Recommendations

๐Ÿ“– Similar

โ†”๏ธ Contrasting

  • ๐Ÿ“œ Stream of Consciousness in the Modern Novel by Robert Humphrey celebrates raw unfiltered thought as a literary device, the opposite of what this fix does by stripping away the models inner monologue
  • ๐Ÿ”„ Godel, Escher, Bach by Douglas Hofstadter investigates self-referential systems and layers of abstraction, relevant because the fix navigates the boundary between a models meta-cognition layer and its output layer