Files
ChatGPT-Discord-Bot/docs/FILE_ACCESS_FIX.md

3.8 KiB

File Access Fix - Database Type Mismatch

Problem

Users were uploading files successfully, but when the AI tried to execute code using load_file(), it would get the error:

ValueError: File 'xxx' not found or not accessible.
No files are currently accessible. Make sure to upload a file first.

Root Cause

Data Type Mismatch in Database Query

The issue was in src/database/db_handler.py in the get_user_files() method:

What Was Happening:

  1. File Upload (code_interpreter.py):

    expires_at = (datetime.now() + timedelta(hours=48)).isoformat()
    # Result: "2025-10-04T22:26:25.044108" (ISO string)
    
  2. Database Query (db_handler.py):

    current_time = datetime.now()  # datetime object
    files = await self.db.user_files.find({
        "user_id": user_id,
        "$or": [
            {"expires_at": {"$gt": current_time}},  # Comparing string > datetime ❌
            {"expires_at": None}
        ]
    }).to_list(length=1000)
    
  3. Result: MongoDB couldn't compare ISO string with datetime object, so the query returned 0 files.

Logs Showing the Issue:

2025-10-02 22:26:25,106 - [DEBUG] Saved file metadata to database: 878573881449906208_1759418785_112e8587
2025-10-02 22:26:34,964 - [DEBUG] Fetched 0 files from DB for user 878573881449906208  ❌
2025-10-02 22:26:34,964 - [DEBUG] No files found in database for user 878573881449906208  ❌

Solution

Changed database query to use ISO string format for time comparison:

# Before:
current_time = datetime.now()  # datetime object

# After:
current_time = datetime.now().isoformat()  # ISO string

This ensures both values are ISO strings, making the MongoDB comparison work correctly.

Files Modified

  1. src/database/db_handler.py (Line 344)

    • Changed current_time = datetime.now() to current_time = datetime.now().isoformat()
    • Added debug logging to show query results
  2. src/module/message_handler.py (Lines 327-339)

    • Added comprehensive debug logging to trace file fetching
  3. src/utils/code_interpreter.py (Lines 153-160)

    • Changed insert_one to update_one with upsert=True to avoid duplicate key errors
    • Added debug logging for database saves
  4. src/module/message_handler.py (Lines 637-680, 716-720)

    • Updated data analysis feature to use load_file() with file IDs
    • Added user_files parameter to execute_code() call

Testing

After the fix, the flow should work correctly:

  1. Upload File:

    ✅ Saved file metadata to database: 878573881449906208_1759418785_112e8587
    
  2. Fetch Files:

    ✅ [DEBUG] Query returned 1 files for user 878573881449906208
    ✅ Code execution will have access to 1 file(s) for user 878573881449906208
    
  3. Execute Code:

    ✅ Processing 1 file(s) for code execution
    ✅ Added file to execution context: 878573881449906208_1759418785_112e8587 -> /path/to/file
    ✅ Total files accessible in execution: 1
    
  4. Load File in Code:

    df = pd.read_excel(load_file('878573881449906208_1759418785_112e8587'))
    # ✅ Works!
    

Restart Required

Yes, restart the bot to apply the changes:

# Stop the bot (Ctrl+C)
# Then restart:
python3 bot.py

Prevention

To prevent similar issues in the future:

  1. Consistent date handling: Always use the same format (ISO strings or datetime objects) throughout the codebase
  2. Add debug logging: Log database queries and results to catch data type mismatches
  3. Test file access: After any database schema changes, test the full file upload → execution flow
  • File upload was working
  • Database saving was working
  • Database query was failing due to type mismatch
  • Code execution couldn't find files

All issues now resolved!