{"record":{"id":"a9e102dceb3f3943","repo":"SubtitleEdit/subtitleedit","slug":"llama-cpp-returned-int-resp-statuscode-json","errorCode":null,"errorMessage":"llama.cpp returned {(int)resp.StatusCode}: {json}","messagePattern":"llama\\.cpp returned (.+?): (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/seconv/Core/LlamaCppOcrEngine.cs","lineNumber":118,"sourceCode":"        using var image = SKImage.FromBitmap(padded);\n        using var data = image.Encode(SKEncodedImageFormat.Png, 90);\n        var base64 = Convert.ToBase64String(data.ToArray());\n\n        var prompt = PromptTemplate.Replace(\"{language}\", _language);\n        var body = \"{ \\\"model\\\": \\\"\" + Escape(_modelName) + \"\\\", \\\"temperature\\\": 0, \\\"messages\\\": [ { \\\"role\\\": \\\"user\\\", \\\"content\\\": [ \" +\n                   \"{ \\\"type\\\": \\\"text\\\", \\\"text\\\": \\\"\" + Escape(prompt) + \"\\\" }, \" +\n                   \"{ \\\"type\\\": \\\"image_url\\\", \\\"image_url\\\": { \\\"url\\\": \\\"data:image/png;base64,\" + base64 + \"\\\" } } \" +\n                   \"] } ] }\";\n\n        using var content = new StringContent(body, Encoding.UTF8);\n        content.Headers.ContentType = MediaTypeHeaderValue.Parse(\"application/json\");\n\n        var resp = _httpClient.PostAsync(url, content).GetAwaiter().GetResult();\n        var bodyBytes = resp.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();\n        var json = Encoding.UTF8.GetString(bodyBytes).Trim();\n        if (!resp.IsSuccessStatusCode)\n        {\n            throw new InvalidOperationException($\"llama.cpp returned {(int)resp.StatusCode}: {json}\");\n        }\n\n        var parser = new SeJsonParser();\n        var contents = parser.GetAllTagsByNameAsStrings(json, \"content\");\n        var text = string.Join(string.Empty, contents).Trim();\n        text = text.Replace(\"\\\\n\", Environment.NewLine).Replace(\"\\\\\\\"\", \"\\\"\");\n        return text.Trim();\n    }\n\n    /// <summary>\n    /// Resolves <c>--ocr-model</c> to an installed model: a full <c>.gguf</c> path (needs its\n    /// mmproj vision-projector sidecar next to it), a curated OCR model by file/display name,\n    /// or - when omitted - the first installed curated OCR model.\n    /// </summary>\n    internal static LlamaCppModel ResolveOcrModel(string? requestedModel)\n    {\n        var curatedNames = string.Join(\", \", LlamaCppServerManager.OcrModels.Select(m => m.FileName));\n","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/seconv/Core/LlamaCppOcrEngine.cs#L100-L136","documentation":"Thrown by the llama.cpp OCR engine when the HTTP POST to the llama-server completion/chat endpoint returns a non-success HTTP status code. The response status code and the raw JSON body are embedded in the message. This is a runtime communication failure with an already-resolved llama-server instance — the server is reachable but rejected or failed the request.","triggerScenarios":"The llama-server is running but returns an error: the model failed to load (500), the request payload is malformed (400), the server is overloaded or the model is too large for available VRAM/RAM (503/500), the endpoint path is wrong (404), or authentication is required.","commonSituations":"Model file is corrupt or unsupported by the llama.cpp build; insufficient GPU/CPU memory for the model; the server was started with a text-only model (no vision support) for an OCR request; a version mismatch between the llama-server API and the client's expected endpoint shape.","solutions":["Check the embedded status code: 404 = wrong endpoint URL, 400 = bad request body, 500/503 = model/server failure — inspect the JSON body in the message for the server's error detail.","Verify the model loads correctly by starting llama-server standalone and testing with curl against the same endpoint.","Ensure sufficient VRAM/RAM for the model — try a smaller model or reduce context size.","For OCR, confirm the model has vision capability (mmproj loaded) — a text-only model cannot process the image_url field.","Update llama.cpp and the model to compatible versions."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var text = ocrEngine.Recognize(imageBitmap); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"llama.cpp returned\"))\n{\n    // Parse status code from message; inspect JSON body for server-side detail\n    // Common fixes: check model loads, reduce context size, verify vision capability\n}","preventionTips":["Test the llama-server model independently with curl before integrating OCR.","Ensure the model has vision capability (mmproj loaded) for OCR requests.","Monitor llama-server logs for model-load errors and OOM conditions.","Use a model size appropriate for available VRAM/RAM."],"tags":["llama-cpp","http-error","ocr","runtime","network"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}