diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py
index 797c9622a..3aa3c5559 100644
--- a/backend/open_webui/main.py
+++ b/backend/open_webui/main.py
@@ -698,12 +698,13 @@ class ChatCompletionMiddleware(BaseHTTPMiddleware):
if "document" in source:
for doc_idx, doc_context in enumerate(source["document"]):
metadata = source.get("metadata")
+ doc_source_id = None
if metadata:
doc_source_id = metadata[doc_idx].get("source", source_id)
if source_id:
- context_string += f"\n"
+ context_string += f"\n"
else:
# If there is no source_id, then do not include the source_id tag
context_string += f"\n"