Repository navigation
Description
Overview
Gemma 4 launched April 2, 2026 (Apache 2.0). The E4B variant was partially validated in the NLP experiment repo against llama-cpp-2 v0.1.143 — chat streaming works, but tool calling and remaining experiments are incomplete, and the production code paths (parser, chat template fallback) need updating before either variant can ship as a user-selectable model.
This issue covers completing validation of Gemma 4 E4B, adding both E4B and 27B as downloadable/selectable model options in the nlp-engine package, and making the parser and architecture changes required to support Gemma 4's tool-call schema alongside Mistral's.
Problem Statement
NodeSpace currently targets Ministral 3B (default) and 8B (upgrade path). Gemma 4 E4B shows significantly stronger reasoning (GPQA Diamond: 58.6% vs 33.8% / 39.9% for Ministral 3B / 8B) with competitive inference speed (20.1 t/s, 265ms TTFT on M2 Pro 16GB) and a much faster model load time (0.67s vs 12.9s for Ministral 8B). Gemma 4 27B is the larger quality-tier option for users with 24GB+ Apple Silicon.
Neither variant is wired into the production model registry, download flow, or chat pipeline. Two production code gaps must be closed:
-
Chat template fallback:
apply_chat_templatereturnsffi error -1for Gemma 4 because its Jinja template applies anupperfilter to an Array type that the bundled llama.cpp in v0.1.143 doesn't support. The fix (manualbuild_gemma4_promptformatter using Gemma 4's native turn format) was validated in the experiment but is not in production code. -
Tool-call parser: Gemma 4 uses
tool_name/tool_argsfields instead of Mistral'sname/arguments. The streaming parser must branch on model family to apply the correct schema.
Supporting multiple model families with incompatible prompt formats and tool-call schemas requires a deliberate architectural decision — see Technical Specifications below.
Proposed Solution
- Complete the remaining NLP experiment validations (Experiments 2–5) for Gemma 4 E4B to confirm tool calling, dual-model loading, context stress, and download behavior.
- Introduce a
ModelFamilyenum carried by each model registry entry; all per-family dispatch (prompt formatting, tool-call parsing, stop tokens) branches on this single value. - Implement the
apply_chat_templatefallback inpackages/nlp-enginefor Gemma 4 models. - Update the streaming tool-call parser to handle Gemma 4's
tool_name/tool_argsschema. - Register Gemma 4 E4B and 27B (Q4_K_M) in the model registry with HuggingFace download URLs, SHA256 hashes, and hardware requirement metadata.
- Expose both models in the model selection UI as downloadable options.
Implementation Approach
Phase 1: Complete E4B Validation (NLP Experiment Repo)
- Run Experiment 2 (tool calling) with Gemma 4 E4B and document parse rate and format
- Run Experiment 3 (dual model) and record combined RSS, VRAM, and embedding latency
- Run Experiment 4 (context stress) across 1k / 4k / 8k / 16k / 32k context sizes
- Run Experiment 5 (model download) to confirm HuggingFace range request behavior
- Update the NLP experiment README with all results
Phase 2: ModelFamily Enum and Per-Family Dispatch
- Define
ModelFamilyenum with at minimumMistralandGemma4variants - Each
ModelEntryin the registry carries afamily: ModelFamilyfield - All downstream code that needs per-family behavior matches on this enum — no string sniffing, no filename heuristics
- This is the single place that knows "this loaded model is Gemma 4"; prompt formatting, tool-call parsing, and stop token handling all derive from it
Phase 3: Parser and Template Updates (nlp-engine)
- Add
build_gemma4_promptto the production chat module: formats messages using Gemma 4's<start_of_turn>user/model...<end_of_turn>syntax, folds system prompt into the first user turn - Wrap
apply_chat_templatecall with fallback: try template first, fall back tobuild_gemma4_prompton error (or skipapply_chat_templateentirely for Gemma 4 until the upstream Jinja bug is fixed) - Update the streaming tool-call parser to branch on
ModelFamily:tool_name/tool_argsforGemma4,name/argumentsforMistral - Note: the Jinja
upper-on-Array bug is a llama.cpp tooling limitation, not a permanent Gemma 4 property. Once upstream fixes it, the manual fallback can be removed and both families route throughapply_chat_template. TheModelFamilyenum and parser branching remain regardless — the tool-call schema difference is a model design choice.
Phase 4: Model Registry and Download Flow
- Add Gemma 4 E4B entry: HuggingFace repo, filename, Q4_K_M GGUF, SHA256, ~5.0GB size,
family: ModelFamily::Gemma4, minimum hardware recommendation - Add Gemma 4 27B entry: same fields, size and SHA256 confirmed from HuggingFace at implementation time,
family: ModelFamily::Gemma4 - Wire both entries into the existing model download flow (resumable HTTP range requests, SHA256 verification)
Phase 5: Model Selection UI
- Surface Gemma 4 E4B and 27B in the model selection component alongside existing Ministral options
- Display hardware requirements and GGUF size so users can assess compatibility before downloading
Acceptance Criteria
- Gemma 4 E4B Experiment 2 (tool calling) completed and documented — parse rate and tool schema confirmed
- Gemma 4 E4B Experiment 3 (dual model) completed — combined RSS and embedding latency recorded
- Gemma 4 E4B Experiment 4 (context stress) completed across all context sizes
- Gemma 4 E4B Experiment 5 (model download) completed — resume and integrity verification confirmed
-
ModelFamilyenum defined; all model registry entries carry afamilyfield - All per-family dispatch (prompt format, tool-call parsing, stop tokens) branches on
ModelFamily— no string sniffing -
build_gemma4_promptimplemented in production nlp-engine with correct turn format -
apply_chat_templatecall wrapped with Gemma 4 fallback — no ffi errors at runtime - Streaming parser handles both Gemma 4 (
tool_name/tool_args) and Mistral (name/arguments) schemas - Gemma 4 E4B registered in model registry with download URL, SHA256, and size
- Gemma 4 27B registered in model registry with download URL, SHA256, and size
- Both models appear in model selection UI with hardware requirements visible
- Download, load, and chat streaming work end-to-end for Gemma 4 E4B on M2 Pro 16GB
- Tests pass (
bun run test:all) - Code passes
bun run quality:fix
Technical Specifications
Reference Files
- NLP experiment README:
~/nodespace/nodespace-nlp-experiment/README.md— validated workaround patterns and benchmark data - nlp-engine chat module:
packages/nlp-engine/src/— location forbuild_gemma4_prompt, template fallback, andModelFamilydispatch - Streaming parser: tool-call parsing logic in
packages/nlp-engine— needs schema branching onModelFamily - Model registry: wherever model metadata (URLs, hashes, sizes) is defined in
packages/nlp-engineorpackages/core - Model selection UI:
packages/desktop-app/src/— model download/selection component
Key Implementation Details
ModelFamily enum (Rust sketch):
pub enum ModelFamily {
Mistral,
Gemma4,
}
pub struct ModelEntry {
pub id: &'static str,
pub family: ModelFamily,
pub display_name: &'static str,
pub gguf_size_bytes: u64,
pub min_memory_gb: u8,
pub download_url: &'static str,
pub sha256: &'static str,
}Per-family dispatch centralised in one match — adding a new model family means adding one enum variant and implementing the match arms, not scattering conditionals:
fn format_prompt(family: &ModelFamily, messages: &[Message]) -> String {
match family {
ModelFamily::Mistral => apply_chat_template_or_fallback(messages),
ModelFamily::Gemma4 => build_gemma4_prompt(messages),
}
}
fn parse_tool_call(family: &ModelFamily, output: &str) -> Option<ToolCall> {
match family {
ModelFamily::Mistral => parse_mistral_tool_call(output),
ModelFamily::Gemma4 => parse_gemma4_tool_call(output),
}
}Gemma 4 prompt format (manual fallback):
<start_of_turn>user
[system prompt folded here if present]
[user message]<end_of_turn>
<start_of_turn>model
[assistant message]<end_of_turn>
<start_of_turn>user
[next user message]<end_of_turn>
<start_of_turn>model
Tool-call schema difference:
- Mistral:
{"name": "fn_name", "arguments": {...}} - Gemma 4:
{"tool_name": "fn_name", "tool_args": {...}}
Validated performance (E4B, M2 Pro 16GB):
- Tokens/sec: 20.1
- TTFT: 265ms
- Model load: 0.67s
- GGUF size: ~5.0GB (Q4_K_M)
Gemma 4 27B notes:
- Not yet benchmarked locally; recommended minimum hardware is 24GB+ Apple Silicon
- GGUF download URL and SHA256 to be confirmed from HuggingFace at implementation time
Hardware Requirement Tags (suggested)
- E4B: suitable for 16GB+ Apple Silicon
- 27B: recommended for 24GB+ Apple Silicon (M3 Pro/Max, M4 Pro)
Non-Goals
- Multimodal (vision/audio) input support — Gemma 4 supports it but NodeSpace has no vision pipeline yet; text-only for now
- Automatic model upgrade prompts — users choose models manually
- Benchmarking on non-Apple-Silicon hardware
Activity
malibio commentedon May 17, 2026
Baseline test results (worktree at issue-1094-gemma-4-models, commit 82f14bf):
Frontend (
bun run test):Starting work on this issue. Will use
../nodespace-nlp-experimentas reference for Gemma 4 validation (Experiments 2-5 already pass there per README — citing rather than re-running). Implementation diverges from issue body: usingapply_chat_template_oaicompatwithuse_jinja: true(validated in experiment repo) instead of writing a manualbuild_gemma4_promptformatter. Cleaner, less per-family code.Add Gemma 4 E4B and 31B as downloadable model options (closes #1094)
Add Gemma 4 E4B and 31B as downloadable model options (#1094) (#1134)
Add Gemma 4 E4B and 31B as downloadable model options (#1094) (#1134)