Trying to List Messages in a Thread, getting Error: "1 validation error for Request\nbody -> role\n field required (type=value_error.missing)"

I’m trying to get a list of all the messages in a thread using the OpenAI documentation.

I’m using Ruby on Rails. Here’s my code:

require 'net/http'
require 'uri'
require 'json'
api_key = ENV['OPENAI_ACCESS_TOKEN']

thread = @support.thread_id.to_s
puts "THREAD: " + thread

# SUMMON THE MESSAGES
uri = URI.parse("https://api.openai.com/v1/threads/#{ thread }/messages")
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request["Authorization"] = "Bearer #{ api_key }"
request["Openai-Beta"] = "assistants=v1"
req_options = { use_ssl: uri.scheme == 'https' }
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

# Parse the JSON response
if response.code == '200'
  messages_data = JSON.parse(response.body)

  # Extract and show the messages
  @array_of_hashes = []
  messages_data['data'].each do |message|
    message_hash = {
      "role" => message['role'],
      "text" => message['content'][0]['text']['value']
    }
  @array_of_hashes << message_hash
  end
else
  puts "Failed to retrieve thread messages. HTTP status code: #{response.code}"
  puts "Response body: #{response.body}"
end

I have successfully used most of this code to create messages and threads, so I know that the api key and general code structure works.

The error I’m getting is:

Failed to retrieve thread messages. HTTP status code: 400
Response body: {
  "error": {
    "message": "1 validation error for Request\nbody -> role\n  field required (type=value_error.missing)",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

I’ve googled it, stack overflowed it, and asked ChatGPT, but I’m still drawing a blank. Here’s what I know:

  • The documentation says there is no body field required.
  • I’ve tried the CURL request in Insomnia and it works just fine, so I know the messages are there.
  • The thread ID (and therefore the endpoint) are propagating correctly (verified with the puts).

Another message I saw on this board said another similar issue may have to do with the model being used, but it’s not actually declared in this code, so I’m not sure if that lead will pan out.

Has anyone else run up against this error? Any hints for where to look?

I had the same issue in Postman with cURL, and fixed it by changing a “POST” request to a “GET” requests. So when polling the following endpoints:

/v1/threads/thread_abcd/messages
/v1/threads/thread_abcd/runs/run_abcd

You have to use “GET” requests not “POST” or “PUT”