{"record":{"id":"439632d85f0ebc53","repo":"HKUDS/DeepTutor","slug":"validation-failed-for-file-original-filename","errorCode":null,"errorMessage":"Validation failed for file '{original_filename}': {format_exception_message(e)}","messagePattern":"Validation failed for file '(.+?)': (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"deeptutor/api/routers/knowledge.py","lineNumber":468,"sourceCode":"                if _pb_sync and kb_name:\n                    try:\n                        _upload_file_to_pb(kb_name, sanitized_filename, file_path)\n                    except Exception as pb_exc:\n                        logger.debug(\n                            \"PocketBase file upload failed for '%s': %s\",\n                            sanitized_filename,\n                            pb_exc,\n                        )\n            except Exception as e:\n                if file_path and file_path.exists():\n                    try:\n                        os.unlink(file_path)\n                    except OSError:\n                        pass\n\n                error_message = f\"Validation failed for file '{original_filename}': {format_exception_message(e)}\"\n                logger.error(error_message, exc_info=True)\n                raise HTTPException(status_code=400, detail=error_message) from e\n    except Exception:\n        for written_path in written_file_paths:\n            if written_path.exists():\n                try:\n                    os.unlink(written_path)\n                except OSError:\n                    pass\n        raise\n\n    return uploaded_files, uploaded_file_paths\n\n\nasync def _save_uploaded_files_off_loop(\n    files: list[UploadFile],\n    target_dir: Path,\n    allowed_extensions: set[str] | None = None,\n    kb_name: str | None = None,\n    rel_paths: list[str] | None = None,","sourceCodeStart":450,"sourceCodeEnd":486,"githubUrl":"https://github.com/HKUDS/DeepTutor/blob/3e82f130422a813cdd73c10b21a44e9325f5821a/deeptutor/api/routers/knowledge.py#L450-L486","documentation":"Raised in _save_uploaded_files when per-file validation (extension/format/content checks via the validation pipeline) throws for an uploaded file. The already-written temp file is unlinked and the original exception's message is embedded, with the original filename (pre-sanitization) reported.","triggerScenarios":"Uploading a file that passes size checks but fails content/extension validation — e.g. mismatched extension, empty file, or unreadable encoding; raised during a batch upload, aborting the whole batch.","commonSituations":"Renamed files (doc.exe → doc.pdf), zero-byte uploads from a failed form submission, corrupt PDFs that parse headers but fail deeper validation.","solutions":["Read the embedded validation message — it names the exact check that failed","Open the file locally with a viewer for its claimed type to confirm it is valid","Re-export or fix the source file and re-upload","If the batch partially succeeded, retry with only the failing file removed"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"# open with the right parser before uploading\ndef doc_ok(path, ext):\n    if ext == '.pdf':\n        from pypdf import PdfReader; PdfReader(path).pages; return True\n    return True","typeGuard":null,"tryCatchPattern":"try: resp = upload(files)\nexcept HTTPError as e:\n    detail = e.response.json()['detail']\n    if 'Validation failed' in detail: remove_offender_and_retry(files, detail)","preventionTips":["Validate content (magic bytes / parser open) client-side","Upload batches item-by-item to isolate failures","Never rename files to a different extension"],"tags":["validation","upload","http-400"],"backgroundTag":"file-validation-failed","analyzedSha":"3e82f130422a813cdd73c10b21a44e9325f5821a","analyzedAt":"2026-08-27T06:57:25.364Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}