Skip to content

Commit 8956b28

Browse files
authored
backend: Filter out invalid message text when generating chat history (#862)
Filter out invalid message text
1 parent 81948ac commit 8956b28

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/backend/chat/custom/custom.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ async def chat(
9090
"status_code": 500,
9191
}
9292

93+
9394
def is_final_event(
9495
self, event: Dict[str, Any], chat_request: CohereChatRequest
9596
) -> bool:

src/backend/chat/custom/tool_calls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ async def _call_all_tools_async(
7171
)
7272

7373

74+
7475
async def _call_tool_async(
7576
ctx: Context,
7677
db: Session,

src/backend/services/chat.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ def create_chat_history(
471471
chat_request: BaseChatRequest,
472472
) -> list[ChatMessage]:
473473
"""
474-
Create chat history from conversation messages or request.
474+
Create chat history from conversation messages or request, this chat history
475+
is sent to the chat SDK call for added context.
475476
476477
Args:
477478
conversation (Conversation): Conversation object.
@@ -487,11 +488,13 @@ def create_chat_history(
487488
if conversation.messages is None:
488489
return []
489490

490-
# Don't include the user message that was just sent
491+
# Filter out user message that was just sent
492+
# And any empty messages
491493
text_messages = [
492494
message
493495
for message in conversation.messages
494496
if message.position < user_message_position
497+
and message.text
495498
]
496499
return [
497500
ChatMessage(

0 commit comments

Comments
 (0)