Skip to content

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

Python

fix: use correct error handling for chat messages and ingestion (#859)

Lucas Oliveira3f02bb7ccad7
diff --git a/frontend/app/chat/_components/error-message.tsx b/frontend/app/chat/_components/error-message.tsx
new file mode 100644
index 000000000..329f4e266
--- /dev/null
+++ b/frontend/app/chat/_components/error-message.tsx
@@ -0,0 +1,51 @@
+import { AlertCircle } from "lucide-react";
+import { motion } from "motion/react";
+import DogIcon from "@/components/icons/dog-icon";
+import { Message } from "./message";
+
+interface ErrorMessageProps {
+ content: string;
+ animate?: boolean;
+ delay?: number;
+}
+
+export function ErrorMessage({
+ content,
+ animate = true,
+ delay = 0.2,
+}: ErrorMessageProps) {
+ return (
+ <motion.div
+ initial={animate ? { opacity: 0, y: -20 } : { opacity: 1, y: 0 }}
+ animate={{ opacity: 1, y: 0 }}
+ transition={
+ animate
+ ? { duration: 0.4, delay: delay, ease: "easeOut" }
+ : { duration: 0 }
+ }
+ >
+ <Message
+ icon={
+ <div className="w-8 h-8 flex items-center justify-center flex-shrink-0 select-none">
+ <DogIcon
+ className="h-6 w-6 transition-colors duration-300"
+ disabled={false}
+ />
+ </div>
+ }
+ >
+ <div className="rounded-lg border border-destructive/50 bg-destructive/5 p-4">
+ <div className="flex items-start gap-3">
+ <AlertCircle className="h-5 w-5 text-destructive flex-shrink-0 mt-0.5" />
+ <div className="flex-1">
+ <p className="text-sm text-destructive font-medium mb-1">Error</p>
+ <p className="text-sm text-muted-foreground">{content}</p>
+ </div>
+ </div>
+ </div>
+ </Message>
+ </motion.div>
+ );
+}
+
+// Made with Bob
diff --git a/frontend/app/chat/_types/types.ts b/frontend/app/chat/_types/types.ts
index c605da7c3..7787c8d11 100644
--- a/frontend/app/chat/_types/types.ts
+++ b/frontend/app/chat/_types/types.ts
@@ -17,6 +17,7 @@ export interface Message {
functionCalls?: FunctionCall[];
isStreaming?: boolean;
source?: "langflow" | "chat";
+ error?: boolean;
usage?: TokenUsage;
}
diff --git a/frontend/app/chat/page.tsx b/frontend/app/chat/page.tsx
index fe0d660e9..57317b08c 100644
--- a/frontend/app/chat/page.tsx
+++ b/frontend/app/chat/page.tsx
@@ -15,6 +15,7 @@ import { useGetNudgesQuery } from "../api/queries/useGetNudgesQuery";
import { useGetSettingsQuery } from "../api/queries/useGetSettingsQuery";
import { AssistantMessage } from "./_components/assistant-message";
import { ChatInput, type ChatInputHandle } from "./_components/chat-input";
+import { ErrorMessage } from "./_components/error-message";
import Nudges from "./_components/nudges";
import { UserMessage } from "./_components/user-message";
import type {
@@ -283,6 +284,7 @@ function ChatPage() {
role: "assistant",
content: `❌ Failed to process document. Please try again.`,
timestamp: new Date(),
+ error: true,
};
setMessages((prev) => [...prev.slice(0, -1), errorMessage]);
} finally {
@@ -367,6 +369,7 @@ function ChatPage() {
content: string;
timestamp?: string;
response_id?: string;
+ error?: boolean;
chunks?: Array<{
item?: {
type?: string;
@@ -394,6 +397,7 @@ function ChatPage() {
role: msg.role as "user" | "assistant",
content: msg.content,
timestamp: new Date(msg.timestamp || new Date()),
+ error: msg.error || false,
};
// Extract function calls from chunks or response_data
@@ -905,6 +909,7 @@ function ChatPage() {
role: "assistant",
content: "Sorry, I encountered an error. Please try again.",
timestamp: new Date(),
+ error: true,
};
setMessages((prev) => [...prev, errorMessage]);
}
@@ -917,6 +922,7 @@ function ChatPage() {
content:
"Sorry, I couldn't connect to the chat service. Please try again.",
timestamp: new Date(),
+ error: true,
};
setMessages((prev) => [...prev, errorMessage]);
}
@@ -1149,23 +1155,30 @@ function ChatPage() {
}-${index}-${message.timestamp?.getTime()}`}
className="space-y-6 group"
>
- <AssistantMessage
- content={message.content}
- functionCalls={message.functionCalls}
- messageIndex={index}
- expandedFunctionCalls={expandedFunctionCalls}
- onToggle={toggleFunctionCall}
- showForkButton={endpoint === "chat"}
- onFork={(e) => handleForkConversation(index, e)}
- animate={false}
- isInactive={index < messages.length - 1}
- isInitialGreeting={
- index === 0 &&
- messages.length === 1 &&
- message.content === "How can I assist?"
- }
- usage={message.usage}
- />
+ {message.error ? (
+ <ErrorMessage
+ content={message.content}
+ animate={false}
+ />
+ ) : (
+ <AssistantMessage
+ content={message.content}
+ functionCalls={message.functionCalls}
+ messageIndex={index}
+ expandedFunctionCalls={expandedFunctionCalls}
+ onToggle={toggleFunctionCall}
+ showForkButton={endpoint === "chat"}
+ onFork={(e) => handleForkConversation(index, e)}
+ animate={false}
+ isInactive={index < messages.length - 1}
+ isInitialGreeting={
+ index === 0 &&
+ messages.length === 1 &&
+ message.content === "How can I assist?"
+ }
+ usage={message.usage}
+ />
+ )}
</div>
),
)}
diff --git a/frontend/hooks/useChatStreaming.ts b/frontend/hooks/useChatStreaming.ts
index 13144e910..eee28360c 100644
--- a/frontend/hooks/useChatStreaming.ts
+++ b/frontend/hooks/useChatStreaming.ts
@@ -134,6 +134,7 @@ export function useChatStreaming({
let currentContent = "";
const currentFunctionCalls: FunctionCall[] = [];
let newResponseId: string | null = null;
+ let isError = false;
let usageData: TokenUsage | undefined;
// Initialize streaming message
@@ -454,6 +455,17 @@ export function useChatStreaming({
currentFunctionCalls.push(newFunctionCall);
}
}
+
+ // 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 || "";
@@ -468,15 +480,15 @@ export function useChatStreaming({
// Handle OpenRAG backend format
else if (chunk.output_text) {
currentContent += chunk.output_text;
- } else if (chunk.delta) {
+ }
+ // Note: chunk.delta.content is already handled in the OpenAI format section above (line 271)
+ // Only handle delta if it's a string or has text (not content)
+ else if (chunk.delta) {
if (typeof chunk.delta === "string") {
currentContent += chunk.delta;
- } else if (typeof chunk.delta === "object") {
- if (chunk.delta.content) {
- currentContent += chunk.delta.content;
- } else if (chunk.delta.text) {
- currentContent += chunk.delta.text;
- }
+ } else if (typeof chunk.delta === "object" && chunk.delta.text && !chunk.delta.content) {
+ // Only add text if content wasn't already processed
+ currentContent += chunk.delta.text;
}
}
@@ -605,6 +617,7 @@ export function useChatStreaming({
currentFunctionCalls.length > 0 ? currentFunctionCalls : undefined,
timestamp: new Date(),
isStreaming: false,
+ error: isError, // Mark as error if Langflow returned error status
usage: usageData,
};
@@ -633,10 +646,10 @@ export function useChatStreaming({
setStreamingMessage(null);
// Create user-friendly error message
- let errorContent =
- "Sorry, I couldn't connect to the chat service. Please try again.";
-
const errorMessage = (error as Error).message;
+ let errorContent = errorMessage; // Default to the actual error message
+
+ // Only override with generic messages for specific infrastructure errors
if (errorMessage?.includes("timed out")) {
errorContent =
"The request timed out. The server took too long to respond. Please try again.";
@@ -648,9 +661,8 @@ export function useChatStreaming({
) {
errorContent =
"Network error. Please check your connection and try again.";
- } else if (errorMessage?.includes("Server error")) {
- errorContent = errorMessage; // Use the detailed server error message
}
+ // For all other errors (including Langflow errors), use the actual error message
onError?.(error as Error);
@@ -659,8 +671,15 @@ export function useChatStreaming({
content: errorContent,
timestamp: new Date(),
isStreaming: false,
+ error: true,
};
+ // Pass error message to onComplete so it gets added to chat history
+ // This ensures errors appear immediately and persist on page refresh
+ if (!streamAbortRef.current?.signal.aborted) {
+ onComplete?.(errorMessageObj, null);
+ }
+
return errorMessageObj;
} finally {
if (timeoutId) clearTimeout(timeoutId);
diff --git a/src/agent.py b/src/agent.py
index 76cf8b1d6..1fa2f4b76 100644
--- a/src/agent.py
+++ b/src/agent.py
@@ -135,14 +135,13 @@ async def async_response_stream(
full_response = ""
chunk_count = 0
- detected_tool_call = False # Track if we've detected a tool call
+ detected_tool_call = False
async for chunk in response:
chunk_count += 1
logger.debug(
"Stream chunk received", chunk_count=chunk_count, chunk=str(chunk)
)
- # Yield the raw event as JSON for the UI to process
import json
# Also extract text content for logging
@@ -186,7 +185,7 @@ async def async_response_stream(
if isinstance(chunk_data, dict):
# Check for any fields that might indicate tool usage
potential_tool_fields = {
- k: v for k, v in chunk_data.items()
+ k: v for k, v in chunk_data.items()
if any(keyword in str(k).lower() for keyword in ['tool', 'call', 'retrieval', 'function', 'result', 'output'])
}
if potential_tool_fields:
@@ -235,8 +234,8 @@ async def async_response_stream(
"tool_name": "Retrieval",
"status": "completed",
"inputs": {"implicit": True, "backend_detected": True},
- "results": chunk_data.get('results') or chunk_data.get('outputs') or
- chunk_data.get('retrieved_documents') or
+ "results": chunk_data.get('results') or chunk_data.get('outputs') or
+ chunk_data.get('retrieved_documents') or
chunk_data.get('retrieval_results') or []
}
}
@@ -698,47 +697,55 @@ async def async_langflow_chat_stream(
response_id = None
usage_data = None
collected_chunks = [] # Store all chunks for function call data
+ error_occurred = False
- async for chunk in async_stream(
- langflow_client,
- prompt,
- flow_id,
- extra_headers=extra_headers,
- previous_response_id=previous_response_id,
- log_prefix="langflow",
- ):
- # Extract text content to build full response for history
- try:
- import json
-
- chunk_data = json.loads(chunk.decode("utf-8"))
- collected_chunks.append(chunk_data) # Collect all chunk data
-
- if "delta" in chunk_data and "content" in chunk_data["delta"]:
- full_response += chunk_data["delta"]["content"]
- # Extract response_id from chunk
- if "id" in chunk_data:
- response_id = chunk_data["id"]
- elif "response_id" in chunk_data:
- response_id = chunk_data["response_id"]
- # Capture usage from response.completed event
- if chunk_data.get("type") == "response.completed":
- response_obj = chunk_data.get("response", {})
- usage_data = response_obj.get("usage")
- except:
- pass
- yield chunk
+ try:
+ async for chunk in async_stream(
+ langflow_client,
+ prompt,
+ flow_id,
+ extra_headers=extra_headers,
+ previous_response_id=previous_response_id,
+ log_prefix="langflow",
+ ):
+ # Extract text content to build full response for history
+ try:
+ import json
+
+ chunk_data = json.loads(chunk.decode("utf-8"))
+ collected_chunks.append(chunk_data) # Collect all chunk data
+
+ if "delta" in chunk_data and "content" in chunk_data["delta"]:
+ full_response += chunk_data["delta"]["content"]
+ # Extract response_id from chunk
+ if "id" in chunk_data:
+ response_id = chunk_data["id"]
+ elif "response_id" in chunk_data:
+ response_id = chunk_data["response_id"]
+
+ # Check for error status
+ if chunk_data.get("finish_reason") == "error" or chunk_data.get("status") == "failed":
+ error_occurred = True
+ logger.error("Error detected in Langflow stream chunk")
+ # Capture usage from response.completed event
+ if chunk_data.get("type") == "response.completed":
+ response_obj = chunk_data.get("response", {})
+ usage_data = response_obj.get("usage")
+ except:
+ pass
+ 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
- }
- # Store usage data if available (from response.completed event)
+ # 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)
if usage_data:
assistant_message["response_data"] = {"usage": usage_data}
conversation_state["messages"].append(assistant_message)
@@ -760,6 +767,34 @@ async def async_langflow_chat_stream(
logger.debug(
f"Stored langflow conversation thread for user {user_id} with response_id: {response_id}"
)
+ except Exception as e:
+ # Log the error
+ logger.error(f"Error in langflow chat stream: {e}", exc_info=True)
+ error_occurred = True
+
+ # Store error message in conversation history so it persists
+ error_message = {
+ "role": "assistant",
+ "content": f"Sorry, I encountered an error: {str(e)}",
+ "timestamp": datetime.now(),
+ "error": True,
+ }
+ conversation_state["messages"].append(error_message)
+
+ # Try to store the conversation with error message
+ # Use a temporary response_id if we don't have one
+ if not response_id:
+ response_id = f"error_{user_id}_{int(datetime.now().timestamp())}"
+
+ try:
+ conversation_state["last_activity"] = datetime.now()
+ await store_conversation_thread(user_id, response_id, conversation_state)
+ logger.debug(f"Stored conversation with error for user {user_id}")
+ except Exception as store_error:
+ logger.error(f"Failed to store error conversation: {store_error}")
+
+ # Re-raise the exception so it propagates to the API layer
+ raise
async def delete_user_conversation(user_id: str, response_id: str) -> bool:
diff --git a/src/services/chat_service.py b/src/services/chat_service.py
index 92c834a84..55d4a98bd 100644
--- a/src/services/chat_service.py
+++ b/src/services/chat_service.py
@@ -516,6 +516,9 @@ class ChatService:
"source": "langflow",
}
+ if msg.get("error"):
+ message_data["error"] = True
+
# Include function call data if present
if msg.get("chunks"):
message_data["chunks"] = msg["chunks"]
diff --git a/src/services/langflow_file_service.py b/src/services/langflow_file_service.py
index c1732489b..a569b4d9f 100644
--- a/src/services/langflow_file_service.py
+++ b/src/services/langflow_file_service.py
@@ -1,5 +1,5 @@
-from typing import Any, Dict, List, Optional
import json
+from typing import Any, Dict, List, Optional
from config.settings import LANGFLOW_INGEST_FLOW_ID, clients
from utils.logging_config import get_logger
@@ -62,7 +62,7 @@ class LangflowFileService:
self,
file_paths: List[str],
file_tuples: list[tuple[str, str, str]],
- jwt_token: str,
+ jwt_token: Optional[str] = None,
session_id: Optional[str] = None,
tweaks: Optional[Dict[str, Any]] = None,
owner: Optional[str] = None,
@@ -180,7 +180,28 @@ class LangflowFileService:
reason=resp.reason_phrase,
body=resp.text[:1000],
)
- resp.raise_for_status()
+
+ # Extract error message from Langflow response
+ error_message = f"Server error '{resp.status_code} {resp.reason_phrase}'"
+ try:
+ error_data = resp.json()
+ if isinstance(error_data, dict) and "detail" in error_data:
+ detail = error_data["detail"]
+ if isinstance(detail, str):
+ try:
+ detail_obj = json.loads(detail)
+ if isinstance(detail_obj, dict) and "message" in detail_obj:
+ error_message = detail_obj["message"]
+ else:
+ error_message = detail
+ except json.JSONDecodeError:
+ error_message = detail
+ elif isinstance(detail, dict) and "message" in detail:
+ error_message = detail["message"]
+ except Exception:
+ pass
+
+ raise Exception(error_message)
# Check if response is actually JSON before parsing
content_type = resp.headers.get("content-type", "")
@@ -319,8 +340,7 @@ class LangflowFileService:
"[LF] Ingestion failed during combined operation",
extra={"error": str(e), "file_path": file_path},
)
- # Note: We could optionally delete the uploaded file here if ingestion fails
- raise Exception(f"Ingestion failed: {str(e)}")
+ raise
# Step 4: Delete file from Langflow (optional)
file_id = upload_result.get("id")
diff --git a/src/services/langflow_history_service.py b/src/services/langflow_history_service.py
index c9a77cfa7..dc4111486 100644
--- a/src/services/langflow_history_service.py
+++ b/src/services/langflow_history_service.py
@@ -71,19 +71,27 @@ class LangflowHistoryService:
for msg in langflow_messages:
try:
+ content = msg.get("text", "")
+
+ # Detect error messages - check explicit error flag or common error patterns
+ is_error = msg.get("error", False) or (
+ msg.get("sender") != "User" and content.startswith("Error:")
+ )
+
# Map Langflow message format to OpenRAG format
converted_msg = {
"role": "user" if msg.get("sender") == "User" else "assistant",
- "content": msg.get("text", ""),
+ "content": content,
"timestamp": msg.get("timestamp"),
"langflow_message_id": msg.get("id"),
"langflow_session_id": msg.get("session_id"),
"langflow_flow_id": msg.get("flow_id"),
"sender": msg.get("sender"),
"sender_name": msg.get("sender_name"),
+ "source": "langflow",
"files": msg.get("files", []),
"properties": msg.get("properties", {}),
- "error": msg.get("error", False),
+ "error": is_error,
"edit": msg.get("edit", False)
}