diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 9e749ac35..a2fd61bbb 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -827,10 +827,15 @@ def apply_source_context_to_messages( messages: list, sources: list, user_message: str, + include_content: bool = True, ) -> list: """ Build source context from citation sources and apply to messages. Uses RAG template to format context for model consumption. + + When include_content is False, emit tags with id/name but no + document body — useful when the content is already present elsewhere + (e.g. in a tool result message) and only citation markers are needed. """ if not sources or not user_message: return messages @@ -844,10 +849,11 @@ def apply_source_context_to_messages( if src_id not in citation_idx: citation_idx[src_id] = len(citation_idx) + 1 src_name = source.get("source", {}).get("name") + body = doc if include_content else "" context_string += ( f'{doc}\n" + + f">{body}\n" ) context_string = context_string.strip() @@ -4227,6 +4233,8 @@ async def streaming_chat_response_handler(response, ctx): await event_emitter({"type": "source", "data": source}) # Apply source context to messages for model + # Use metadata_only=True to avoid duplicating content + # that is already in the tool result message. all_tool_call_sources.extend(tool_call_sources) if all_tool_call_sources and user_message: # Restore original user message before re-applying to avoid recursive nesting @@ -4238,6 +4246,7 @@ async def streaming_chat_response_handler(response, ctx): form_data["messages"], all_tool_call_sources, user_message, + include_content=False, ) tool_call_sources.clear()