#Getting error while calling message api using assistant

1 messages · Page 1 of 1 (latest)

smoky flare
#

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?

barren citrusBOT
#

Hi @smoky flare. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

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.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: