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(quarto): don't cite [N] missing from Sources (#7955)

Vladimir Babin347e1b289193
diff --git a/changelog.d/7913.bugfix.md b/changelog.d/7913.bugfix.md
new file mode 100644
index 0000000000..00925cee9e
--- /dev/null
+++ b/changelog.d/7913.bugfix.md
@@ -0,0 +1 @@
+Quarto export now leaves `[N]` and `[N, M]` as literal text when the Sources section has no entry for every number, instead of emitting `[@refN]` citations that `references.bib` does not define, matching the LaTeX export.
diff --git a/src/local_deep_research/text_optimization/citation_formatter.py b/src/local_deep_research/text_optimization/citation_formatter.py
index 6db89e1dd6..e64c447933 100644
--- a/src/local_deep_research/text_optimization/citation_formatter.py
+++ b/src/local_deep_research/text_optimization/citation_formatter.py
@@ -1798,11 +1798,20 @@ csl: apa.csl
# brackets of a rejected span are never mistaken for a citation.
processed_content = _defuse_report_math_for_quarto(processed_content)
+ # Cite only what references.bib defines: the bibliography is built
+ # from these entries, so a number missing from them would be an
+ # undefined reference. As in the LaTeX export, a group is cited only
+ # when every member is listed, and anything else is prose such as
+ # "versions [4, 5]" or "[2020]" and is left as written.
+ entries = self._bibliography_entries(content)
+
# First handle comma-separated citations like [1, 2, 3]
def replace_comma_citations(match):
citation_nums = match.group(1)
# Split by comma and strip whitespace
nums = [num.strip() for num in citation_nums.split(",")]
+ if not all(num in entries for num in nums):
+ return match.group(0)
refs = [f"@ref{num}" for num in nums]
return f"[{', '.join(refs)}]"
@@ -1813,6 +1822,8 @@ csl: apa.csl
# Then convert individual citations to Quarto format [@citation]
def replace_citation(match):
citation_num = match.group(1)
+ if citation_num not in entries:
+ return match.group(0)
return f"[@ref{citation_num}]"
processed_content = self.citation_pattern.sub(
@@ -1860,15 +1871,19 @@ csl: apa.csl
+ bibliography_note
)
- def _generate_bibliography(self, content: str) -> str:
- """Generate BibTeX bibliography from sources."""
+ def _bibliography_entries(self, content: str) -> dict:
+ """The entries references.bib is generated from."""
sources_start, _on_sentinel = find_sources_section(content)
# Keep accepting bare source lists for programmatic callers. A full
# report's explicit Sources boundary takes precedence over its body.
sources_content = (
content[sources_start:] if sources_start != -1 else content
)
- entries = _collect_bibliography_entries(sources_content)
+ return _collect_bibliography_entries(sources_content)
+
+ def _generate_bibliography(self, content: str) -> str:
+ """Generate BibTeX bibliography from sources."""
+ entries = self._bibliography_entries(content)
bibliography = ""
for citation_num in sorted(entries, key=_sort_key):
diff --git a/tests/text_optimization/test_citation_formatter.py b/tests/text_optimization/test_citation_formatter.py
index fea75c1952..681eebc109 100644
--- a/tests/text_optimization/test_citation_formatter.py
+++ b/tests/text_optimization/test_citation_formatter.py
@@ -1512,9 +1512,11 @@ Multiple studies [1, 2, 3] have shown promising results.
exporter = QuartoExporter()
result = exporter.export_to_quarto(content)
- # Citations should still be converted
- assert "[@ref1]" in result
- assert "[@ref2]" in result
+ # references.bib has no entries, so there is nothing to cite
+ assert "[@ref1]" not in result
+ assert "[@ref2]" not in result
+ assert "[1]" in result
+ assert "[2]" in result
# Bibliography note should still appear but be empty
assert "Bibliography File Required" in result
@@ -1606,6 +1608,51 @@ Consecutive lenticular【1】【2】【3】citations.
assert "[@ref2]" in result
assert "[@ref3]" in result
+ def test_quarto_single_citation_not_in_sources_left_as_text(self):
+ """A single [N] with no matching Sources entry stays literal text."""
+ content = """# Report
+
+Prose mentioning a year [2020] and a stray [0] index, plus real [1].
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+ body = result.split("**Bibliography File Required**")[0]
+
+ assert "[@ref1]" in body
+ assert "[@ref2020]" not in body
+ assert "[@ref0]" not in body
+ assert "[2020]" in body
+ assert "[0]" in body
+
+ def test_quarto_group_cited_only_when_all_members_in_sources(self):
+ """A [N, M] group is cited only when Sources lists every member."""
+ content = """# Report
+
+Real group [1, 2], partly listed [2, 3], and versions [4, 5].
+
+## Sources
+
+[1] First Source
+ URL: https://example.com/1
+
+[2] Second Source
+ URL: https://example.com/2
+"""
+ exporter = QuartoExporter()
+ result = exporter.export_to_quarto(content)
+ body = result.split("**Bibliography File Required**")[0]
+
+ assert "[@ref1, @ref2]" in body
+ assert "@ref3" not in body
+ assert "@ref4" not in body
+ assert "[2, 3]" in body
+ assert "[4, 5]" in body
+
class TestExtractDomainExceptionHandling:
"""Tests for _extract_domain handling malformed URLs (PR #2016)."""
diff --git a/tests/text_optimization/test_citation_formatter_behavior.py b/tests/text_optimization/test_citation_formatter_behavior.py
index 0cf5ccbbbf..59b41c82f9 100644
--- a/tests/text_optimization/test_citation_formatter_behavior.py
+++ b/tests/text_optimization/test_citation_formatter_behavior.py
@@ -798,7 +798,7 @@ class TestQuartoExporterExportToQuarto:
)
exporter = QuartoExporter()
- content = "See [1] for details."
+ content = "See [1] for details.\n\n## Sources\n\n[1] A Source\n"
result = exporter.export_to_quarto(content)
assert "[@ref1]" in result
@@ -809,7 +809,10 @@ class TestQuartoExporterExportToQuarto:
)
exporter = QuartoExporter()
- content = "See [1, 2, 3] for details."
+ content = (
+ "See [1, 2, 3] for details.\n\n## Sources\n\n"
+ "[1] A Source\n\n[2] B Source\n\n[3] C Source\n"
+ )
result = exporter.export_to_quarto(content)
assert "@ref1" in result
assert "@ref2" in result