Files with extensions [none] are not supported for retrieval

hi i am facing error when trying to upload files to specific thread using message

  • Issue summary

  • i am building exam generator ai that takes files(books) and creates question based on the file content using django drf api

  • every thing is working smooth i uploaded the file i get file id in console when i log

  • thread is created but when message tries to access file its error and says : Files with extensions [none] are not supported for retrieval

  • then i look my storage

    it uploads files without extension so when message tries to access the file it can’t because it does not have type .

  • so how can i solve this issue

  • my code :

from openai import OpenAI
import environ

env =environ.Env()
environ.Env.read_env()

openai_api_key = env(“OPENAI_API_KEY”)
client = OpenAI(api_key=openai_api_key)

def generate_exam(prompt, the_file):
assistant_id = env(“ASSISTANT_ID”)
thread = client.beta.threads.create()
print(“Thread is created”)

# Upload file to OpenAI
uploaded_file = client.files.create(file=the_file.read(), purpose='assistants')
file_id = uploaded_file.id
print("File is uploaded: " + file_id)

# Create message with file attachment
message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content=prompt,  # Use the prompt directly as a string
    attachments=[
        {
            "file_id": file_id,
            "tools": [{"type": "file_search"}]
        }
    ]
)
print("Message is created")

# Create and monitor run
run = client.beta.threads.runs.create(
    thread_id=thread.id,
    assistant_id=assistant_id,
)
print("Run instance is created")

# Polling for run completion
while run.status != "completed":
    run = client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
    )
    print(f"Run status: {run.status}")

# Retrieve and display messages after run completion
messages = client.beta.threads.messages.list(thread_id=thread.id)
response = ""
for msg in messages.data:
    if msg.role == "assistant":
        response += msg.content[0].text.value + "\n"

return response if response else "No response generated."

I had the same issue while doing the same in Apex. I had to manually give the .PDF in it’s name in order to solve this issue. I might be wrong but that is how I solved this issue.

1 Like

Your code has no setting method for “the_file”

It also has no context manager, good for keeping the file open until the openai function is done.

I’ve been meaning to robustify some example code, so here is an upload with bonus checking and documentation.

def upload_file(file_path: str = None, purpose: str = "assistants") -> 'FileObject':
    """
    Uploads a file to the OpenAI client with a specified purpose and returns the response object.
    Default purpose is 'assistants'.

    Args:
        file_path (str, optional): The path to the file to upload. If None, it prompts the user to enter a filename.
        purpose (str, optional): The purpose of the file upload, either 'assistants' or 'fine-tune'. 
                                 Default is 'assistants'.

    Returns:
        FileObject: The Pydantic model representing the file upload response from the OpenAI API.

    Raises:
        FileNotFoundError: If the file does not exist at the specified path.
        ValueError: If no filename is provided or if an invalid purpose is specified.

    Prints:
        Success message with file details if upload is successful.
        Error message if the file is not found or other IO errors occur.
    """
    # Validate the purpose parameter
    if purpose not in ["assistants", "fine-tune"]:
        raise ValueError("Invalid purpose specified. Available options are 'assistants' and 'fine-tune'.")

    # Ensure there is a file path, either from the argument or input from the user
    if file_path is None:
        file_path = input("Enter the filename to upload: ").strip()
        if not file_path:  # Check if the input string is empty
            raise ValueError("No filename provided.")

    # Attempt to open and read the file in binary mode
    try:
        with open(file_path, "rb") as file:
            response = client.files.create(file=file, purpose=purpose)
            # Output the response and file details using direct attribute access
            print(f"File uploaded successfully: {response.filename} [{response.id}]")
            return response  # Return the full Pydantic model
    except FileNotFoundError:
        print("File not found. Please make sure the filename and path are correct.")
        raise
    except Exception as e:
        print(f"An error occurred: {e}")
        raise

It still returns a pydantic response object the same as from the openai module.

You can mute some prints if desired.

You can include more openai.ErrorTYpes…

Then to call,

try:
    upload_response = upload_file("path_to_your_file.pdf")
    print("Upload complete. Response ID:", upload_response.id)
except FileNotFoundError:
    print("Please check the file path and try again.")
except Exception as error:
    print(f"An unexpected error occurred: {error}")

(Your name “uploaded_file” makes one think the variable is a file instead of an API return object, so I changed it.)

You could make it more versatile/reliable if your file name variable is from os.path.

Then if you still get nada from the API, it may be that the file type validator and parser can’t get into the PDF binary at all to get any searchable text. This can be disallowed by password protection, or just might not be there.

unbelievable. that was the issue. lower case “pdf” extension required

1 Like