Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.46k stars 4.8k forks source link

[BUG] `response_format` `json_schema` throws exception when creating assistants #45894

Open rstropek opened 1 month ago

rstropek commented 1 month ago

Library name and version

Azure.AI.OpenAI 2.0.0-beta.5

Describe the bug

I try to create an assistant according to the REST documentation with response_format set to json_schema. The C# SDK gives me an HTTP 400 error with the error message Invalid value: 'json_schema'. Supported values are: 'json_object' and 'text'.'. This errors occurs although I switch to API version 2024-08-01-preview.

Expected behavior

An assistant with Structured Outputs is created.

Actual behavior

An error is thrown.

Reproduction Steps

var azureClient = new AzureOpenAIClient(
        new Uri(options.Endpoint),
        new AzureKeyCredential(options.ApiKey ?? configuration["AZURE_OPENAI_KEY"]!),
        new AzureOpenAIClientOptions() { // <<< this is new
            Transport = new ApiVersionSelectorTransport("2024-08-01-preview")
        });
//...
assistant = await openAIClient.CreateAssistantAsync(options.Model, new()
{
    Name = options.AssistantName,
    Instructions = await File.ReadAllTextAsync(options.PromptFile),
    ResponseFormat = AssistantResponseFormat.CreateJsonSchemaFormat("ExtractedInvoiceData", BinaryData.FromString(jsonSchema)),
    Tools = { new FileSearchToolDefinition() }
});
//...
class ApiVersionSelectorTransport(string apiVersion) : HttpClientPipelineTransport
{
    protected override void OnSendingRequest(PipelineMessage message, HttpRequestMessage httpRequest)
    {
        var uriBuilder = new UriBuilder(httpRequest.RequestUri!);
        var query = HttpUtility.ParseQueryString(uriBuilder.Query);
        query["api-version"] = apiVersion;
        uriBuilder.Query = query.ToString();
        httpRequest.RequestUri = uriBuilder.Uri;

        base.OnSendingRequest(message, httpRequest);
    }
}

Environment

.NET, C#, REST API Version 2024-08-01-preview, model version gpt-4o 2024-08-06

github-actions[bot] commented 1 month ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jpalvarezl @ralph-msft @trrwilson.