Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: properly handle citations and source formatting in TopicOrganizationStrategy

LearningCircuitc8700649f6da
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
index 44ce1a1890..a05537641f 100644
--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
@@ -6,6 +6,7 @@ from loguru import logger
from .base_strategy import BaseSearchStrategy
from .source_based_strategy import SourceBasedSearchStrategy
from ..findings.topic import Topic, TopicGraph
+from ..findings.repository import FindingsRepository
from ...utilities.thread_context import preserve_research_context
@@ -67,6 +68,9 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
self.iteration_history = [] # Track what happened in each iteration
self.generate_text = generate_text
+ # Initialize findings repository for proper formatting
+ self.findings_repository = FindingsRepository(model)
+
# Create a no-op citation handler to skip text generation
class NoTextCitationHandler:
def analyze_followup(
@@ -936,11 +940,34 @@ Otherwise, respond with only the follow-up question, nothing else.
f"No new sources found in refinement iteration {len(self.refinement_questions)}"
)
- # Generate text if enabled
+ self._update_progress(
+ "Finalizing topic organization",
+ 95,
+ {"phase": "finalizing", "final_topic_count": len(relevant_topics)},
+ )
+
+ # Collect all sources from relevant topics only
+ # These are the ONLY sources we want to use for citations
+ topic_sources = []
+ for topic in relevant_topics:
+ topic_sources.extend(topic.get_all_sources())
+
+ # IMPORTANT: Update all_links_of_system to only include sources that made it into topics
+ # This ensures citations only reference sources we actually use
+ self.all_links_of_system = topic_sources
+
+ # Ensure all sources have both 'link' and 'index' fields for proper formatting
+ for i, source in enumerate(topic_sources, 1):
+ if "url" in source and "link" not in source:
+ source["link"] = source["url"]
+ # Re-index sources to be sequential for the final output
+ source["index"] = str(i)
+
+ # Generate text AFTER re-indexing sources
if self.generate_text and self.citation_handler:
self._update_progress(
"Generating text synthesis from topics",
- 95,
+ 96,
{"phase": "text_generation"},
)
generated_text = self._generate_topic_based_text(
@@ -949,15 +976,10 @@ Otherwise, respond with only the follow-up question, nothing else.
else:
generated_text = ""
- # Format findings - include both generated text and topic structure
- if generated_text:
- # Combine generated text with topic organization details
- formatted_findings = f"{generated_text}\n\n---\n\n{self._format_topic_findings(relevant_topics, query)}"
- else:
- # Just the topic organization
- formatted_findings = self._format_topic_findings(
- relevant_topics, query
- )
+ # Format the topic graph as a structured knowledge representation
+ topic_graph_str = self._format_topic_graph_as_knowledge(
+ relevant_topics, query
+ )
self._update_progress(
"Topic organization complete",
@@ -965,18 +987,6 @@ Otherwise, respond with only the follow-up question, nothing else.
{"phase": "complete", "final_topic_count": len(relevant_topics)},
)
- # Collect all sources from relevant topics only
- # Note: We don't need to add to all_links_of_system since we're already
- # maintaining it from source_based_strategy results
- topic_sources = []
- for topic in relevant_topics:
- topic_sources.extend(topic.get_all_sources())
-
- # Format the topic graph as a structured knowledge representation
- topic_graph_str = self._format_topic_graph_as_knowledge(
- relevant_topics, query
- )
-
# Create findings with sources included in content
findings = []
@@ -986,7 +996,7 @@ Otherwise, respond with only the follow-up question, nothing else.
"phase": "Research Answer",
"content": generated_text,
"question": query,
- "documents": topic_sources, # All sources from validated topics
+ "search_results": topic_sources, # Changed from documents to search_results for format_findings
}
findings.append(answer_finding)
@@ -995,7 +1005,7 @@ Otherwise, respond with only the follow-up question, nothing else.
"phase": "Topic Organization",
"content": topic_graph_str,
"question": query,
- "documents": topic_sources,
+ "search_results": topic_sources, # Changed from documents to search_results
"topics": [topic.to_dict() for topic in relevant_topics],
}
findings.append(topic_finding)
@@ -1006,10 +1016,26 @@ Otherwise, respond with only the follow-up question, nothing else.
"phase": f"Topic {i}: {topic.title}",
"content": self._format_single_topic_with_sources(topic),
"question": query,
- "documents": topic.get_all_sources(),
+ "search_results": topic.get_all_sources(), # Changed from documents to search_results
}
findings.append(topic_finding)
+ # Set questions by iteration for proper formatting
+ self.findings_repository.set_questions_by_iteration(
+ source_results.get("questions_by_iteration", {})
+ )
+
+ # Format findings properly with sources using findings repository
+ # The generated_text becomes the synthesized_content for format_findings
+ synthesized_content = (
+ generated_text
+ if generated_text
+ else self._format_topic_findings(relevant_topics, query)
+ )
+ formatted_findings = self.findings_repository.format_findings_to_text(
+ findings, synthesized_content
+ )
+
return {
# Standard fields expected by the system
"findings": findings,
@@ -1213,11 +1239,11 @@ Otherwise, respond with only the follow-up question, nothing else.
# Part 1: Generate comprehensive paragraph from lead sources
lead_sources = []
for topic in topics:
- # Use the existing index from the source (already set by source_based_strategy)
+ # Sources now have correct sequential indices
lead_sources.append(topic.lead_source)
- # Create documents for citation handler using existing indices
- # Pass nr_of_links=0 since sources already have correct indices
+ # Create documents for citation handler using re-indexed sources
+ # Pass nr_of_links=0 since we just re-indexed all sources
lead_documents = self.citation_handler._create_documents(
lead_sources, nr_of_links=0
)
@@ -1259,13 +1285,13 @@ Make the paragraph substantive and informative."""
f"- Topic {j + 1} ({other_topic.title}): {other_lead.get('snippet', '')[:150]}"
)
- # Get ALL sources from this topic (they already have indices from source_based_strategy)
+ # Get ALL sources from this topic (now with sequential re-indexed numbers)
topic_sources = []
topic_sources.append(topic.lead_source)
topic_sources.extend(topic.supporting_sources)
- # Create documents and format using existing indices
- # Pass nr_of_links=0 since sources already have correct indices
+ # Create documents and format using re-indexed sources
+ # Pass nr_of_links=0 since we just re-indexed all sources
topic_documents = self.citation_handler._create_documents(
topic_sources, nr_of_links=0
)