fix: strip Gemma reasoning markers from inference streams#2071
Merged
Conversation
🦋 Changeset detectedLatest commit: ecad09d The changes in this PR will be included in the next version bump. This PR includes changesets to release 37 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Keep tool-call content out of TTS-facing chunks and retain split reasoning markers until a definitive finish reason arrives. Co-authored-by: Cursor <cursoragent@cursor.com>
toubatbrian
approved these changes
Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Ports livekit/agents#6398 to agents-js, stripping Gemma reasoning markers from streamed LiveKit Inference LLM output while preserving visible text.
Changes Made
<think>tags and model-specific Gemma channel markers.inference.LLMStreamwhile preserving the existing safety contract that tool-call chunks do not expose content to TTS.finish_reasonvalues as non-final so split markers remain buffered.@livekit/agents.Source diff coverage
Coverage classification
livekit-agents/livekit/agents/inference/llm.py: adapted toagents/src/inference/llm.ts. Added the Gemma model-specific reasoning marker map, instantiated one streaming thinking-token filter per LLM stream run, and stripped content before tool-call handling. The JS adaptation retains its existing tool-call-to-TTS safety contract by suppressing content on chunks that contain tool calls.livekit-agents/livekit/agents/llm/utils.py: adapted toagents/src/llm/utils.ts. The target had the same LLM utility counterpart but no existing thinking-token stripper, so this PR adds the TypeScript equivalent infrastructure:ThinkingTokenFilter, partial marker buffering, final flush/drop semantics, andstripThinkingTokens.tests/test_llm_utils.py: adapted toagents/src/llm/utils.test.tsandagents/src/inference/llm.test.ts. Utility-focused tests were ported into the existing LLM utils test file; focused inference regressions additionally cover omittedfinish_reasonand tool-call content suppression.Testing
pnpm exec vitest run agents/src/inference/llm.test.ts agents/src/llm/utils.test.tspassed: 2 files, 64 tests passed, 1 skipped.pnpm --filter @livekit/agents buildpassed.Cue voice E2E
sid_a151bb355799.ecad09dd3b576cbdd27fcf365449e94227988041.voiceover LiveKit with realdeepgram/nova-3STT,cartesia/sonic-3TTS, tool execution, room transport, and recorded audio.google/gemma-4-31b-it, because a hosted Gemma response cannot reliably reproduce the exact malformed split-marker/tool-content sequence on demand.finish_reasonand a tool-call content leak marker produced onlySPOKEN_SAFE_PREFIX., the expected tool output, and the final safe answer. Persisted events contained none ofSECRET_REASONING_MARKER,LEAKED_TOOL_CONTENT_MARKER,<|channel>, or<channel|>.~/.cue-cli/sessions/sid_a151bb355799/events.jsonl,recordings/001_run/result.json, sessionrecording.wav, and commandrecordings/001_run/recording.wav. Both WAVs were flushed and inspected directly.Additional Notes
Ported from livekit/agents#6398
Original PR description
Fixes #6375
Problem
Gemma channel-thought markers and their hidden reasoning pass through the shared thinking-token filter into customer-visible output, including TTS, transcripts, and chat history.
Root cause
strip_thinking_tokensonly recognizes<think>blocks and uses anasyncio.Event, which records whether the stream is inside a reasoning block but cannot retain marker fragments split across chunks. It also does not handle complete or multiple reasoning blocks within one chunk.Change
<think>and Gemma<|channel>thought/<channel|>blocksTests
uv run --frozen pytest tests/test_llm_utils.py --unit -q— 11 passeduv run --frozen pytest --unit --no-concurrent --ignore=tests/test_room.py -q— 1116 passed, 2 skippedmake check— formatting, Ruff, and mypy passedtests/test_room.pycould not run locally because this environment has no Docker executable; its nine failures were fixture setup errors before test execution.Scope
No new dependencies, provider calls, or unrelated inference changes.