{"record":{"id":"8daacc47a8b0b9ff","repo":"Budibase/budibase","slug":"invalid-knowledge-base-file-id","errorCode":null,"errorMessage":"Invalid knowledge base file id","messagePattern":"Invalid knowledge base file id","errorType":"http","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/rag/files.ts","lineNumber":231,"sourceCode":"  return await knowledgeBaseSdk.uploadKnowledgeBaseFile({\n    knowledgeBaseId,\n    filename: input.filename,\n    mimetype: input.mimetype,\n    size: input.size ?? input.buffer.byteLength,\n    buffer: input.buffer,\n    uploadedBy: input.uploadedBy,\n  })\n}\n\nexport const deleteFileForOperation = async (\n  agentId: string,\n  operationId: string,\n  fileId: string\n): Promise<void> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const fileKnowledgeBaseId = file.knowledgeBaseId\n  if (!fileKnowledgeBaseId) {\n    throw new HTTPError(\"Invalid knowledge base file id\", 400)\n  }\n  const knowledgeBaseIds = await getKnowledgeBaseIdsForOperation(\n    agentId,\n    operationId\n  )\n  if (!knowledgeBaseIds.includes(fileKnowledgeBaseId)) {\n    throw new HTTPError(\"File does not belong to this operation\", 404)\n  }\n\n  const knowledgeBase = await knowledgeBaseSdk.find(fileKnowledgeBaseId)\n  if (!knowledgeBase) {\n    throw new HTTPError(\"Operation file storage not found\", 404)\n  }\n  await knowledgeBaseSdk.removeKnowledgeBaseFile(knowledgeBase, file)\n}\n\nconst removeKnowledgeBaseWithFiles = async (\n  knowledgeBaseId: string,","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/rag/files.ts#L213-L249","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait deleteFileForOperation(agentId, operationId, someKey)\n// after\nconst file = await getKnowledgeBaseFileOrThrow(someKey)\nif (!file.knowledgeBaseId) {\n  // wrong id type — use the kb file id from the upload response\n}\nawait deleteFileForOperation(agentId, operationId, uploadResponse.fileId)","handlingStrategy":"validation","validationCode":"const file = await getKnowledgeBaseFileOrThrow(fileId)\nif (!file.knowledgeBaseId) throw new Error(\"fileId does not reference a valid knowledge base file\")","typeGuard":"const hasKbId = (f: KnowledgeBaseFile): f is KnowledgeBaseFile & { knowledgeBaseId: string } =>\n  typeof f.knowledgeBaseId === \"string\" && f.knowledgeBaseId.length > 0","tryCatchPattern":"try {\n  await deleteFileForOperation(agentId, operationId, fileId)\n} catch (e) {\n  if (e instanceof HTTPError && e.status === 400) {\n    // treat as invalid/unattributable file id\n  }\n  throw e\n}","preventionTips":["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"],"tags":["ai","rag","validation","http-error-400"],"backgroundTag":"invalid-resource-id","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}