How to specify file name when uploading a file with Python API

I am uploading a file using the Python API with the following code:

file = client.files.create(
  file=open("speech.py", "rb"),
  purpose='assistants'
)

However all my uploads appear as “upload” in the file section. How can I change the displayed name to something more friendly? I tried to use the query_param but with no success. Thank you

1 Like

What is the “file section”?

You can write your own file browser and get the file objects, to present as you wish:


{
  "id": "file-abc123",
  "object": "file",
  "bytes": 120000,
  "created_at": 1677610602,
  "filename": "salesOverview.pdf",
  "purpose": "assistants",
}

There is no other metadata that you can add, except by tracking with your own database.

You could thus make your own “assistants file attacher” interface or such.

You can use a tuple like this:

with open("speech.py", "rb") as fp:
    file = client.files.create(
        file=("speech.py", fp),
        purpose='assistants'
    )

After struggling with the same problem, I found the answer by digging through the OpenAI code: openai._types.FileTypes.

6 Likes

This totally seems like it should be working, but it doesn’t seem to be working for me :confused:

I checked the docs and everything is like you said.

I guess it IS working, but then assistants api doesn’t use the filename? Incredibly stupid.

works perfectly good for me icl, what r u stuck on

Yep, the file search results that the AI gets do not have the source file name in the tool return, just the long file ID. A lot more tokens of a name.

The AI is instructed to write file citations that refer to the name in the file search instructions, but that is of little use for organically talking about the source of information. You’d have to provide run time system instructions, and keep track of all the connected vector stores and files inside, and provide some mapping the AI can use.

You can get the rank results in “steps”, but that is of little use because on Assistants you can’t alter the instructions during tool call.

In ChatGPT you are limited to 20 files so the AI can be provided a list of what’s behind the file search, but on the API you could potentially have thousands.

Just another reason to investigate building better document RAG yourself.

1 Like