Skip to content

Commit

Permalink
Improved error handling in thread for long running agent conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
VRSEN committed Feb 12, 2024
1 parent 3640aca commit 5954675
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion agency_swarm/threads/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_completion(self, message: str, message_files=None, yield_messages=True,
assistant_id=recipient_agent.id,
)

run_failed = False
while True:
self.await_run_completion()

Expand Down Expand Up @@ -94,13 +95,27 @@ def get_completion(self, message: str, message_files=None, yield_messages=True,
)
except BadRequestError as e:
if 'Runs in status "expired"' in e.message:
self.client.beta.threads.messages.create(
thread_id=self.thread.id,
role="user",
content="Please repeat the exact same function calls again."
)

self.run = self.client.beta.threads.runs.create(
thread_id=self.thread.id,
assistant_id=recipient_agent.id,
)

self.await_run_completion()

if self.run.status != "requires_action":
raise Exception("Run Failed. Error: ", self.run.last_error)

# change tool call ids
tool_calls = self.run.required_action.submit_tool_outputs.tool_calls
for i, tool_call in enumerate(tool_calls):
tool_outputs[i]["tool_call_id"] = tool_call.id

self.run = self.client.beta.threads.runs.submit_tool_outputs(
thread_id=self.thread.id,
run_id=self.run.id,
Expand All @@ -110,7 +125,15 @@ def get_completion(self, message: str, message_files=None, yield_messages=True,
raise e
# error
elif self.run.status == "failed":
raise Exception("Run Failed. Error: ", self.run.last_error)
# retry run 1 time
if not run_failed and "something went wrong" in self.run.last_error:
self.run = self.client.beta.threads.runs.create(
thread_id=self.thread.id,
assistant_id=recipient_agent.id,
)
run_failed = True
else:
raise Exception("Run Failed. Error: ", self.run.last_error)
# return assistant message
else:
messages = self.client.beta.threads.messages.list(
Expand Down

0 comments on commit 5954675

Please sign in to comment.