{"record":{"id":"f3f244263de2c19d","repo":"mudler/LocalAI","slug":"3-f3f244","errorCode":"3","errorMessage":"[sam3-cpp] Failed to encode image\\n","messagePattern":"\\[sam3-cpp\\] Failed to encode image\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/go/sam3-cpp/cpp/gosam3.cpp","lineNumber":79,"sourceCode":"\n    fprintf(stderr, \"[sam3-cpp] Model loaded: %s (threads=%d)\\n\", model_path, threads);\n    return 0;\n}\n\nint sam3_cpp_encode_image(const char *image_path) {\n    if (!g_model || !g_state) {\n        fprintf(stderr, \"[sam3-cpp] Model not loaded\\n\");\n        return 1;\n    }\n\n    sam3_image img = sam3_load_image(image_path);\n    if (img.data.empty()) {\n        fprintf(stderr, \"[sam3-cpp] Failed to load image: %s\\n\", image_path);\n        return 2;\n    }\n\n    if (!sam3_encode_image(*g_state, *g_model, img)) {\n        fprintf(stderr, \"[sam3-cpp] Failed to encode image\\n\");\n        return 3;\n    }\n\n    return 0;\n}\n\nint sam3_cpp_segment_pvs(float *points, int n_point_triples,\n                         float *boxes, int n_box_quads,\n                         float threshold) {\n    if (!g_model || !g_state) {\n        return -1;\n    }\n\n    sam3_pvs_params pvs_params;\n\n    // Parse points: each triple is [x, y, label]\n    for (int i = 0; i < n_point_triples; i++) {\n        float x = points[i * 3];","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/mudler/LocalAI/blob/44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26/backend/go/sam3-cpp/cpp/gosam3.cpp#L61-L97","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the image loads and has sane dimensions (open it in an image tool; re-export as PNG/JPEG) and retry","Confirm the model was fully loaded first: sam3_cpp_load returned 0 and no 'Model not loaded' (code 1) was printed","If it persists after a successful reload, rebuild/inspect the underlying sam3 library's encode step for the exact failure (dimensions, normalization, memory)"],"exampleFix":"// before: encode immediately after load\nsam3_cpp_load(model_path, \"cpu\");\nsam3_cpp_encode_image(\"weird.tiff\"); // returns 3\n\n// after: re-export to a well-supported format and check the return code\nif (sam3_cpp_encode_image(\"input.png\") != 0) {\n    // handle: image load (2) vs encode (3) failure\n}","handlingStrategy":"validation","validationCode":"// Go caller: ensure model is loaded and the image decodes before encoding\nif sam3Loaded != 0 {\n    return fmt.Errorf(\"sam3 model not loaded\")\n}\nf, err := os.Open(imagePath)\nif err != nil {\n    return fmt.Errorf(\"image unreadable: %w\", err)\n}\n_, format, err := image.DecodeConfig(bufio.NewReader(f))\nf.Close()\nif err != nil || (format != \"png\" && format != \"jpeg\") {\n    return fmt.Errorf(\"image is not png/jpeg\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check the load call's exit code before encoding; code 1 means the model was never loaded","Normalize user uploads to PNG/JPEG before passing the path to the backend","Distinguish return codes: 1 = no model, 2 = image load, 3 = encode failure"],"tags":["sam3","image-encoding","cgo","backend"],"backgroundTag":null,"analyzedSha":"44413a9d06bf5bc52ce088ba8ca74e5a2e8bee26","analyzedAt":"2026-08-15T10:13:50.291Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}