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.
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’}
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.)
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.
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.
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 , 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