# Error from OpenAI: Status code 404

**URL:** <https://community.openai.com/t/error-from-openai-status-code-404/662251>\
**Category:** API\
**Tags:** assistants-api\
**Created:** [March 1, 2024, 9:39pm UTC](https://community.openai.com/t/error-from-openai-status-code-404/662251 "2024-03-01T21:39:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alone](https://avatars.discourse-cdn.com/v4/letter/a/b19c9b/32.png) [@Alone](https://community.openai.com/u/Alone)\
**Post date:** [March 1, 2024, 9:39pm UTC](https://community.openai.com/t/error-from-openai-status-code-404/662251/1 "2024-03-01T21:39:30Z")

</div>

Hello, I’m a beginner in programming and I planned to add an assistant to my Telegram bot. I tried adding ChatGPT without any issues, but when attempting to add the assistant (specifying its parameters including the model beforehand), I constantly encounter the same error. I double-checked all the parameters, generated a new API key, and also obtained a new assistant ID. Here’s my code for verification, thank you in advance.

import logging  
from aiogram import Bot, Dispatcher, executor, types  
import json  
import aiohttp  
from sqlalchemy import create\_engine, Column, Integer, String, Text  
from sqlalchemy.ext.declarative import declarative\_base  
from sqlalchemy.orm import sessionmaker  
from sqlalchemy.orm import declarative\_base

# Load configuration

with open(‘config.json’) as config\_file:  
config = json.load(config\_file)

# Configure logging

logging.basicConfig(level=logging.INFO)

# Initialize bot and dispatcher

bot = Bot(token=config[“=BOT\_TOKEN”])  
dp = Dispatcher(bot)

# Connect to the database

engine = create\_engine(‘sqlite:///users.db’)  
Base = declarative\_base()

class User(Base):  
**tablename** = ‘users’  
id = Column(Integer, primary\_key=True)  
user\_id = Column(Integer, unique=True)  
messages = Column(Text)

Base.metadata.create\_all(engine)  
Session = sessionmaker(bind=engine)  
db\_session = Session()

async def send\_openai\_request(path: str, method: str = ‘POST’, json: dict = None):  
“”“Sends a request to OpenAI.”“”  
url = f’/api.openai.com/v1{path}‘’  
headers = {  
‘Content-Type’: ‘application/json’,  
‘Authorization’: f’Bearer {config[“OPENAI\_API\_KEY”]}',  
‘OpenAI-Beta’: ‘assistants=v1’  
}  
async with aiohttp.ClientSession() as session:  
async with session.request(method, url, headers=headers, json=json) as resp:  
if resp.status == 200:  
return await resp.json()  
else:  
logging.error(f"Error from OpenAI: Status code {resp.status}, Response: {await resp.text()}")  
return None

async def get\_assistant\_response(prompt, user\_id):  
response = await send\_openai\_request(  
path=f"/assistants/{config[‘ASSISTANT\_ID’]}/messages",  
json={“input”: {“type”: “text”, “data”: prompt}, “assistant\_id”: config[‘ASSISTANT\_ID’]}  
)  
if response:  
# Assumes that the API returns the response text in a suitable format  
return response.get(‘choices’, [{}])[0].get(‘message’, ‘Sorry, I cannot process the request.’)  
return “Sorry, I cannot process your request right now.”

@dp.message\_handler(commands=[‘start’, ‘help’])  
async def send\_welcome(message: types.Message):  
await message.reply(“Hello! I’m your assistant. How can I help you today?”)

@dp.message\_handler()  
async def handle\_message(message: types.Message):  
await bot.send\_chat\_action(message.chat.id, types.ChatActions.TYPING)  
user\_input = message.text  
response = await get\_assistant\_response(user\_input, message.from\_user.id)  
await message.answer(response)

if **name** == ‘ **main** ’:  
executor.start\_polling(dp, skip\_updates=True)

UPD: I also checked the assistants available to me - everything is in order.

---

<div class="post-metadata">

**Author:** ![\_j](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/_j/32/719663_2.png) [@\_j](https://community.openai.com/u/_j)\
**Post date:** [March 1, 2024, 10:26pm UTC](https://community.openai.com/t/error-from-openai-status-code-404/662251/2 "2024-03-01T22:26:24Z")

</div>

You picked the wrong day to mess with assistants - it was basically down for 10 hours (but not throwing 404s)

If you edit your post and enclose your code in “```python” (using backticks) and then close it with ```, we might be able to read it…

Life will also be easier with the openai python module. It doesn’t mess up URLs.

---

<div class="post-metadata">

**Author:** ![RobSteele](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/robsteele/32/239657_2.png) [@RobSteele](https://community.openai.com/u/RobSteele)\
**Post date:** [March 1, 2024, 10:26pm UTC](https://community.openai.com/t/error-from-openai-status-code-404/662251/3 "2024-03-01T22:26:48Z")

</div>

Try this…

```auto
import logging
from aiogram import Bot, Dispatcher, executor, types
import json
import aiohttp
from sqlalchemy import create_engine, Column, Integer, String, Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# Load configuration
with open('config.json') as config_file:
    config = json.load(config_file)

# Configure logging
logging.basicConfig(level=logging.INFO)

# Initialize bot and dispatcher
bot = Bot(token=config["BOT_TOKEN"])
dp = Dispatcher(bot)

# Connect to the database
engine = create_engine('sqlite:///users.db')
Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, unique=True)
    messages = Column(Text)

Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
db_session = Session()

async def send_openai_request(path: str, method: str = 'POST', json: dict = None):
    """Sends a request to OpenAI."""
    url = f'https://api.openai.com/v1{path}'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {config["OPENAI_API_KEY"]}',
        'OpenAI-Beta': 'assistants=v1'
    }
    async with aiohttp.ClientSession() as session:
        async with session.request(method, url, headers=headers, json=json) as resp:
            if resp.status == 200:
                return await resp.json()
            else:
                logging.error(f"Error from OpenAI: Status code {resp.status}, Response: {await resp.text()}")
                return None

if __name__ == ' __main__':
    executor.start_polling(dp, skip_updates=True)

```
