Open
Description
Service
OpenAI
Describe the bug
I'm attempting to upload a JSON file and then use that within my user message prompts. However, after I upload the JSON file and reference its fileId in a UserChatMessage, it throws this exception:
System.ClientModel.ClientResultException: HTTP 400 (invalid_request_error: invalid_value)
Parameter: file_id
Invalid file data: 'file_id'. Expected a file with an application/pdf MIME type, but got unsupported MIME type 'application/json'.
It's confusing because clearly JSON files are supported (or else it wouldn't allow me to upload the file), and yet it's expecting a PDF for some reason. What am I missing?
public async Task TestJsonUpload()
{
var openAiApiKey = RprConfigurationManager.GetAppSetting<string>("GenAiOpenAiApiKey");
ChatClient client = new(model: "gpt-4.1", apiKey: openAiApiKey);
OpenAIClient openAIClient = new(openAiApiKey);
var json = """{"propertyId":37037566,"propertyMode":"Residential","propertyType":"Single Family","address":"10900 Fox Hedge Rd, Matthews, NC 28105","zip":"28105","centroid":{"latitude":35.0519990000,"longitude":-80.7527680000},"transactionType":"For Sale","listingStatus":"Closed","zoning":"R-3","listDate":"2024-11-07T00:00:00Z","offMarketDate":"2025-02-20T00:00:00Z","pendingDate":"2025-01-25T00:00:00Z","yearBuilt":1999,"bedrooms":5,"bathrooms":3,"livingArea":"3,760 sq ft","homeownersAssociationFeatures":{"Dues":"$1,175","Payments Per Year":"1"}}{"propertyId":37037566,"propertyMode":"Residential","propertyType":"Single Family","address":"10900 Fox Hedge Rd, Matthews, NC 28105","zip":"28105","centroid":{"latitude":35.0519990000,"longitude":-80.7527680000},"transactionType":"For Sale","listingStatus":"Closed","zoning":"R-3","listDate":"2024-11-07T00:00:00Z","offMarketDate":"2025-02-20T00:00:00Z","pendingDate":"2025-01-25T00:00:00Z","yearBuilt":1999,"bedrooms":5,"bathrooms":3,"livingArea":"3,760 sq ft","homeownersAssociationFeatures":{"Dues":"$1,175","Payments Per Year":"1"}}""";
string? fileId = null;
var fileClient = openAIClient.GetOpenAIFileClient();
using (var stream = BinaryData.FromBytes(Encoding.ASCII.GetBytes(json)).ToStream())
{
OpenAIFile file = await fileClient.UploadFileAsync(
stream,
$"subject-property.json",
FileUploadPurpose.UserData);
fileId = file.Id;
}
var completionOptions = new ChatCompletionOptions
{
MaxOutputTokenCount = 350,
Temperature = .4f,
TopP = .8f
};
var chatMessages = new List<ChatMessage> { new UserChatMessage([ChatMessageContentPart.CreateFilePart(fileId)]) };
ChatCompletion completion = await client.CompleteChatAsync(chatMessages, completionOptions);
var message = completion.Content.Any() ? completion.Content[0].Text : null;
}
Steps to reproduce
Run the sample code, inserting your own API key.
Code snippets
OS
Windows 11 Preview for ARM
.NET version
9
Library version
2.2.0-beta.4