Getting error while calling message api using assistant

I am getting error as below while creating a message using assistant api as follows, using dotnet c#

My code is as below:

using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add(“Authorization”, $“Bearer {apiKey}”);
httpClient.DefaultRequestHeaders.Add(“OpenAI-Beta”, “assistants=v1”);

                var requestBody = $@"{{""messages"": [{{ ""role"": ""user"",""content"": ""Can you brief about NewYork City?""}}]}}";

                HttpContent content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                string url = $"https://api.openai.com/v1/threads/{threadId}/messages";
                var response = await httpClient.PostAsync(url, content);

                if (response.IsSuccessStatusCode)
                {
                    var result = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    var result = await response.Content.ReadAsStringAsync();
                    return null;
                }
            }

When Executing this, i am getting below error as reponse :

“{\n "error": {\n "message": "3 validation errors for Request\nbody → role\n field required (type=value_error.missing)\nbody → content\n field required (type=value_error.missing)\nbody → messages\n extra fields not permitted (type=value_error.extra)",\n "type": "invalid_request_error",\n "param": null,\n "code": null\n }\n}”

Can anybody tell a solution ? or Am I doing something wrong in my coding?

Welcome to the community, @kuber.mannur. Hope you stick around.

Here’s an answer from the OpenAI kapa.ai Discord bot…

Based on the error message you’re receiving, it seems like the API is expecting the role and content fields to be present in the request body, but it’s not finding them. This could be due to the structure of your JSON request body. In the extracts provided, there’s an example of how to structure the request body when creating a message:

message = client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content=user_message
)

In this example, role and content are directly included in the request, not nested within a messages array. Your current request body is structured like this:

{
  "messages": [
    {
      "role": "user",
      "content": "Can you brief about NewYork City?"
    }
  ]
}

However, based on the error message and the example provided, it seems like the API is expecting a structure more like this:

{
  "role": "user",
  "content": "Can you brief about NewYork City?"
}

Please note that this is an inference based on the information provided in the extracts and the error message you’re receiving. You should refer to the official OpenAI API documentation for the most accurate information.

Thank you Mr. PaulBellow, for your fast response. Let me follow as per your instruction.

Thank you so much again…

1 Like

The solution provided by you is perfectly working, Mr.PaulBellow… Thanks a lot. It saved my day.

1 Like

Not me, it was kapa.ai trained on OpenAI docs. I just pressed the buttons.

Glad it worked for you, though. Thanks for reporting back.

Hope you stick around. We’ve got a great community garden growing here!

1 Like