Mintplex-Labs/anything-llm · error
Failed to serve image
Error message
Failed to serve image
What it means
Catch-all HTTP 500 from the generated-image serve handler. Any throw outside the inner readFile guard — userFromSession failures that slip through, an exception from findFileSource's database query, response.send errors, header issues — is logged server-side as '[agentFileServer] Image serve error: <message>' and returned to the client as this generic message. The real cause only exists in the server console.
Source
Thrown at server/endpoints/agentFileServer.js:135
return response
.status(404)
.json({ error: "Image not found or access denied" });
const imagePath = path.resolve(generatedImagesPath, filename);
let imageBuffer;
try {
imageBuffer = await fs.promises.readFile(imagePath);
} catch {
return response
.status(404)
.json({ error: "Image not found in storage" });
}
response.setHeader("Content-Type", "image/png");
return response.send(imageBuffer);
} catch (error) {
console.error("[agentFileServer] Image serve error:", error.message);
return response.status(500).json({ error: "Failed to serve image" });
}
}
);
}
/**
* Locates the source record (a workspace chat or a scheduled job run) that
* references the given storage filename, and confirms the requester has access.
*
* Search order:
* 1. Workspace chats the user can access (per multi-user permissions).
* 2. Scheduled job runs — single-user only, so no per-user access check.
*
* @param {string} storageFilename
* @param {{ user: object|null, isMultiUser: boolean }} ctx
* @returns {Promise<{workspaceId: number|null, displayFilename: string}|null>}
*/
async function findFileSource(storageFilename, { user, isMultiUser }) {View on GitHub (pinned to 3aec848f28)
Solutions
- Check the server console for '[agentFileServer] Image serve error:' at the request time — it carries the underlying message
- Reload/retry once; aborted-connection races and transient DB errors often clear
- If every image fails, test the DB queries behind findFileSource directly — a systemic 500 points at the authorization lookup, not the files
- Log the full error object temporarily to obtain a stack for the specific fault
Example fix
// before: message-only logging loses stacks
console.error('[agentFileServer] Image serve error:', error.message);
// after: full error while debugging
console.error('[agentFileServer] Image serve error:', error); Defensive patterns
Strategy: try-catch
Try / catch
try {
const res = await fetch(imgUrl, {credentials: 'include'});
if (res.status === 500) {
// generic by design — check the '[agentFileServer] Image serve error:' server log
// for the real cause; a single retry covers aborted-connection races
return await fetch(imgUrl, {credentials: 'include'});
}
} catch (e) {
// network-level failure reaching the server
} Prevention
- Pair every report of this 500 with the server-side console line at the same timestamp
- If all images 500 at once, suspect the DB behind findFileSource rather than individual files
- Use onerror handlers on <img> tags to swap in a placeholder instead of surfacing broken images
When it happens
Trigger: A database error inside findFileSource (the workspace-chat lookup) while authorizing the image; response.send failing after a client abort; an unexpected throw from userFromSession for a malformed session cookie; fs errors other than ENOENT surfacing from the surrounding logic.
Common situations: Broken <img> tags in production chats with no client-side detail; intermittent failures tied to DB load; post-upgrade schema mismatches in the chat tables used by findFileSource.
Related errors
- Failed to download file
- Image edit failed (${res.status}): ${body || res.statusText}
- Failed to fetch edited image: ${imgRes.status}
- Image edit returned no image data.
- Failed to fetch generated image: ${res.status}
AI-assisted analysis of Mintplex-Labs/anything-llm@3aec848f28 (2026-08-18).
Data as JSON: /api/errors/a34c25cb27153c80.
Report an issue: GitHub.