Error trying to download csv or excel file using Assistants API

Hi,

I’m trying to download a csv (or excel) file from the results of using the Assistants API in a Jupyter notebook. I’m using the following:

fileContent = client.files.retrieve_content(
file_id=file.id
)

But, I get the following error:


BadRequestError Traceback (most recent call last)
Cell In[221], line 1
----> 1 fileContent = client.files.retrieve_content(
2 file_id=‘file-EP6HIHRTp4EDuHUByZjSDFXF’
3 )

File ~\miniconda3\envs\openai_assistants_dev\Lib\site-packages\openai\resources\files.py:224, in Files.retrieve_content(self, file_id, extra_headers, extra_query, extra_body, timeout)
211 “”"
212 Returns the contents of the specified file.
213
(…)
221 timeout: Override the client-level default timeout for this request, in seconds
222 “”"
223 extra_headers = {“Accept”: “application/json”, **(extra_headers or {})}
→ 224 return self._get(
225 f"/files/{file_id}/content",
226 options=make_request_options(
227 extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
228 ),
229 cast_to=str,
230 )

File ~\miniconda3\envs\openai_assistants_dev\Lib\site-packages\openai_base_client.py:998, in SyncAPIClient.get(self, path, cast_to, options, stream, stream_cls)
995 opts = FinalRequestOptions.construct(method=“get”, url=path, **options)
996 # cast is required because mypy complains about returning Any even though
997 # it understands the type variables
→ 998 return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))

File ~\miniconda3\envs\openai_assistants_dev\Lib\site-packages\openai_base_client.py:834, in SyncAPIClient.request(self, cast_to, options, remaining_retries, stream, stream_cls)
825 def request(
826 self,
827 cast_to: Type[ResponseT],
(…)
832 stream_cls: type[_StreamT] | None = None,
833 ) → ResponseT | _StreamT:
→ 834 return self._request(
835 cast_to=cast_to,
836 options=options,
837 stream=stream,
838 stream_cls=stream_cls,
839 remaining_retries=remaining_retries,
840 )

File ~\miniconda3\envs\openai_assistants_dev\Lib\site-packages\openai_base_client.py:877, in SyncAPIClient._request(self, cast_to, options, remaining_retries, stream, stream_cls)
874 # If the response is streamed then we need to explicitly read the response
875 # to completion before attempting to access the response text.
876 err.response.read()
→ 877 raise self._make_status_error_from_response(err.response) from None
878 except httpx.TimeoutException as err:
879 if retries > 0:

BadRequestError: Error code: 400 - {‘error’: {‘message’: ‘Not allowed to download files of purpose: assistants’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

Anyone have this issue and can it be resolved?

Thanks!

2 Likes

I had the same error, using a Jupyter notebook and retrieve_content(). I finally did get it to work but I had to retrieve the correct file_id from the assistant’s response. I instructed it to prepare the file for download, which generates a link you can’t use but also the file id which you can. Then get the file contents as binary and convert it into a file you can use. In my case it was csv.

import io

# extract the file name from the response and retrieve the content
first_thread_message = messages.data[0]  # Accessing the first ThreadMessage

if first_thread_message.file_ids:
    first_file_id = first_thread_message.file_ids[0]
    print(first_file_id)
    
else:
    print("No file_ids found in the first ThreadMessage")

# its binary, so read it and then make it a file like object
file_data = client.files.content(first_file_id)
file_data_bytes = file_data.read()
file_like_object = io.BytesIO(file_data_bytes)

#now read as csv to create df
df = pd.read_csv(file_like_object)
df

Hope that helps.

2 Likes

Thanks! I really appreciate the help!