API Status Error in Uploading a File - API Status Error

The following code snippet to upload a file to OpenAI worked perfectly few hours ago, and it started to throw an validation error suddenly.

client.files.create(
  file=open("document.pdf", "rb"),
  purpose="fine-tune"
)


file_id = file.id

print(file_id)

Error

APIStatusError                            Traceback (most recent call last)
<ipython-input-30-b038bb4b1448> in <cell line: 1>()
----> 1 client.files.create(
      2   file=open("document.pdf", "rb"),
      3   purpose="fine-tune"
      4 )
      5 

3 frames
/usr/local/lib/python3.10/dist-packages/openai/_base_client.py in _request(self, cast_to, options, remaining_retries, stream, stream_cls)
   1010 
   1011             log.debug("Re-raising status error")
-> 1012             raise self._make_status_error_from_response(err.response) from None
   1013 
   1014         return self._process_response(

APIStatusError: Error code: 415 - {'error': {'message': "Invalid Content-Type header (application/json), expected multipart/form-data. (HINT: If you're using curl, you can pass -H 'Content-Type: multipart/form-data')", 'type': 'invalid_request_error', 'param': None, 'code': None}}

According to the documentation, all the arguments and parameters passed and declared are correct.

Is this an temporary error?

1 Like

You canā€™t fine-tune AI models with a PDF. The only valid input for fine tuning is a JSONL file.

Perhaps you meant to use the purpose ā€œassistantsā€, so the PDF could be added to a vector store?

Then, take a look at file uploading example:

curl https://api.openai.com/v1/files \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F purpose="fine-tune" \
  -F file="@mydata.jsonl"

The -F option is indeed multipart form-data, just as if you send audio for transcription with additional content:

curl https://api.openai.com/v1/audio/transcriptions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F file="@/path/to/file/audio.mp3" \
  -F "timestamp_granularities[]=word" \
  -F model="whisper-1" \
  -F response_format="verbose_json"

This function is still working fine for me, employing the Python module (1.23.6) to abstract away the HTTP method:

def upload_file():
    filename = input("Enter the filename to upload: ")
    try:
        with open(filename, "rb") as file:
            response = client.files.create(file=file, purpose="assistants")
            print(response)
            print(f"File uploaded successfully: {response.filename} [{response.id}]")
    except FileNotFoundError:
        print("File not found. Please make sure the filename and path are correct.")

The code you show also doesnā€™t set the file ID from the response, so it could not have been working as intended before.

1 Like

Thank you for lending an hand and helping me out here, @_j

I thought so too, and Iā€™ve changed the purpose to assistants

2024-04-28_22-52-47 (1)

I am using OpenAI SDK.

I donā€™t think the ā€˜purposeā€™ should matter (It might have other implications but to test file upload it shouldnā€™t matter).

I uploaded a PDF file with ā€˜fine-tuneā€™ as purpose and it worked as expected.

Looking at the error it appears to be an issue with how the request headers are configured. Are you explicitly configuring the header somewhere else in your code?

1 Like

I am explicitly configuring headers.

client = OpenAI(
    api_key=userdata.get('OPENAI_API_KEY'),
    base_url=PORTKEY_GATEWAY_URL,
    default_headers=createHeaders(
        provider="openai",
        api_key=userdata.get('PORTKEY_API_KEY_2'),
        config="pc-assist-f3c8b9" # removing this is solving the issue
    )
)

Thanks for pointing in the right direction! @gokulraya90