{"record":{"id":"6cac32df47d579c7","repo":"huggingface/candle","slug":"text-embeddings-cannot-be-empty","errorCode":null,"errorMessage":"text_embeddings cannot be empty","messagePattern":"text_embeddings cannot be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/z_image/preprocess.rs","lineNumber":46,"sourceCode":"\n/// Pad variable-length text embeddings to uniform length\n///\n/// # Arguments\n/// * `text_embeddings` - Variable-length text embeddings, each of shape (seq_len, dim)\n/// * `pad_value` - Padding value (typically 0.0)\n/// * `device` - Device\n///\n/// # Returns\n/// * Padded tensor (B, max_len, dim)\n/// * Attention mask (B, max_len), 1=valid, 0=padding\n/// * Original lengths\npub fn pad_text_embeddings(\n    text_embeddings: &[Tensor],\n    pad_value: f32,\n    device: &Device,\n) -> Result<(Tensor, Tensor, Vec<usize>)> {\n    if text_embeddings.is_empty() {\n        candle::bail!(\"text_embeddings cannot be empty\");\n    }\n\n    let batch_size = text_embeddings.len();\n    let dim = text_embeddings[0].dim(1)?;\n    let dtype = text_embeddings[0].dtype();\n\n    // Compute max length and align to SEQ_MULTI_OF\n    let lengths: Vec<usize> = text_embeddings\n        .iter()\n        .map(|t| t.dim(0))\n        .collect::<Result<Vec<_>>>()?;\n    let max_len = *lengths.iter().max().unwrap();\n    let padded_len = max_len + compute_padding_len(max_len);\n\n    // Build padded tensor and mask\n    let mut padded_list = Vec::with_capacity(batch_size);\n    let mut mask_list = Vec::with_capacity(batch_size);\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/z_image/preprocess.rs#L28-L64","documentation":"pad_text_embeddings requires at least one text embedding tensor to batch and pad; when the input slice is empty there is no dimension/dtype to infer and no batch to produce, so it bails with 'text_embeddings cannot be empty' at candle-transformers/src/models/z_image/preprocess.rs:46. It is called from prepare_inputs, so this surfaces when preparing Z-Image model inputs.","triggerScenarios":"Calling pad_text_embeddings(&[], pad_value, &device) directly, or calling prepare_inputs with an empty list of text embeddings — typically when the text-encoding step produced no outputs (e.g. empty prompt list, encoder skipped, or embeddings filtered out upstream).","commonSituations":"Passing an empty prompt/batch list to an image-generation pipeline; filtering embeddings by a mask or length threshold that removes everything; forgetting to run the text-encoder step before prepare_inputs; collecting embeddings into a Vec that silently stayed empty due to an earlier error handled with a default.","solutions":["Ensure at least one text embedding tensor is produced before calling prepare_inputs/pad_text_embeddings — check that the prompt list is non-empty and the text encoder actually ran","Guard the call site: return early or substitute a default embedding when the slice is empty","Fix upstream filtering logic (masks, length cutoffs) that can empty the embeddings list","If calling pad_text_embeddings directly, validate text_embeddings.len() > 0 before invoking"],"exampleFix":"// before\nlet (padded, mask, sizes) = pad_text_embeddings(&embeddings, 0.0, &device)?;\n// after\nif embeddings.is_empty() {\n    anyhow::bail!(\"no text embeddings produced; check prompts and text encoder\");\n}\nlet (padded, mask, sizes) = pad_text_embeddings(&embeddings, 0.0, &device)?;","handlingStrategy":"validation","validationCode":"fn ensure_non_empty_embeddings(embeds: &[Tensor]) -> Result<(), String> {\n    if embeds.is_empty() {\n        return Err(\"text_embeddings cannot be empty\".to_string());\n    }\n    Ok(())\n}","typeGuard":"fn has_embeddings(embeds: &[Tensor]) -> bool { !embeds.is_empty() }","tryCatchPattern":"match pad_text_embeddings(&embeddings, 0.0, &device) {\n    Ok((padded, mask, sizes)) => { /* proceed */ }\n    Err(e) if e.to_string().contains(\"cannot be empty\") => {\n        eprintln!(\"no text embeddings produced; check prompts/text encoder: {e}\");\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Check that the prompt list is non-empty before building model inputs","Always run the text-encoding step before prepare_inputs","Review any filtering/masking of embeddings so it cannot remove all entries","Fail loudly (log or propagate) when embedding collection yields zero items instead of passing an empty slice down"],"tags":["rust","candle","validation","empty-input"],"backgroundTag":null,"analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}