Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
def add_messages(self, room_id, prompt, file_ids: List[str] | NotGiven = NotGiven): thread = self.get_thread(room_id) message = self.client.beta.threads.messages.create( thread_id=thread.id, role="user", content=prompt, metadata={'room_id': room_id}, file_ids=file_ids, ) mlogger.info(f'[add_messages]room_id:{room_id} thread_id:{thread.id} message_id:{message.id}') return message
I've finished my test before. But I got this error these days.
openai.BadRequestError: Error code: 400 - {'error': {'message': "Can't add messages to thread_jD1dj44APTovq48PCBZACOGV while a run run_KfVG8fSJg882VHYeewFQ2BHk is active.", 'type': 'invalid_request_error', 'param': None, 'code': None}}
I'v make sure the thread was just created .
To Reproduce
- get assistant from list assistant
- get file ids from file list
- create thread or get thread id from cache
- message create with content 、file_ids 、 metadata
- call run.create
- see error
Code snippets
def reply(self, room_id, prompt, payload: dict):
file_ids = self._get_file_ids(payload, room_id)
# get file ids from redis
try:
self.repo.add_messages(room_id, prompt, file_ids)
self.repo.create_run(room_id)
except Exception as e:
mlogger.exception(e)
yield from self._get_answer(room_id, payload)
def get_thread(self, room_id):
thread_id = room_id_to_thread_id.get(room_id)
if thread_id:
return self.client.beta.threads.retrieve(thread_id=thread_id)
thread = self.client.beta.threads.create()
room_id_to_thread_id.set(key=room_id, value=thread.id)
return thread
def add_messages(self, room_id, prompt, file_ids: List[str] | NotGiven = NotGiven):
thread = self.get_thread(room_id)
message = self.client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content=prompt,
metadata={'room_id': room_id},
file_ids=file_ids,
)
mlogger.info(f'[add_messages]room_id:{room_id} thread_id:{thread.id} message_id:{message.id}')
return message
def create_run(self, room_id):
thread = self.get_thread(room_id)
run = self.client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=self.assistant.id,
metadata={'room_id': room_id}
)
mlogger.info(f'[create_run]room_id:{room_id} thread_id:{thread.id} run_id:{run.id}')
room_id_to_run_id[room_id] = run.id
return run
OS
win11
Python version
Python 3.11.2
Library version
openai v1.6.1