HKUDS/DeepTutor · error · HTTPException

Validation failed for file '{original_filename}': {format_ex

Error message

Validation failed for file '{original_filename}': {format_exception_message(e)}

What it means

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.

Source

Thrown at deeptutor/api/routers/knowledge.py:468

                if _pb_sync and kb_name:
                    try:
                        _upload_file_to_pb(kb_name, sanitized_filename, file_path)
                    except Exception as pb_exc:
                        logger.debug(
                            "PocketBase file upload failed for '%s': %s",
                            sanitized_filename,
                            pb_exc,
                        )
            except Exception as e:
                if file_path and file_path.exists():
                    try:
                        os.unlink(file_path)
                    except OSError:
                        pass

                error_message = f"Validation failed for file '{original_filename}': {format_exception_message(e)}"
                logger.error(error_message, exc_info=True)
                raise HTTPException(status_code=400, detail=error_message) from e
    except Exception:
        for written_path in written_file_paths:
            if written_path.exists():
                try:
                    os.unlink(written_path)
                except OSError:
                    pass
        raise

    return uploaded_files, uploaded_file_paths


async def _save_uploaded_files_off_loop(
    files: list[UploadFile],
    target_dir: Path,
    allowed_extensions: set[str] | None = None,
    kb_name: str | None = None,
    rel_paths: list[str] | None = None,

View on GitHub (pinned to 3e82f13042)

Solutions

  1. Read the embedded validation message — it names the exact check that failed
  2. Open the file locally with a viewer for its claimed type to confirm it is valid
  3. Re-export or fix the source file and re-upload
  4. If the batch partially succeeded, retry with only the failing file removed
Defensive patterns

Strategy: try-catch

Validate before calling

# open with the right parser before uploading
def doc_ok(path, ext):
    if ext == '.pdf':
        from pypdf import PdfReader; PdfReader(path).pages; return True
    return True

Try / catch

try: resp = upload(files)
except HTTPError as e:
    detail = e.response.json()['detail']
    if 'Validation failed' in detail: remove_offender_and_retry(files, detail)

Prevention

When it happens

Trigger: 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.

Common situations: Renamed files (doc.exe → doc.pdf), zero-byte uploads from a failed form submission, corrupt PDFs that parse headers but fail deeper validation.

Related errors


AI-assisted analysis of HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/439632d85f0ebc53. Report an issue: GitHub.