mudler/LocalAI · error

3

3

Error message

[sam3-cpp] Failed to encode image\n

What it means

Emitted by the sam3-cpp backend's C shim when sam3_encode_image() fails after an image was successfully loaded from disk. It means the SAM 3 image encoder could not preprocess/embed the loaded pixels (e.g. unsupported dimensions, corrupt pixel data, or an encoder/model state mismatch). The function returns exit code 3 so the Go caller can distinguish it from 'model not loaded' (1) and 'image load failure' (2).

Solutions

  1. Verify the image loads and has sane dimensions (open it in an image tool; re-export as PNG/JPEG) and retry
  2. Confirm the model was fully loaded first: sam3_cpp_load returned 0 and no 'Model not loaded' (code 1) was printed
  3. If it persists after a successful reload, rebuild/inspect the underlying sam3 library's encode step for the exact failure (dimensions, normalization, memory)

Example fix

// before: encode immediately after load
sam3_cpp_load(model_path, "cpu");
sam3_cpp_encode_image("weird.tiff"); // returns 3

// after: re-export to a well-supported format and check the return code
if (sam3_cpp_encode_image("input.png") != 0) {
    // handle: image load (2) vs encode (3) failure
}
Defensive patterns

Strategy: validation

Validate before calling

// Go caller: ensure model is loaded and the image decodes before encoding
if sam3Loaded != 0 {
    return fmt.Errorf("sam3 model not loaded")
}
f, err := os.Open(imagePath)
if err != nil {
    return fmt.Errorf("image unreadable: %w", err)
}
_, format, err := image.DecodeConfig(bufio.NewReader(f))
f.Close()
if err != nil || (format != "png" && format != "jpeg") {
    return fmt.Errorf("image is not png/jpeg")
}

Prevention

When it happens

Trigger: Calling sam3_cpp_encode_image(image_path) when g_model/g_state are set but the loaded image cannot be encoded: wrong image dimensions for the model, a truncated file that stb_image still decodes partially, or a model/state pair left inconsistent by a failed prior operation.

Common situations: Running segmentation (PVS/PCS) on an image the model was not prepared for, reusing a stale global state after a model reload, or feeding an exotic/corrupt image format that decodes but has invalid metadata.

Related errors


AI-assisted analysis of mudler/LocalAI@44413a9d06 (2026-08-15). Data as JSON: /api/errors/f3f244263de2c19d. Report an issue: GitHub.

Appendix: source

Thrown at backend/go/sam3-cpp/cpp/gosam3.cpp:79

    fprintf(stderr, "[sam3-cpp] Model loaded: %s (threads=%d)\n", model_path, threads);
    return 0;
}

int sam3_cpp_encode_image(const char *image_path) {
    if (!g_model || !g_state) {
        fprintf(stderr, "[sam3-cpp] Model not loaded\n");
        return 1;
    }

    sam3_image img = sam3_load_image(image_path);
    if (img.data.empty()) {
        fprintf(stderr, "[sam3-cpp] Failed to load image: %s\n", image_path);
        return 2;
    }

    if (!sam3_encode_image(*g_state, *g_model, img)) {
        fprintf(stderr, "[sam3-cpp] Failed to encode image\n");
        return 3;
    }

    return 0;
}

int sam3_cpp_segment_pvs(float *points, int n_point_triples,
                         float *boxes, int n_box_quads,
                         float threshold) {
    if (!g_model || !g_state) {
        return -1;
    }

    sam3_pvs_params pvs_params;

    // Parse points: each triple is [x, y, label]
    for (int i = 0; i < n_point_triples; i++) {
        float x = points[i * 3];

View on GitHub (pinned to 44413a9d06)