I cannot get access to assistant files

Hi i want to know how exactly step by step i can do to access a file that an assistant has created. Here is the code i have try :

run = client.beta.threads.runs.create(

messages = client.beta.threads.messages.list(
r = messages.data[0]

# handle image file
api_response = client.files.with_raw_response.retrieve_content(r.image_file.file_id)

if api_response.status_code == 200:
  content = api_response.content
  with open('image.png', 'wb') as f:
  print('File downloaded successfully.')

This one was founded in openai forum and i get this error :

Traceback (most recent call last):
  File "C:\Users\hp\Desktop\crew ai\.venv\lib\site-packages\pydantic\main.py", line 753, in __getattr__       
    return pydantic_extra[item]
KeyError: 'image_file'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\Users\hp\Desktop\crew ai\openai assistant.py", line 86, in <module>
    api_response = client.files.with_raw_response.retrieve_content(r.image_file.file_id)
  File "C:\Users\hp\Desktop\crew ai\.venv\lib\site-packages\pydantic\main.py", line 755, in __getattr__       
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}') from exc
AttributeError: 'ThreadMessage' object has no attribute 'image_file'

I have also try the openai code :

# Retrieve the message object
message = client.beta.threads.messages.retrieve(

# Extract the message content
message_content = message.content[0].text
annotations = message_content.annotations
citations = []

# Iterate over the annotations and add footnotes
for index, annotation in enumerate(annotations):
    # Replace the text with a footnote
    message_content.value = message_content.value.replace(annotation.text, f' [{index}]')

    # Gather citations based on annotation attributes
    if (file_citation := getattr(annotation, 'file_citation', None)):
        cited_file = client.files.retrieve(file_citation.file_id)
        citations.append(f'[{index}] {file_citation.quote} from {cited_file.filename}')
    elif (file_path := getattr(annotation, 'file_path', None)):
        cited_file = client.files.retrieve(file_path.file_id)
        citations.append(f'[{index}] Click <here> to download {cited_file.filename}')
        # Note: File download functionality not implemented above for brevity

# Add footnotes to the end of the message before displaying to user
message_content.value += '\n' + '\n'.join(citations)

This time they told me that the variable 'texte when doing message_content = message.content[0].text does not exist
I really don’t understand how i can manage to do it.
If you can tell me step by step how to get the file id and how to donwload it it’s will be very usefull, thanks you

For any one with the same issue here is the solution :

from PIL import Image
import io
from openai import OpenAI

# Retrieve the message object
message = client.beta.threads.messages.retrieve(

file = message.content[0].image_file
file = client.files.content(file_id="file-wIqOLDik7Vwr0jWrP6Ee9zKh")
image = Image.open(io.BytesIO(file.content))