managarten/packages/local-llm/src
Till JS e8423e7551 fix(local-llm): use two-step tokenization to fix Gemma 4 generate crash
The previous attempt to fix the "Cannot read properties of null
(reading 'dims')" chat error was incomplete: I only stopped passing
the bogus return_tensor:'pt' option to apply_chat_template. The
underlying issue was that apply_chat_template's all-in-one mode
(return_dict:true) does not produce a proper Tensor-backed
{ input_ids, attention_mask } pair for multimodal-capable processors
like Gemma4Processor — it returns a shape that has no .dims on
input_ids, so model.generate() crashes deep inside the forward pass
the moment it tries to read the sequence length.

Switch to the documented two-step pattern from the Gemma 4 model
card: call apply_chat_template with tokenize:false to get the
formatted prompt as a plain string, then run that string through
processor.tokenizer with return_tensors:'pt' to get a proper Tensor
pair. The tokenizer's return_tensors option is the *Python*
convention and IS supported by transformers.js's Tokenizer class
(the API name collision between apply_chat_template's return_tensor
boolean and Tokenizer's return_tensors string is one of those nasty
spots where the JS port intentionally diverges from Python).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 23:19:24 +02:00
..
cache.ts feat(local-llm): swap WebLLM/Qwen for transformers.js + Gemma 4 E2B 2026-04-08 22:22:32 +02:00
engine.ts fix(local-llm): use two-step tokenization to fix Gemma 4 generate crash 2026-04-08 23:19:24 +02:00
index.ts fix(local-llm): wrap @mlc-ai/web-llm in dynamic import for Docker builds 2026-04-02 12:22:20 +02:00
models.ts feat(local-llm): swap WebLLM/Qwen for transformers.js + Gemma 4 E2B 2026-04-08 22:22:32 +02:00
svelte.svelte.ts feat: rename ManaCore to Mana across entire codebase 2026-04-05 20:00:13 +02:00
types.ts feat(local-llm): swap WebLLM/Qwen for transformers.js + Gemma 4 E2B 2026-04-08 22:22:32 +02:00