Suddenly getting 400 Bad Request error calling OpenAI.Chat.ChatClient.CompleteChat()

Clay Hopkins 0 Reputation points
2025-04-16T18:34:47.4933333+00:00

Hi-

Late last week, our RAG chat service started returning 400 Bad Request errors. We are running a web API on ASP.NET Core 8. Nothing in the code changed, and I have verified that our code follows examples for Azure.AI.OpenAI 2.1.0. Does anyone know what would be causing the errors?

Following is the code snippet that makes the CompleteChat() call. The variables for the endpoints and API keys are confirmed correct:

[Authorize]

[HttpPost]

public ActionResult<string> Post([FromBody] string userMessage)

{

try

{

	AzureOpenAIClient client = new AzureOpenAIClient(

		new Uri(azureOpenAIEndpoint),

		new ApiKeyCredential(azureOpenAIKey)

	);

	ChatClient chatClient = client.GetChatClient(deploymentName);

	ChatCompletionOptions chatOptions = new ChatCompletionOptions() {

		MaxOutputTokenCount = 4096,

		ResponseFormat = ChatResponseFormat.CreateJsonObjectFormat(),

		Temperature = 0.1f

	};

	chatOptions.AddDataSource(new AzureSearchChatDataSource() {

		Authentication = DataSourceAuthentication.FromApiKey(searchKey),

		Endpoint = new Uri(searchEndpoint),

		IndexName = searchIndex

	});

	ChatCompletion chatCompletion = chatClient.CompleteChat([

			new SystemChatMessage(systemPrompt),

			new UserChatMessage(userMessage)

		],

		chatOptions);

	return Ok(chatCompletion.Content[0].Text);

}

catch (Exception ex)

{

	var result = StatusCode(StatusCodes.Status500InternalServerError, new { Message = ex.Message });

	return result;

}

}

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
3,985 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.