Budibase/budibase · error · HTTPError
Invalid knowledge base file id
Error message
Invalid knowledge base file id
What it means
deleteFileForOperation fetches the knowledge base file by id and requires it to carry a knowledgeBaseId. If the stored file document has no knowledgeBaseId, its ownership cannot be established, so the SDK throws HTTPError 400 "Invalid knowledge base file id" instead of deleting an unattributable file.
Solutions
- Confirm the fileId is a knowledge base file id (from uploadFileForOperation's response), not an object store key or other id.
- Inspect the file document in the database and fix/restore its knowledgeBaseId if corrupted.
- Delete and re-upload the file if the record cannot be repaired.
- Catch the 400 HTTPError and treat the file as undeletable/misattributed in the caller.
Example fix
// before
await deleteFileForOperation(agentId, operationId, someKey)
// after
const file = await getKnowledgeBaseFileOrThrow(someKey)
if (!file.knowledgeBaseId) {
// wrong id type — use the kb file id from the upload response
}
await deleteFileForOperation(agentId, operationId, uploadResponse.fileId) Defensive patterns
Strategy: validation
Validate before calling
const file = await getKnowledgeBaseFileOrThrow(fileId)
if (!file.knowledgeBaseId) throw new Error("fileId does not reference a valid knowledge base file") Type guard
const hasKbId = (f: KnowledgeBaseFile): f is KnowledgeBaseFile & { knowledgeBaseId: string } =>
typeof f.knowledgeBaseId === "string" && f.knowledgeBaseId.length > 0 Try / catch
try {
await deleteFileForOperation(agentId, operationId, fileId)
} catch (e) {
if (e instanceof HTTPError && e.status === 400) {
// treat as invalid/unattributable file id
}
throw e
} Prevention
- Store the fileId returned from uploadFileForOperation, not object-store keys
- Type file references to distinguish knowledge base file ids from storage keys
- Audit for file records missing knowledgeBaseId after schema migrations
- Re-upload files whose records cannot be repaired
When it happens
Trigger: Calling deleteFileForOperation with a fileId that resolves to a file document missing the knowledgeBaseId field — corrupted record, manually seeded data, or a fileId that is not a real knowledge base file id shape.
Common situations: Passing an object-store key or random id instead of the knowledge base file id; records created before a schema change added knowledgeBaseId; partial writes during file upload.
Related errors
- AI message content must be a string
- AI message content must be a string
- AI user message must be a string
- Cannot create a query tool without a query ID
- Failed to create operation file storage
AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29).
Data as JSON: /api/errors/8daacc47a8b0b9ff.
Report an issue: GitHub.
Appendix: source
Thrown at packages/server/src/sdk/workspace/ai/rag/files.ts:231
return await knowledgeBaseSdk.uploadKnowledgeBaseFile({
knowledgeBaseId,
filename: input.filename,
mimetype: input.mimetype,
size: input.size ?? input.buffer.byteLength,
buffer: input.buffer,
uploadedBy: input.uploadedBy,
})
}
export const deleteFileForOperation = async (
agentId: string,
operationId: string,
fileId: string
): Promise<void> => {
const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)
const fileKnowledgeBaseId = file.knowledgeBaseId
if (!fileKnowledgeBaseId) {
throw new HTTPError("Invalid knowledge base file id", 400)
}
const knowledgeBaseIds = await getKnowledgeBaseIdsForOperation(
agentId,
operationId
)
if (!knowledgeBaseIds.includes(fileKnowledgeBaseId)) {
throw new HTTPError("File does not belong to this operation", 404)
}
const knowledgeBase = await knowledgeBaseSdk.find(fileKnowledgeBaseId)
if (!knowledgeBase) {
throw new HTTPError("Operation file storage not found", 404)
}
await knowledgeBaseSdk.removeKnowledgeBaseFile(knowledgeBase, file)
}
const removeKnowledgeBaseWithFiles = async (
knowledgeBaseId: string,View on GitHub (pinned to a81a902e9a)