Skip to content

Commit 5fb28b9

Browse files
committed
Parse index name from chunk metadata source attribute
1 parent 36c2ae2 commit 5fb28b9

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

src/utils/responses.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,16 @@ def _resolve_source_for_result(
842842

843843
if len(vector_store_ids) > 1:
844844
attributes = getattr(result, "attributes", {}) or {}
845+
846+
# Primary: read index name embedded directly by rag-content.
847+
# This value is already the user-facing rag_id, not a vector_db_id,
848+
# so no mapping is needed.
849+
attr_source: Optional[str] = attributes.get("source")
850+
if attr_source:
851+
return attr_source
852+
853+
# Fallback: if llama-stack ever populates vector_store_id in results,
854+
# use it with the rag_id_mapping.
845855
attr_store_id: Optional[str] = attributes.get("vector_store_id")
846856
if attr_store_id:
847857
return rag_id_mapping.get(attr_store_id, attr_store_id)

tests/unit/utils/test_responses.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,69 @@ def test_multiple_stores_attribute_not_in_mapping(
20442044
)
20452045
assert source == "vs-unknown"
20462046

2047+
def test_multiple_stores_source_attribute_fallback(
2048+
self, mocker: MockerFixture
2049+
) -> None:
2050+
"""Test resolution falls back to source attribute when no vector_store_id."""
2051+
mock_result = mocker.Mock()
2052+
mock_result.filename = "file-abc123"
2053+
mock_result.attributes = {"source": "ocp-documentation"}
2054+
2055+
source = _resolve_source_for_result(
2056+
mock_result,
2057+
["vs-001", "vs-002"],
2058+
{"vs-001": "ocp-4.18-docs"},
2059+
)
2060+
assert source == "ocp-documentation"
2061+
2062+
def test_multiple_stores_source_attribute_ignores_mapping(
2063+
self, mocker: MockerFixture
2064+
) -> None:
2065+
"""Test source attribute is returned directly without rag_id_mapping lookup."""
2066+
mock_result = mocker.Mock()
2067+
mock_result.filename = "file-abc123"
2068+
mock_result.attributes = {"source": "custom-index"}
2069+
2070+
source = _resolve_source_for_result(
2071+
mock_result,
2072+
["vs-001", "vs-002"],
2073+
{"custom-index": "should-not-be-used"},
2074+
)
2075+
assert source == "custom-index"
2076+
2077+
def test_multiple_stores_source_preferred_over_vector_store_id(
2078+
self, mocker: MockerFixture
2079+
) -> None:
2080+
"""Test source attribute takes precedence over vector_store_id."""
2081+
mock_result = mocker.Mock()
2082+
mock_result.filename = "file-abc123"
2083+
mock_result.attributes = {
2084+
"vector_store_id": "vs-002",
2085+
"source": "ocp-documentation",
2086+
}
2087+
2088+
source = _resolve_source_for_result(
2089+
mock_result,
2090+
["vs-001", "vs-002"],
2091+
{"vs-002": "rhel-9-docs"},
2092+
)
2093+
assert source == "ocp-documentation"
2094+
2095+
def test_multiple_stores_no_vector_store_id_no_source(
2096+
self, mocker: MockerFixture
2097+
) -> None:
2098+
"""Test resolution returns None when neither vector_store_id nor source present."""
2099+
mock_result = mocker.Mock()
2100+
mock_result.filename = "file-abc123"
2101+
mock_result.attributes = {"title": "some doc"}
2102+
2103+
source = _resolve_source_for_result(
2104+
mock_result,
2105+
["vs-001", "vs-002"],
2106+
{"vs-001": "ocp-docs"},
2107+
)
2108+
assert source is None
2109+
20472110

20482111
class TestBuildChunkAttributes:
20492112
"""Tests for _build_chunk_attributes function."""

0 commit comments

Comments
 (0)