HKUDS/DeepTutor · error · HTTPException

Duplicate filename after sanitization: '{duplicate_key}'. Re

Error message

Duplicate filename after sanitization: '{duplicate_key}'. Rename one of the files and try again.

What it means

Raised in _validate_upload_batch when two files in one request sanitize to the same subdir/filename key (e.g. 'report.PDF' and 'report.pdf', or 'a b.txt' and 'a_b.txt' after segment sanitization). Sanitization is lossy, so distinct raw names can collide; the batch is rejected to avoid silent overwrites.

Source

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

                size_bytes,
                allowed_extensions=allowed_extensions,
            )
        except Exception as e:
            error_message = (
                f"Validation failed for file '{original_filename}': {format_exception_message(e)}"
            )
            raise HTTPException(status_code=400, detail=error_message) from e

        rel = (
            rel_paths[idx].replace("\\", "/")
            if rel_paths and idx < len(rel_paths) and rel_paths[idx]
            else ""
        )
        subdir = _sanitize_rel_subdir(rel.rsplit("/", 1)[0]) if "/" in rel else ""
        duplicate_key = f"{subdir}/{sanitized_filename}" if subdir else sanitized_filename

        if duplicate_key in seen_names:
            raise HTTPException(
                status_code=400,
                detail=(
                    f"Duplicate filename after sanitization: '{duplicate_key}'. "
                    "Rename one of the files and try again."
                ),
            )

        seen_names.add(duplicate_key)
        validated.append(
            {
                "original_filename": original_filename,
                "sanitized_filename": sanitized_filename,
                "path": duplicate_key,
                "size_bytes": size_bytes,
            }
        )

    return validated

View on GitHub (pinned to 3e82f13042)

Solutions

  1. Rename one of the colliding files before uploading
  2. Place the files in different subfolders via rel_paths so the keys differ
  3. Deduplicate the batch locally (case-insensitive name check) before sending

Example fix

# before
files: ['Report.pdf', 'report.pdf']
# after
files: ['Report-v1.pdf', 'Report-v2.pdf']
Defensive patterns

Strategy: validation

Validate before calling

import re

def dedupe_keys(files, rel_paths=None):
    seen = set()
    for i, name in enumerate(files):
        sub = (rel_paths[i] if rel_paths and rel_paths[i] else '').rsplit('/',1)[0]
        sub = re.sub(r'[^\w-]','_', sub.strip()).lower()
        key = f"{sub}/{sanitize(name)}" if sub else sanitize(name)
        if key in seen: raise ValueError(f'collision: {key}')
        seen.add(key)

Prevention

When it happens

Trigger: Uploading multiple files whose names differ only by case, whitespace, or characters stripped by the sanitizer; or two files explicitly targeting the same rel_path in a batch upload.

Common situations: Windows/macOS case-insensitive origins exporting both 'Notes.md' and 'notes.md'; bulk uploads from different sources with near-identical names.

Related errors


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