fix: think token splitting#214
Merged
artus9033 merged 3 commits intoJun 9, 2026
Merged
Conversation
|
@JKobrynski is attempting to deploy a commit to the Callstack Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens @react-native-ai/llama’s streaming adapter against cases where <think> / <tool_call> delimiters are split across streamed chunks, by extracting the delimiter-handling state machine into a dedicated parser and adding buffering/placeholder detection logic.
Changes:
- Extracted streaming parsing into
createLlamaStreamParserand integrated it intoLlamaLanguageModel#doStream. - Added buffering logic to detect
<think>/</think>and<tool_call>/</tool_call>even when split or embedded in larger chunks. - Added Bun-based regression tests for split/embedded markers and tool-call dedupe behavior; adjusted TypeScript and ESLint configs to accommodate test setup.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/llama/tsconfig.json | Excludes src/**/__tests__/** from the package typecheck inputs. |
| packages/llama/src/streamParser.ts | Introduces the extracted streaming parser/state machine with buffering + marker detection. |
| packages/llama/src/ai-sdk.ts | Replaces inline streaming parsing logic with createLlamaStreamParser. |
| packages/llama/src/tests/streamParser.test.ts | Adds regression coverage for split/embedded markers and tool-call behavior. |
| eslint.config.mjs | Adds test-only ESLint setting to treat bun:test as a core module for import linting. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
artus9033
approved these changes
Jun 9, 2026
artus9033
left a comment
Contributor
There was a problem hiding this comment.
LGTM after 1 comment resolved, great job!
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.
Related issue - #199
Summary
Makes the
@react-native-ai/llamastreaming parser resilient to split reasoning and tool-call delimiters.Previously, the fallback stream adapter assumed markers like
<think>,</think>,<tool_call>, and</tool_call>arrived as whole tokens. When tokenization split those markers across multiple streamed chunks, the parser could miss the transition entirely and leak reasoning or marker text into normal output.This change extracts the stream state machine into a dedicated helper and updates it to buffer possible placeholder fragments, detect markers even when they are split or embedded inside a larger chunk, and preserve normal text/tool-call behavior.
Changes
createLlamaStreamParserdoStream()parsing with the extracted parser helper<think>/</think>delimiters correctly<tool_call>/</tool_call>markerspackages/llamatypecheck/build inputsTesting
bun test packages/llama/src/__tests__/streamParser.test.ts./node_modules/.bin/tsc --noEmit --project packages/llama/tsconfig.json./node_modules/.bin/tsc --noEmit --project packages/llama/tsconfig.build.jsonbun lint<think>delimiter-splitting leakage no longer appears<think>/</think>markers<think>reasoning</think>final answer<tool_call>markers