Skip to content

fix(api): strip Gemma special tokens from model output (fixes #1087)#1698

Merged
jundot merged 1 commit into
jundot:mainfrom
JimStenstrom:fix/1087-strip-gemma-tokens
Jun 6, 2026
Merged

fix(api): strip Gemma special tokens from model output (fixes #1087)#1698
jundot merged 1 commit into
jundot:mainfrom
JimStenstrom:fix/1087-strip-gemma-tokens

Conversation

@JimStenstrom

Copy link
Copy Markdown
Contributor

Fixes #1087.

SPECIAL_TOKENS_PATTERN in omlx/api/utils.py stripped special tokens for the Llama / Qwen / Mistral / GPT-2 families but omitted Gemma's — <eos>, <bos>, <end_of_turn>, <start_of_turn> — so they leaked into assistant message.content, most visibly in structured outputs.

This adds those four to the strip pattern, so both clean_special_tokens() and clean_output_text() remove them on every engine output path.

Test

Adds test_clean_gemma_special_tokens (covers <eos>, <end_of_turn>, <start_of_turn>, and <bos>…<eos>). The existing </s> / <s> / <pad> / [PAD] / [SEP] / [CLS] cases still pass, guarding the pattern.

pytest tests/test_api_utils.py → 201 passed.

…1087)

SPECIAL_TOKENS_PATTERN covered Llama/Qwen/Mistral/GPT-2 special tokens but
omitted Gemma's (<eos>, <bos>, <end_of_turn>, <start_of_turn>), so they leaked
into assistant content (notably structured outputs). Add them to the strip
pattern — clean_output_text/clean_special_tokens now remove them on every
engine output path. Adds test_clean_gemma_special_tokens (and the existing
</s>/<s>/<pad> cases still pass, guarding the pattern).
@jundot

jundot commented Jun 6, 2026

Copy link
Copy Markdown
Owner

Thanks for fixing this. The change is in the right central cleanup path, so it covers the structured output leak from #1087 without adding model-specific response handling. I verified the focused API utility and thinking tests locally, and this looks good to me. Merging.

@jundot jundot merged commit a8e15ac into jundot:main Jun 6, 2026
@JimStenstrom JimStenstrom deleted the fix/1087-strip-gemma-tokens branch June 7, 2026 14:00
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.

structured_outputs leaks special tokens (<eos>) into message.content for Gemma models

2 participants