From 49312cace3f97205f055114dd884519a805b4b1b Mon Sep 17 00:00:00 2001 From: Harsh Kashyap Date: Thu, 25 Jun 2026 16:30:01 +0530 Subject: [PATCH] fix(api): align use_sql Markdown separator with Source header (#16317) --- api/db/services/dialog_service.py | 2 +- ...t_dialog_service_use_sql_source_columns.py | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/api/db/services/dialog_service.py b/api/db/services/dialog_service.py index 360a9ea6a5..860a1dd20c 100644 --- a/api/db/services/dialog_service.py +++ b/api/db/services/dialog_service.py @@ -1309,7 +1309,7 @@ Please correct the error and write SQL again using json_extract_string(chunk_dat # compose Markdown table columns = "|" + "|".join([map_column_name(tbl["columns"][i]["name"]) for i in column_idx]) + ("|Source|" if docid_idx and doc_name_idx else "|") - line = "|" + "|".join(["------" for _ in range(len(column_idx))]) + ("|------|" if docid_idx and docid_idx else "") + line = "|" + "|".join(["------" for _ in range(len(column_idx))]) + ("|------|" if docid_idx and doc_name_idx else "") # Build rows ensuring column names match values - create a dict for each row # keyed by column name to handle any SQL column order diff --git a/test/unit_test/api/db/services/test_dialog_service_use_sql_source_columns.py b/test/unit_test/api/db/services/test_dialog_service_use_sql_source_columns.py index 297cf3f4f1..cd87ca960c 100644 --- a/test/unit_test/api/db/services/test_dialog_service_use_sql_source_columns.py +++ b/test/unit_test/api/db/services/test_dialog_service_use_sql_source_columns.py @@ -157,10 +157,44 @@ def test_use_sql_repairs_missing_source_columns_for_non_aggregate(monkeypatch, f assert result is not None assert "|product|Source|" in result["answer"] + answer_lines = [ln.strip() for ln in result["answer"].splitlines() if ln.strip().startswith("|")] + header, separator = answer_lines[0], answer_lines[1] + assert header.count("|") == separator.count("|") assert len(chat_model.calls) == 2 assert len(retriever.sql_calls) == 2 +@pytest.mark.p2 +def test_use_sql_separator_matches_header_without_doc_name(monkeypatch, force_es_engine): + retriever = _StubRetriever( + [ + { + "columns": [{"name": "doc_id"}, {"name": "product"}], + "rows": [["doc-1", "desk"]], + }, + ] + ) + chat_model = _StubChatModel(["SELECT doc_id, product FROM ragflow_tenant"]) + monkeypatch.setattr(dialog_service.settings, "retriever", retriever, raising=False) + + result = asyncio.run( + dialog_service.use_sql( + question="show product with doc id only", + field_map={"product": "product"}, + tenant_id="tenant-id", + chat_mdl=chat_model, + quota=True, + kb_ids=None, + ) + ) + + assert result is not None + answer_lines = [ln.strip() for ln in result["answer"].splitlines() if ln.strip().startswith("|")] + assert answer_lines[0] == "|product|" + assert answer_lines[1] == "|------" + assert "|------|------|" not in result["answer"] + + @pytest.mark.p2 def test_use_sql_keeps_aggregate_flow_without_source_repair(monkeypatch, force_es_engine): retriever = _StubRetriever(