{"record":{"id":"3ad5df592b7816ff","repo":"janhq/jan","slug":"invalid-metadata-context-length-not-found-or-inva","errorCode":null,"errorMessage":"Invalid metadata: context_length not found or invalid","messagePattern":"Invalid metadata: context_length not found or invalid","errorType":"error_code","errorClass":"KVCacheError::ContextLengthInvalid","httpStatus":null,"severity":"error","filePath":"src-tauri/plugins/tauri-plugin-llamacpp/src/gguf/types.rs","lineNumber":71,"sourceCode":"    pub metadata: HashMap<String, String>,\n}\n\n#[derive(Debug, Serialize, Deserialize)]\npub struct KVCacheEstimate {\n    pub size: u64,\n    pub per_token_size: u64,\n}\n#[derive(Debug, thiserror::Error)]\npub enum KVCacheError {\n    #[error(\"Invalid metadata: architecture not found\")]\n    ArchitectureNotFound,\n    #[error(\"Invalid metadata: block_count not found or invalid\")]\n    BlockCountInvalid,\n    #[error(\"Invalid metadata: head_count not found or invalid\")]\n    HeadCountInvalid,\n    #[error(\"Invalid metadata: embedding_length not found or invalid\")]\n    EmbeddingLengthInvalid,\n    #[error(\"Invalid metadata: context_length not found or invalid\")]\n    ContextLengthInvalid,\n}\n\nimpl serde::Serialize for KVCacheError {\n    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>\n    where\n        S: serde::Serializer,\n    {\n        serializer.serialize_str(&self.to_string())\n    }\n}\n\n\n#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize)]\npub enum ModelSupportStatus {\n    #[serde(rename = \"RED\")]\n    Red,\n    #[serde(rename = \"YELLOW\")]","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/janhq/jan/blob/fad3f12a147d138388a66f0d92a02b2675f65294/src-tauri/plugins/tauri-plugin-llamacpp/src/gguf/types.rs#L53-L89","documentation":"`KVCacheError::ContextLengthInvalid` — the estimator could not find or parse the maximum context length (e.g. `llama.context_length`). Context length multiplies per-token KV-cache size to compute the total cache size for a full context window.","triggerScenarios":"The `<arch>.context_length` key is missing or its value does not parse to an integer. This is the last field the estimator needs; if it fails here, all prior fields (architecture, block_count, head_count, embedding_length) were already resolved successfully.","commonSituations":"Architecture-specific naming (`max_context_length`, `n_ctx`, `max_position_embeddings`); stripped metadata; a float-formatted value. Some GGUFs expose only a default context and rely on the runner to set the actual context length.","solutions":["Dump `<arch>.*` for context-related keys and add synonym fallbacks.","If only a default context exists, fall back to a sensible default (e.g. 2048) and surface that to the caller.","Parse defensively.","Re-download if metadata is genuinely incomplete."],"exampleFix":"// before\nlet ctx: u64 = meta.metadata.get(&format!(\"{}.context_length\", arch))\n    .ok_or(KVCacheError::ContextLengthInvalid)?\n    .parse().map_err(|_| KVCacheError::ContextLengthInvalid)?;\n\n// after - synonyms plus default\nlet ctx: u64 = [\"context_length\", \"max_context_length\", \"n_ctx\", \"max_position_embeddings\"].iter()\n    .find_map(|k| meta.metadata.get(&format!(\"{}.{}\", arch, k)))\n    .map(|s| s.trim().parse::<u64>().unwrap_or(2048))\n    .unwrap_or(2048);","handlingStrategy":"validation","validationCode":"fn context_len(meta: &GgufMetadata, arch: &str) -> Option<u64> {\n    [\"context_length\", \"max_context_length\", \"n_ctx\", \"max_position_embeddings\"].iter()\n        .find_map(|k| meta.metadata.get(&format!(\"{arch}.{k}\")))\n        .and_then(|s| s.trim().parse::<u64>().ok())\n}","typeGuard":"null","tryCatchPattern":"match estimate_kv_cache(&meta) {\n    Ok(est) => Ok(est),\n    Err(KVCacheError::ContextLengthInvalid) => {\n        tracing::warn!(\"context_length missing — falling back to 2048\");\n        estimate_with_default_context(&meta, 2048)\n    }\n    Err(e) => Err(e.into()),\n}","preventionTips":["Provide a sensible default context for models that omit the field.","Try multiple synonym keys before giving up.","Log the resolved value so underestimates are visible."],"tags":["gguf","metadata","context","kvcache","llamacpp","rust"],"backgroundTag":null,"analyzedSha":"fad3f12a147d138388a66f0d92a02b2675f65294","analyzedAt":"2026-08-12T20:33:47.516Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}