Resource Not Found Error when Using Files API

I am using Python OpenAI version 1.30.0 and python version 3.9 .
I am getting resource not found error when trying to create a file using files api

Code :

client = OpenAI(api_key="key1", base_url="url1")
batch_input_file = client.files.create(file=open("batchinput.jsonl", "rb"), purpose="batch")

Error traceback :
    batch_input_file = client.files.create(file=open("batchinput.jsonl", "rb"), purpose="batch")
  File "/python3.10/site-packages/openai/resources/files.py", line 113, in create
    return self._post(
  File "/python3.10/site-packages/openai/_base_client.py", line 1240, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "python3.10/site-packages/openai/_base_client.py", line 921, in request
    return self._request(
  File "/python3.10/site-packages/openai/_base_client.py", line 961, in _request
    return self._retry_request(
  File "python3.10/site-packages/openai/_base_client.py", line 1053, in _retry_request
    return self._request(
  File "python3.10/site-packages/openai/_base_client.py", line 961, in _request
    return self._retry_request(
  File "python3.10/site-packages/openai/_base_client.py", line 1053, in _retry_request
    return self._request(
  File "python3.10/site-packages/openai/_base_client.py", line 1020, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {'statusCode': 404, 'message': 'Resource not found'}

I believe this maybe because the base_url I am using does not support files api . How can I find more about the base_url. Also when I’m not using any base url I get the same error.

You should never have to specify a base URL when connecting to OpenAI. The client software module already knows.

API key also is automatically loaded from an environment variable, and should not be in code.

Actually , when not using the base url and just creating :

client = OpenAI(api_key=“key1”)

I am getting this same error :

batch_input_file = client.files.create(file=open(“batchinput.jsonl”, “rb”), purpose=“batch”)
File “/python3.10/site-packages/openai/resources/files.py”, line 113, in create
return self._post(
File “/python3.10/site-packages/openai/_base_client.py”, line 1240, in post
return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
File “python3.10/site-packages/openai/_base_client.py”, line 921, in request
return self._request(
File “/python3.10/site-packages/openai/_base_client.py”, line 961, in _request
return self._retry_request(
File “python3.10/site-packages/openai/_base_client.py”, line 1053, in _retry_request
return self._request(
File “python3.10/site-packages/openai/_base_client.py”, line 961, in _request
return self._retry_request(
File “python3.10/site-packages/openai/_base_client.py”, line 1053, in _retry_request
return self._request(
File “python3.10/site-packages/openai/_base_client.py”, line 1020, in _request
raise self._make_status_error_from_response(err.response) from None
openai.NotFoundError: Error code: 404 - {‘statusCode’: 404, ‘message’: ‘Resource not found’}

I don’t think it’s a version issue either .

The API is working just fine…

Enter your choice: 5
Purpose changed to: ‘batch’
== OpenAI File Storage Utility ==
– Main Menu –
[1] Change purpose (to access different type of API files)
[2] Change local directory (the upload source)
[3] Upload file
[4] List remote files
[5] List all remote and delete one of your choice
[6] List all remote and download one of your choice
[7] Delete all files with current purpose (confirmation required)
[9] Exit
(current mode: purpose=‘batch’)

==========

Enter your choice: 3
== OpenAI File Storage Utility ==
– Upload File –

(current mode: purpose=‘batch’)

==========

Enter the filename to upload (leave empty to exit): batchtest.jsonl
File uploaded successfully: batchtest.jsonl [file-QySa8yIV4SlN4N6BNEdhsf8W]

with this interactive method (that I had an AI make reusable):

def upload_file(pwd, client, purpose):
    """
    Uploads a file located at `pwd` directory using `client` to a specified `purpose`.
    
    Args:
    - pwd (Path or str): Path to the directory where the file is located.
    - client: The client object used to interact with the file service.
    - purpose (str): Purpose or description for uploading the file.
    """
    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  # 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}")

# Example usage:
# Assuming `pwd`, `client`, and `purpose` are defined elsewhere in your code
# and passed to the function.
# upload_file(pwd, client, purpose)

You only need:

from openai import OpenAI
client = OpenAI()

having set the environment variable OPENAI_API_KEY (which I recommend you do now before you write even more code with secrets within.)

Is it possible that

https://api.openai.com/v1/files 

doesn’t work because I’m under a vpn and some more security layers which I don’t know.

If you can use simpler API endpoints, such as listing models, I don’t see any reason for you to get 404’d on that specific endpoint.

Are you able to list files? Something simple like

  # may want to wrap with try+except
  for fx in client.files.list():
    print(fx.filename)

On trying to list files , I’m getting

API connection error :
File "/opt/genai-
x = client.files.list()
File “python3.10/site-packages/openai/resources/files.py”, line 181, in list
return self._get_api_list(
File “python3.10/site-packages/openai/_base_client.py”, line 1289, in get_api_list
return self._request_api_list(model, page, opts)
File “python3.10/site-packages/openai/_base_client.py”, line 1134, in _request_api_list
return self.request(page, options, stream=False)
File “python3.10/site-packages/openai/_base_client.py”, line 921, in request
return self._request(
File “python3.10/site-packages/openai/_base_client.py”, line 976, in _request
return self._retry_request(
File “python3.10/site-packages/openai/_base_client.py”, line 1053, in _retry_request
return self._request(
File “python3.10/site-packages/openai/_base_client.py”, line 976, in _request
return self._retry_request(
File “python3.10/site-packages/openai/_base_client.py”, line 1053, in _retry_request
return self._request(
File “python3.10/site-packages/openai/_base_client.py”, line 986, in _request
raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

You can try to ping the actual API domain, then install and use ping3 in Python to see if the code has different problems contacting the site.

Here’s a little batch or script that can be run on console against the Python install to get everything not just meeting version requirements but updated to the latest version needed, although library versions are an unlikely source of such an issue.

pip install --upgrade "regex>=2022.1.18"
pip install --upgrade "distro>=1.7.0,<2"
pip install --upgrade "idna>=2.8,<4"
pip install --upgrade "charset-normalizer>=2,<4"
pip install --upgrade "urllib3>=1.21.1,<3"
pip install --upgrade "certifi>2024.6"
pip install --upgrade "requests>=2.32"
pip install --upgrade "sniffio>=1.1"
pip install --upgrade "anyio>=3.5.0,<5"
pip install --upgrade "h11>=0.13,<0.15"
pip install --upgrade "httpcore==1.*"
pip install --upgrade "httpx>=0.23.0,<1"
pip install --upgrade "annotated-types>=0.4.0"
pip install --upgrade "typing-extensions>4.7,<5"
pip install --upgrade "pydantic-core==2.18.4"
pip install --upgrade "pydantic>=1.9.0,<3"
pip install --upgrade "colorama"
pip install --upgrade "tqdm>4"
pip install --upgrade "openai>=1.35.7"
pip install --upgrade "tiktoken>=0.7"

If you have curl and you suspect a vpn problem, try a command like

curl https://api.openai.com/v1/models -H "Authorization: Bearer test_badapikey"

You should get a error code of “invalid_api_key” if you are able to connect. If you’re still getting a connect error, then your vpn connection is suspect.

1 Like

I have curl . On running this command I get :
Could not resolve host: api.openai.com

That means your nameserver is not working properly and is unable to resolve the name to an IP address. Somehow, though, you’re able to respond to this site. Don’t know what networking problems you’re having. Sorry.

In that case, DNS lookup to translate the name to an IP address is not even working.

Perhaps you have a proxy setting on your OS that is directing you to nowhere. VPN will sometimes mess with this, while a browser may still be able to connect with its independent setting and alternate DNS.

Alternately, you may require outbound connections only through a firewall proxy in some business environments.

ping, recommended above, would make a non-secure test.

Hi , ideally what response should I get when I’m trying to list files if I’ve never created any files before ?

x=client.files.list()
return x

You should get an empty array. Or if you use curl

curl https://api.openai.com/v1/files -H "YOUR_GOOD_KEY_HERE"

You’ll get something like

{
  "object": "list",
  "data": [],
  "has_more": false
}

Hi , actually I need to use azure api’s endpoint to create the base url of openai .
Default openai’s base url : https://api.openai.com/v1
What should be the new url like :
following formats give me resource not found error
{azure_openai_base_url}/v1
{azure_openai_base_url}/openai/v1