Assistant API - attachments

Here’s a little demo function of uploading a file in Python. You can see getting the file id printed after you upload one.

def upload_file():
    global pwd
    menu_print()
    while True:
        filename = input("Enter the filename to upload (leave empty to exit): ").strip()
        
        if not filename:  # Exit on empty input
            print("Operation cancelled.")
            return
        
        file_path = pwd / filename
        try:
            with open(file_path, "rb") as file:
                response = client.files.create(file=file, purpose=purpose)
                print(f"File uploaded successfully: {response.filename} [{response.id}]")
                return  response.id # Exit after successful upload
        except FileNotFoundError:
            print("File not found. Please make sure the filename and path are correct.")
        except PermissionError as e:
            print(f"Permission error: {e}")
        except Exception as e:
            print(f"An unexpected error occurred: {e}")