Skip to content

Commit

Permalink
Improve an error handling: latest_message.content is empty.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshinishio committed Jul 9, 2024
1 parent cf01ee5 commit ba8012f
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions services/openai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
from openai.types.beta.threads.run_submit_tool_outputs_params import ToolOutput

# Local imports
from config import OPENAI_FINAL_STATUSES, OPENAI_MAX_STRING_LENGTH, OPENAI_MODEL_ID, TIMEOUT_IN_SECONDS
from config import (
OPENAI_FINAL_STATUSES,
OPENAI_MAX_STRING_LENGTH,
OPENAI_MODEL_ID,
TIMEOUT_IN_SECONDS,
)
from services.openai.functions import (
GET_REMOTE_FILE_CONTENT,
functions,
Expand Down Expand Up @@ -137,16 +142,18 @@ def run_assistant(
messages: SyncCursorPage[Message] = get_response(thread=thread)
messages_list = list(messages)
if not messages_list:
raise ValueError("messages_list is empty.")
msg = f"In run_assistant(), messages_list is empty: '{messages_list}'"
raise ValueError(msg)
latest_message: Message = messages_list[0]
if not latest_message.content:
raise ValueError("latest_message.content is empty.")
msg = f"In run_assistant(), latest_message.content is empty. latest_message: '{latest_message}'"
raise ValueError(msg)
if isinstance(latest_message.content[0], TextContentBlock):
value: str = latest_message.content[0].text.value
output_data += json.dumps(latest_message.content[0].text.value)
else:
raise ValueError("Last message content is not text.")
print(f"Last message: {value}\n")
msg = f"In run_assistant(), Last message content is not text. latest_message: '{latest_message}'"
raise ValueError(msg)

# Clean the diff text and split it
diff: str = clean_specific_lines(text=value)
Expand Down Expand Up @@ -187,7 +194,6 @@ def run_assistant(
output_data += json.dumps(latest_message.content[0].text.value)
else:
raise ValueError("Last message content is not text.")
print(f"Last message: {value}\n")

# Clean the diff text and split it
diff: str = clean_specific_lines(text=value)
Expand Down

0 comments on commit ba8012f

Please sign in to comment.