Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

fix: make watsonx work in chat (#1534)

Lucas Oliveira644c730a8b23
diff --git a/frontend/app/chat/page.tsx b/frontend/app/chat/page.tsx
index d372c4a5b..524cc991d 100644
--- a/frontend/app/chat/page.tsx
+++ b/frontend/app/chat/page.tsx
@@ -44,6 +44,7 @@ function ChatPage() {
refreshConversations,
refreshConversationsSilent,
refreshTrigger,
+ refreshTriggerSilent,
previousResponseIds,
setPreviousResponseIds,
placeholderConversation,
@@ -82,7 +83,7 @@ function ChatPage() {
// Check if chat history is loading
const { isLoading: isConversationsLoading } = useGetConversationsQuery(
endpoint,
- refreshTrigger,
+ refreshTrigger + refreshTriggerSilent,
);
// Use conversation-specific filter instead of global filter
@@ -358,15 +359,20 @@ function ChatPage() {
};
}, [abortStream, setLoading]);
- // Load conversation only when user explicitly selects a conversation
+ // Load conversation data from context
useEffect(() => {
// Only load conversation data when:
// 1. conversationData exists AND
- // 2. It's different from the last loaded conversation AND
+ // 2. (It's a different conversation OR we're not streaming and data has changed) AND
// 3. User is not in the middle of an interaction
+ const isNewConversation =
+ lastLoadedConversationRef.current !== conversationData?.response_id;
+ const hasMessageCountChanged =
+ conversationData?.messages?.length !== messages.length;
+
if (
conversationData?.messages &&
- lastLoadedConversationRef.current !== conversationData.response_id &&
+ (isNewConversation || (!isStreamLoading && hasMessageCountChanged)) &&
!isUserInteracting &&
!isForkingInProgress
) {
@@ -558,6 +564,8 @@ function ChatPage() {
isUserInteracting,
isForkingInProgress,
setPreviousResponseIds,
+ isStreamLoading,
+ messages.length,
]);
// Handle new conversation creation - only reset messages when placeholderConversation is set
diff --git a/frontend/components/chat-renderer.tsx b/frontend/components/chat-renderer.tsx
index 247ed54df..e68fab18b 100644
--- a/frontend/components/chat-renderer.tsx
+++ b/frontend/components/chat-renderer.tsx
@@ -42,6 +42,7 @@ export function ChatRenderer({
const {
endpoint,
refreshTrigger,
+ refreshTriggerSilent,
refreshConversations,
startNewConversation,
setConversationFilter,
@@ -79,7 +80,7 @@ export function ChatRenderer({
// Only fetch conversations on chat page
const isOnChatPage = pathname === "/" || pathname === "/chat";
const { data: conversations = [], isLoading: isConversationsLoading } =
- useGetConversationsQuery(endpoint, refreshTrigger, {
+ useGetConversationsQuery(endpoint, refreshTrigger + refreshTriggerSilent, {
enabled: isOnChatPage && (isAuthenticated || isNoAuthMode),
}) as { data: ChatConversation[]; isLoading: boolean };
diff --git a/frontend/hooks/useChatStreaming.ts b/frontend/hooks/useChatStreaming.ts
index cd7bf96ab..c5f5b43c1 100644
--- a/frontend/hooks/useChatStreaming.ts
+++ b/frontend/hooks/useChatStreaming.ts
@@ -152,7 +152,7 @@ export function useChatStreaming({
}
try {
- while (true) {
+ streamLoop: while (true) {
const { done, value } = await reader.read();
if (controller.signal.aborted || thisStreamId !== streamIdRef.current)
break;
@@ -460,19 +460,6 @@ export function useChatStreaming({
}
}
- // Check for error status from Langflow
- if (
- chunk.finish_reason === "error" ||
- chunk.status === "failed"
- ) {
- console.error("Error detected in stream");
-
- // Mark this as an error message and complete the stream
- isError = true;
-
- // Exit the streaming loop by throwing so the reader stops promptly on error
- throw new Error("Error detected in stream");
- }
// Handle text output streaming (Realtime API)
else if (chunk.type === "response.output_text.delta") {
currentContent += chunk.delta || "";
@@ -552,6 +539,20 @@ export function useChatStreaming({
);
}
+ // Check for error status from Langflow
+ if (
+ chunk.finish_reason === "error" ||
+ chunk.status === "failed"
+ ) {
+ console.error("Error detected in stream");
+
+ // Mark this as an error message and complete the stream
+ isError = true;
+
+ // Exit the streaming loop by breaking the labeled while loop
+ break streamLoop;
+ }
+
// Update streaming message in real-time
if (
!controller.signal.aborted &&
diff --git a/src/agent.py b/src/agent.py
index 1d80c4188..9856918dd 100644
--- a/src/agent.py
+++ b/src/agent.py
@@ -515,25 +515,24 @@ async def async_chat_stream(
yield chunk
# Add the complete assistant response to message history with response_id and timestamp
- if full_response:
- assistant_message = {
- "role": "assistant",
- "content": full_response,
- "response_id": response_id,
- "timestamp": datetime.now(),
- }
- # Store usage data if available (from response.completed event)
- if usage_data:
- assistant_message["response_data"] = {"usage": usage_data}
- conversation_state["messages"].append(assistant_message)
+ assistant_message = {
+ "role": "assistant",
+ "content": full_response,
+ "response_id": response_id,
+ "timestamp": datetime.now(),
+ }
+ # Store usage data if available (from response.completed event)
+ if usage_data:
+ assistant_message["response_data"] = {"usage": usage_data}
+ conversation_state["messages"].append(assistant_message)
- # Store the conversation thread with its response_id
- if response_id:
- conversation_state["last_activity"] = datetime.now()
- await store_conversation_thread(user_id, response_id, conversation_state)
- logger.debug(
- f"Stored conversation thread for user {user_id} with response_id: {response_id}"
- )
+ # Store the conversation thread with its response_id
+ if response_id:
+ conversation_state["last_activity"] = datetime.now()
+ await store_conversation_thread(user_id, response_id, conversation_state)
+ logger.debug(
+ f"Stored conversation thread for user {user_id} with response_id: {response_id}"
+ )
# Async langflow function with conversation storage (non-streaming)
@@ -789,16 +788,15 @@ async def async_langflow_chat_stream(
yield chunk
# Add the complete assistant response to message history with response_id, timestamp, and function call data
- if full_response:
- assistant_message = {
- "role": "assistant",
- "content": full_response,
- "response_id": response_id,
- "timestamp": datetime.now(),
- "chunks": collected_chunks, # Store complete chunk data for function calls
- "error": error_occurred, # Mark if this was an error response
- }
- # Store usage data if available (from response.completed event)
+ assistant_message = {
+ "role": "assistant",
+ "content": full_response,
+ "response_id": response_id,
+ "timestamp": datetime.now(),
+ "chunks": collected_chunks, # Store complete chunk data for function calls
+ "error": error_occurred, # Mark if this was an error response
+ }
+ # Store usage data if available (from response.completed event)
if usage_data:
assistant_message["response_data"] = {"usage": usage_data}
conversation_state["messages"].append(assistant_message)