How to output Assistant Response

from openai import OpenAI
import os
import time
global client
global messages
os.environ["OPENAI_API_KEY"] = "{key}"
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))

assistant = client.beta.assistants.retrieve("{assistant id}")
print("Assistant Located")

thread = client.beta.threads.create()
print("Thread  Created")
message = client.beta.threads.messages.create(,
    content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
print("Thread Ready")

run = client.beta.threads.runs.create(,
print("Assistant Loaded")
print("Run Started - Please Wait")

while True:

  run_status = client.beta.threads.runs.retrieve(,

  if run_status.status == "completed":
    print("Run is Completed")
    messages = client.beta.threads.messages.list(
    print("Run is in progress - Please Wait")

When I run the line, “print(messages)” it outputs the full unformatted output of the thread. Is there any way I can have it print just the messages with the Assistant role? I’ve tried, (used google lol) but I could not figure out how to output it. Does anyone have any ideas how?

I have a similar script, and get the assistant’s message by using:[1].content[0].text.value
I hope this helps!

1 Like

Struggling with the same thing. The first thing I learned is that each of these objects is a Pydantic object. Knowing that I was able to create the following code (with a little help from ChatGPT).

messages = client.beta.threads.messages.list(

for thread_message in
    # Iterate over the 'content' attribute of the ThreadMessage, which is a list
    for content_item in thread_message.content:
        # Assuming content_item is a MessageContentText object with a 'text' attribute
        # and that 'text' has a 'value' attribute, print it