janhq/jan · error · KVCacheError::ContextLengthInvalid

Invalid metadata: context_length not found or invalid

Error message

Invalid metadata: context_length not found or invalid

What it means

`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.

Source

Thrown at src-tauri/plugins/tauri-plugin-llamacpp/src/gguf/types.rs:71

    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct KVCacheEstimate {
    pub size: u64,
    pub per_token_size: u64,
}
#[derive(Debug, thiserror::Error)]
pub enum KVCacheError {
    #[error("Invalid metadata: architecture not found")]
    ArchitectureNotFound,
    #[error("Invalid metadata: block_count not found or invalid")]
    BlockCountInvalid,
    #[error("Invalid metadata: head_count not found or invalid")]
    HeadCountInvalid,
    #[error("Invalid metadata: embedding_length not found or invalid")]
    EmbeddingLengthInvalid,
    #[error("Invalid metadata: context_length not found or invalid")]
    ContextLengthInvalid,
}

impl serde::Serialize for KVCacheError {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}


#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize)]
pub enum ModelSupportStatus {
    #[serde(rename = "RED")]
    Red,
    #[serde(rename = "YELLOW")]

View on GitHub (pinned to fad3f12a14)

Solutions

  1. Dump `<arch>.*` for context-related keys and add synonym fallbacks.
  2. If only a default context exists, fall back to a sensible default (e.g. 2048) and surface that to the caller.
  3. Parse defensively.
  4. Re-download if metadata is genuinely incomplete.

Example fix

// before
let ctx: u64 = meta.metadata.get(&format!("{}.context_length", arch))
    .ok_or(KVCacheError::ContextLengthInvalid)?
    .parse().map_err(|_| KVCacheError::ContextLengthInvalid)?;

// after - synonyms plus default
let ctx: u64 = ["context_length", "max_context_length", "n_ctx", "max_position_embeddings"].iter()
    .find_map(|k| meta.metadata.get(&format!("{}.{}", arch, k)))
    .map(|s| s.trim().parse::<u64>().unwrap_or(2048))
    .unwrap_or(2048);
Defensive patterns

Strategy: validation

Validate before calling

fn context_len(meta: &GgufMetadata, arch: &str) -> Option<u64> {
    ["context_length", "max_context_length", "n_ctx", "max_position_embeddings"].iter()
        .find_map(|k| meta.metadata.get(&format!("{arch}.{k}")))
        .and_then(|s| s.trim().parse::<u64>().ok())
}

Type guard

null

Try / catch

match estimate_kv_cache(&meta) {
    Ok(est) => Ok(est),
    Err(KVCacheError::ContextLengthInvalid) => {
        tracing::warn!("context_length missing — falling back to 2048");
        estimate_with_default_context(&meta, 2048)
    }
    Err(e) => Err(e.into()),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of janhq/jan@fad3f12a14 (2026-08-12). Data as JSON: /api/errors/3ad5df592b7816ff. Report an issue: GitHub.