The problem with getting the assistant's response! Python code

Hello everyone I can’t figure out what the error is?
I created an assistant, created python code…
it successfully creates a stream, successfully adds a message, successfully starts the stream, BUT I DO NOT RECEIVE A RESPONSE FROM THE ASSISTANT!

import requests
import time
import json

# Ваши данные ассистента и API-ключ OpenAI
assistant_id = 'asst_secret4Mo••••P0r'  # Ваш ID ассистента
api_key = 'sk-u8SkhgER5r••••••••••••••••••••••••••••'  # Ваш API-ключ

# Заголовки для запросов
headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json',
    'OpenAI-Beta': 'assistants=v1'

# Функция для создания потока
def create_thread():
    response ='', headers=headers)
    if response.status_code == 200:
        thread_id = response.json()['id']
        print(f"Поток успешно создан, ID потока: {thread_id}")
        return thread_id
        print("Ошибка при создании потока:", response.text)
        return None

# Функция для добавления сообщения
def add_message(thread_id, message_content):
    data = {
        "role": "user",
        "content": message_content
    response ='{thread_id}/messages', json=data, headers=headers)
    if response.status_code == 200:
        print("Сообщение успешно добавлено:", message_content)
        print("Ошибка при добавлении сообщения:", response.text)

# Функция для запуска потока и получения ответа
def run_thread(thread_id):
    data = {
        "assistant_id": assistant_id
    response ='{thread_id}/runs', json=data, headers=headers)
    if response.status_code == 200:
        print("Поток успешно запущен")
        run_id = response.json()['id']
        while True:
            response = requests.get(f'{thread_id}/runs/{run_id}', headers=headers)
            if response.status_code == 200:
                run_info = response.json()
                if run_info['status'] == 'completed':
                    if 'output' in run_info:
                        assistant_response = run_info['output']['content']
                        print("Ответ ассистента:")
                        print("Ассистент не вернул ответ.")
                print("Ошибка при получении статуса выполнения:", response.text)
        print("Ошибка при запуске потока:", response.text)

if __name__ == '__main__':
    # Создаем поток
    thread_id = create_thread()
    if thread_id:
        # Сообщение, которое хотим отправить
        message_content = "Что за услуга?"
        # Добавляем сообщение в поток
        add_message(thread_id, message_content)
        # Запускаем поток и получаем ответ

Help me pls!:pray:

“run thread” only puts the agent into motion.

You must make continued API calls to poll the status of the run.

After the run has a status that it is completed, then you can obtain the assistant response message within a thread with another API call.

“API Reference” or “Documentation” on the forum’s sidebar is your friend.

1 Like

I thank you)
I understand that the documentation is my friend, but I came here after I got confused and didn’t figure anything out there…
maybe it is possible to send a screenshot from the documentation, what should I pay attention to?

Here’s a link into documentation:

1 Like

I do not know your name! But you’re the best! Thank you so much, it worked out!)

1 Like