Invalid Purpose Error when Using file.create in OpenAI Azure

I’m attempting to use file.create to feed a CSV file of data to an assistant I’m creating. The idea is that the assistant would leverage the data provided for analysis. I’ve been unable to do this both via the Python API and the openai CLI due to HTTP 400 Error response codes, Error code: 400 - {‘error’: {‘code’: ‘invalidPayload’, ‘message’: ‘Invalid value for purpose.’}}.

Example of how I’m running cli:

$ openai -k “$OPENAI_API_KEY” -t azure --api-version “2023-07-01-preview” --azure-endpoint hxxps://xxxxxx.openai.azure.com api files.create -f …\results.csv -p answers

Example of Python code:

import coloredlogs
import csv
import http.client
import logging
import os
import sys
import time
from openai import AzureOpenAI
from openai._exceptions import BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError
from openai._exceptions import ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError
from openai._exceptions import APIConnectionError, APIStatusError
from pathlib import Path

http.client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger(“http.client”)
coloredlogs.install(level=“DEBUG”, logger=requests_log)

client = AzureOpenAI(
api_version = “2023-12-01-preview”,
azure_endpoint = “hxxps://xxxxxx.openai.azure.com/”,
api_key = os.getenv(“OPENAI_API_KEY”)
)

def set_assistant_file():
try:
file = client.files.create(
file=open(sys.argv[1],“rb”),
purpose=‘assistants’
)
return file
except APIConnectionError as err:
print(f"APIConnectionError: {str(err)}“)
print(err.response)
except APIStatusError as err:
print(f"APIStatusError: {str(err)}”)
print(err.response)
except BadRequestError as err:
print(f"BadRequestError: {str(err)}“)
except TypeError as err:
print(f"TypeError: {str(err)}”)

if name == “main”:
main()

Output is as follows:

2023-11-24 13:50:49 ZFNQ openai._base_client[39316] DEBUG Request options: {‘method’: ‘post’, ‘url’: ‘/files’, ‘headers’: {‘Content-Type’: ‘multipart/form-data’, ‘api-key’: ‘xxxx’}, ‘files’: [(‘file’, SerializationIterator(index=0, iterator=<_io.BufferedReader name=‘.\sample.json’>))], ‘json_data’: {‘purpose’: ‘assistants’}}
2023-11-24 13:50:49 ZFNQ httpcore.connection[39316] DEBUG connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0x0000012AF40B9AC0>
2023-11-24 13:50:49 ZFNQ httpx[39316] INFO HTTP Request: POST hxxps://xxxxxx.openai.azure.com//openai/files?api-version=2023-12-01-preview “HTTP/1.1 400 Bad Request”
2023-11-24 13:50:49 ZFNQ openai._base_client[39316] DEBUG HTTP Request: POST hxxps://xxxxx.openai.azure.com//openai/files?api-version=2023-12-01-preview “400 Bad Request”
APIStatusError: Error code: 400 - {‘error’: {‘code’: ‘invalidPayload’, ‘message’: ‘Invalid value for purpose.’}}
<Response [400 Bad Request]>

Hmm, it would seem that this being a beta API feature, it’s not available currently in OpenAI Azure APIs: https://github.com/Azure/azure-rest-api-specs/tree/main/specification/cognitiveservices/data-plane/AzureOpenAI/authoring/preview/2023-12-01-preview. Looking there, I see that file upload is available, but only for fine-tune purpose and only for jsonl files. Can anyone in community point me to where I would need to go to request access to the beta API version that provides ability to create assistants based upon uploaded files?

1 Like

I am not able to replicate the file upload using Azure OpenAI. The code:
file = client.files.create(file=open(“…/…/data/input.pdf”, “rb”), purpose=“assistants”)
works in openAI but fails to work under AzureOpenAI. Like the previous comment, I am using it as an upload for retrieval not for finetuning.