Skip to content

Fix/gemma4 reasoning open optional newline#277

Merged
cubist38 merged 2 commits intomainfrom
fix/gemma4-reasoning-open-optional-newline
Apr 8, 2026
Merged

Fix/gemma4 reasoning open optional newline#277
cubist38 merged 2 commits intomainfrom
fix/gemma4-reasoning-open-optional-newline

Conversation

@cubist38
Copy link
Copy Markdown
Owner

@cubist38 cubist38 commented Apr 8, 2026

Fix: Make newline after reasoning open tag optional in Gemma4ReasoningParser

Summary

  • Remove the hardcoded \n from REASONING_OPEN, making the tag <|channel>thought instead of <|channel>thought\n, and handle the optional newline via regex (\n?) so the parser works regardless of whether the model emits a newline after the tag.
  • Refactor extract_reasoning_streaming to process the NORMAL state first (buffering partial open-tag matches) before handling the FOUND_PREFIX state. This fixes an issue where content arriving before the reasoning open tag could be dropped or misattributed, and ensures partial open-tag overlaps are correctly buffered instead of being emitted as regular content.

Changes

  • app/parsers/gemma4.py
    • REASONING_OPEN no longer includes a trailing newline; the newline is stripped with .lstrip("\n") after matching.
    • Regex updated to \n? between the open tag and captured content.
    • Streaming extraction now checks for and buffers partial REASONING_OPEN overlaps in NORMAL state, preventing premature passthrough of incomplete tags.
    • Content before the reasoning open tag is now properly returned as "content" rather than being silently discarded.

cubist38 added 2 commits April 6, 2026 14:03
- Removed trailing newline from REASONING_OPEN constant for accurate parsing.
- Updated reasoning regex to allow for optional newline before content capture.
- Trimmed leading whitespace from reasoning content to ensure clean extraction.
- Improved state management for parsing reasoning content, ensuring accurate handling of opening and closing tags.
- Added support for capturing content before reasoning and refined buffer management for partial matches.
- Streamlined the logic for returning reasoning content and passthrough data, enhancing overall parsing efficiency.
@cubist38 cubist38 merged commit 653593b into main Apr 8, 2026
2 checks passed
@cubist38 cubist38 deleted the fix/gemma4-reasoning-open-optional-newline branch April 8, 2026 02:29
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.

1 participant