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(report): strip bracketed source placeholders (#6381)

Sf15f41ab8db0
diff --git a/changelog.d/6132.bugfix.md b/changelog.d/6132.bugfix.md
new file mode 100644
index 0000000000..ce98d87e37
--- /dev/null
+++ b/changelog.d/6132.bugfix.md
@@ -0,0 +1,2 @@
+Report generation now removes bracketed bibliography placeholder notes and no
+longer prompts models with the heading syntax those placeholders copied.
diff --git a/src/local_deep_research/report_generator.py b/src/local_deep_research/report_generator.py
index d3238e452b..099ee5bade 100644
--- a/src/local_deep_research/report_generator.py
+++ b/src/local_deep_research/report_generator.py
@@ -42,10 +42,9 @@ _SUBSECTION_OUTPUT_GUIDANCE = (
"(#, ##, ###, ####, etc.). The framework already inserts the "
"subsection heading for you; starting with your own heading line "
"creates a visible duplicate next to it in the final report.\n"
- "2. Do NOT end your output with a '## Sources', '## References', "
- "'## Bibliography', '## Citations', '## Key References', or "
- "'## Selected Bibliography' section. The framework appends a single "
- "consolidated '## Sources' block to the entire report after every "
+ "2. Do NOT end your output with a sources, references, bibliography, "
+ "citations, key references, or selected bibliography section. The "
+ "framework appends a single consolidated source list after every "
"subsection is written; including your own bibliography duplicates "
"the same source list.\n"
"3. Begin your output with prose (a paragraph or a table), not a "
@@ -126,9 +125,14 @@ _CITATION_LINE_RE = re.compile(
re.IGNORECASE,
)
-# Italic note line matching LLM boilerplate (e.g. "*(Note: ... bibliography ...)*")
+# Note line matching LLM boilerplate (e.g. "*(Note: ... bibliography ...)*" or
+# "[Note: The framework will append...]")
_BIB_NOTE_RE = re.compile(
- r"^\s*[\*_]\s*\(?Note\b[^\n]*?\b(?:bibliography|sources|references|citations)\b",
+ r"^\s*(?:"
+ r"[\*_]\s*\(?Note\b[^\n]*?\b(?:bibliography|sources?|references?|citations?)\b"
+ r"|\[\s*Note\b"
+ r"(?=[^\n]*\b(?:consolidat\w*|append\w*|omit\w*|duplicat\w*|placeholder\w*)\b)"
+ r"[^\n]*?\b(?:bibliography|sources?|references?|citations?)\b)",
re.IGNORECASE,
)
@@ -731,15 +735,34 @@ class IntegratedReportGenerator:
)
block_text = content[after_heading:block_end]
+ # If the block immediately begins with a code fence, the heading is
+ # introducing fenced example/code content, not a bibliography.
+ offset = 0
+ first_non_empty_start = None
+ for ln in block_text.splitlines(keepends=True):
+ if ln.strip():
+ first_non_empty_start = after_heading + offset
+ break
+ offset += len(ln)
+
+ if first_non_empty_start is not None and _is_in_spans(
+ first_non_empty_start, fence_spans
+ ):
+ continue
+
# Body-shape safeguard: only treat as bibliography if the block
# contains citation-like lines. Require at least one citation
# marker, or >30% of non-empty lines look like citations, to
# avoid deleting substantive analysis headings that happen to
# match the label grammar (e.g. "### Sources for the Analysis"
# with prose body).
- non_empty_lines = [
- ln for ln in block_text.splitlines() if ln.strip()
- ]
+ non_empty_lines = []
+ line_offset = 0
+ for ln in block_text.splitlines(keepends=True):
+ line_start = after_heading + line_offset
+ if ln.strip() and not _is_in_spans(line_start, fence_spans):
+ non_empty_lines.append(ln)
+ line_offset += len(ln)
if not non_empty_lines:
# Empty block — treat as bibliography (heading with no body)
is_bib_block = True
@@ -793,8 +816,10 @@ class IntegratedReportGenerator:
offset = 0
last_citation_end = 0
for ln in lines:
- if _CITATION_LINE_RE.match(ln) or (
- _BIB_NOTE_RE.match(ln) and offset < 300
+ line_start = after_heading + offset
+ if not _is_in_spans(line_start, fence_spans) and (
+ _CITATION_LINE_RE.match(ln)
+ or (_BIB_NOTE_RE.match(ln) and offset < 300)
):
last_citation_end = offset + len(ln)
# Also keep consecutive citation lines; stop at first
@@ -837,15 +862,20 @@ class IntegratedReportGenerator:
non_empty_seen = 0
trailing_content = False
for ln in block_text.splitlines(keepends=True):
+ line_start = after_heading + offset
substantive = bool(ln.strip())
- non_empty_seen += substantive
+ outside_fence = not _is_in_spans(line_start, fence_spans)
+ non_empty_seen += substantive and outside_fence
# The note window is the classifier's first-three-non-empty-
# lines rule, so a block accepted as a bibliography always
# has a qualifying line here and cannot fall through.
is_bib_line = bool(
- _CITATION_LINE_RE.match(ln)
- or (_BIB_NOTE_RE.match(ln) and non_empty_seen <= 3)
- or _HR_LINE_RE.match(ln)
+ outside_fence
+ and (
+ _CITATION_LINE_RE.match(ln)
+ or (_BIB_NOTE_RE.match(ln) and non_empty_seen <= 3)
+ or _HR_LINE_RE.match(ln)
+ )
)
if citation_end and substantive and not is_bib_line:
trailing_content = True
diff --git a/tests/report/test_report_generator_helpers.py b/tests/report/test_report_generator_helpers.py
index 8f4bc0d1d7..fc35fa8dcf 100644
--- a/tests/report/test_report_generator_helpers.py
+++ b/tests/report/test_report_generator_helpers.py
@@ -415,6 +415,110 @@ class TestStripSubsectionBoilerplate:
assert "Some paper" not in result
assert "Body of the subsection with findings." in result
+ def test_strips_bracket_led_bibliography_note(self, generator):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "[Note: The framework will append the consolidated bibliography.]\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert result == "Body of the subsection with findings.\n"
+
+ def test_strips_bracket_led_placeholder_observed_in_the_wild(
+ self, generator
+ ):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "[Note: The framework will append the consolidated '## Sources' block after this subsection, so avoid duplicating it here.]\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert result == "Body of the subsection with findings.\n"
+
+ def test_preserves_bracket_led_editorial_note(self, generator):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "[Note: This report includes sources from oral histories and archives.]\n"
+ "The evidence requires careful interpretation.\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert (
+ "[Note: This report includes sources from oral histories" in result
+ )
+ assert "The evidence requires careful interpretation." in result
+
+ def test_preserves_bracket_led_framework_methodology_note(self, generator):
+ content = (
+ "Analysis.\n\n"
+ "## Sources\n"
+ "[Note: The framework uses primary sources from field interviews.]\n"
+ "The evidence requires careful interpretation.\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert (
+ "[Note: The framework uses primary sources from field interviews.]"
+ in result
+ )
+ assert "The evidence requires careful interpretation." in result
+
+ def test_preserves_bracket_led_placeholder_inside_code_fence(
+ self, generator
+ ):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "```text\n"
+ "[Note: The framework will append the consolidated bibliography.]\n"
+ "```\n"
+ "The example is followed by substantive prose.\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert "[Note: The framework will append" in result
+ assert "The example is followed by substantive prose." in result
+
+ def test_preserves_fenced_only_sources_block(self, generator):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "```text\n"
+ "[Note: The framework will append the consolidated bibliography.]\n"
+ "```\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert "## Sources" in result
+ assert "[Note: The framework will append" in result
+
+ def test_preserves_fence_when_sources_block_has_citation(self, generator):
+ content = (
+ "Body of the subsection with findings.\n\n"
+ "## Sources\n"
+ "```text\n"
+ "[Note: The framework will append the consolidated bibliography.]\n"
+ "```\n"
+ "1. Real citation.\n"
+ "Afterword prose.\n"
+ )
+
+ result = self._strip(generator, content, name="Whatever")
+
+ assert "## Sources" in result
+ assert "[Note: The framework will append" in result
+ assert "1. Real citation." in result
+ assert "Afterword prose." in result
+
def test_strips_selected_bibliography_mid_stream(self, generator):
# Bibliography appears mid-content, then more prose continues.
content = (
@@ -886,3 +990,28 @@ class TestStripEmbeddedBibliographies:
assert "## Sources" not in result
assert "every source consulted" not in result
assert para in result
+
+ def test_strips_bibliography_with_trailing_code_fence(self, generator):
+ content = (
+ "Analysis prose.\n\n"
+ "## Sources\n"
+ "[1] https://example.org/one\n"
+ "[2] https://example.org/two\n\n"
+ "```python\n"
+ "print('trailing code')\n"
+ "```\n"
+ )
+
+ result = self._strip(generator, content)
+
+ assert "## Sources" not in result
+ assert "https://example.org/one" not in result
+ assert "```python\nprint('trailing code')\n```" in result
+
+ def test_subsection_guidance_omits_literal_source_heading(self):
+ from local_deep_research.report_generator import (
+ _SUBSECTION_OUTPUT_GUIDANCE,
+ )
+
+ assert "## Sources" not in _SUBSECTION_OUTPUT_GUIDANCE
+ assert "'## Sources'" not in _SUBSECTION_OUTPUT_GUIDANCE