How do download files generated in AI Assistants?

How do I go about downloading files generated in open AI assistant?
I have file annotations like this

 TextAnnotationFilePath(end_index=466, file_path=TextAnnotationFilePathFilePath(file_id='file-7FiD35cCwF6hv2eB7QOMT2rs'), start_index=427, text='sandbox:/mnt/data/stat_sig_pie_plot.png', type='file_path')]

I am not able to read the file object using .read()

2 Likes

I’m having the same problem. I can retrieve the ‘file’ from the API but I haven’t been able to figure out how to decode it. I am getting a PNG image created but can’t seem to get it to an openable format.

It’s crazy how hard this was to figure out - had to go digging through the SDK to put the pieces together.

This works for me:

import OpenAI from 'openai';
import fs from 'fs';

(async function run() {
  const fileid = 'file-kqzPeg6MhD0HoCaDnaK3XSJN';
  console.log('Loading ', fileid);
  const openai = new OpenAI();

  const file = await openai.files.content(fileid);

  console.log(file.headers);

  const bufferView = new Uint8Array(await file.arrayBuffer());

  fs.writeFileSync('file-kqzPeg6MhD0HoCaDnaK3XSJN.png', bufferView);
})();

Replace fileid with the image_file.file_id or other ids you get and it should work. There’s no easy way to figure out the extension programmatically other than to go digging through the headers and to parse them.

If anyone has better ways please do share them!

1 Like

I am adding some more example code in the section of the docs that goes over this: OpenAI Platform just waiting for an approval and deploy.

2 Likes

Cool thanks folks figured it out using SDK code. Forgot to update here :confused:

The text you added to the documentation says that the link is of the form:

sandbox:/mnt/data/shuffled_file.csv

But that is not a valid url at all. How we are supposed to get those files from the playground?

2 Likes

you have to use

await openai.files.content

to get the file

That is the way to download it via python programmatically, ok. It works. But what if I want to download the generated file while I’m in the playground using Safari? The link to download the file gives error and says that the javascript is not valid, while the button to download the file in the files section doesn’t do anything. Telling the Assistant that the download link doesn’t work, or asking for a link to the file, even specifying the file ID, will make the assistant answer with a link to a google server!!

‘’‘https://storage.googleapis.com/assistant-sandbox-attachments/session_WuFUqxIJG99AoTWnihXVz/file-85NrdwClpdBrNbZLywortwj9’‘’

If I ask again, the Assistant apologizes, and then answer with this other link:

‘’‘https://assistant-sandbox.dynalistcdn.com/sandbox/file-85NrdwClpdBrNbZLywortwj9’‘’

Both are completely bogus links…

Conclusion: ATM there is no way to download a file generated by the AI using the browser when using the playground to test the assistants.

I am getting the same behaviour in chatGPT… can’t download files being presented, just links to sandbox:/mnt/data/… or then “://bellard.org/textsynth/assets/chatgpt/sandbox?path=/mnt/data/image_recolored.png” if i press for alternatives.

1 Like

It is possible via the client by using the file_id.

The steps are:

  • Get the file_id from the thread
  • Load the bytes from the file using the client
  • Save the bytes to file

If working in python:

# open_ai_client = ...
# thread = ...

def get_response(thread):
    return open_ai_client.beta.threads.messages.list(thread_id=thread.id)

def get_file_ids_from_thread(thread):
    file_ids = [
        file_id
        for m in get_response(thread)
        for file_id in m.file_ids
    ]
    return file_ids

def write_file_to_temp_dir(file_id, output_path):
    file_data = open_ai_client.files.content(file_id)
    file_data_bytes = file_data.read()
    with open(output_path, "wb") as file:
        file.write(file_data_bytes)

# So to get a file and write it
file_ids = get_file_ids_from_thread(thread)
some_file_id = file_ids[0]
write_file_to_temp_dir(some_file_id, '/tmp/some_data.txt')
1 Like

This section in the documentation explains how you can download files generated by tools. https://platform.openai.com/docs/assistants/tools/file-citations

tl;dr: you have to look for file_ids in the content array of the message and then download the file using https://platform.openai.com/docs/api-reference/files/retrieve-contents

3 Likes