Skip to content

Commit 5588b50

Browse files
committed
Improve source fallback semantics and use keyword arguments
1 parent 868f293 commit 5588b50

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

src/app/endpoints/streaming_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ async def response_generator( # pylint: disable=too-many-branches,too-many-stat
532532
tool_call, tool_result = build_tool_call_summary(
533533
output_item_done_chunk.item,
534534
turn_summary.rag_chunks,
535-
context.vector_store_ids,
536-
context.rag_id_mapping,
535+
vector_store_ids=context.vector_store_ids,
536+
rag_id_mapping=context.rag_id_mapping,
537537
)
538538
if tool_call:
539539
turn_summary.tool_calls.append(tool_call)

src/utils/responses.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,12 @@ def _resolve_single_store_source(
483483
rag_id_mapping: Mapping from vector_db_id to user-facing rag_id.
484484
485485
Returns:
486-
The resolved rag_id if exactly one store is used, None otherwise.
486+
The resolved rag_id (or raw store_id as fallback) if exactly one
487+
store is used, None otherwise.
487488
"""
488489
if len(vector_store_ids) == 1:
489490
store_id = vector_store_ids[0]
490-
return rag_id_mapping.get(store_id)
491+
return rag_id_mapping.get(store_id, store_id)
491492
return None
492493

493494

@@ -807,15 +808,15 @@ def _resolve_source_for_result(
807808
"""
808809
if len(vector_store_ids) == 1:
809810
store_id = vector_store_ids[0]
810-
return rag_id_mapping.get(store_id, result.filename)
811+
return rag_id_mapping.get(store_id, store_id)
811812

812813
if len(vector_store_ids) > 1:
813814
attributes = getattr(result, "attributes", {}) or {}
814815
attr_store_id: Optional[str] = attributes.get("vector_store_id")
815-
if attr_store_id and attr_store_id in rag_id_mapping:
816-
return rag_id_mapping[attr_store_id]
816+
if attr_store_id:
817+
return rag_id_mapping.get(attr_store_id, attr_store_id)
817818

818-
return result.filename
819+
return None
819820

820821

821822
def _build_chunk_attributes(result: Any) -> Optional[dict[str, Any]]:

tests/unit/utils/test_responses.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ def test_extract_rag_chunks_with_results(self, mocker: MockerFixture) -> None:
13871387

13881388
assert len(rag_chunks) == 2
13891389
assert rag_chunks[0].content == "chunk 1"
1390-
assert rag_chunks[0].source == "doc1.pdf"
1390+
assert rag_chunks[0].source is None
13911391
assert rag_chunks[0].score == 0.9
13921392

13931393
def test_extract_rag_chunks_no_results(self, mocker: MockerFixture) -> None:
@@ -1596,12 +1596,12 @@ def test_single_store_mapped(self, mocker: MockerFixture) -> None:
15961596
assert source == "ocp-4.18-docs"
15971597

15981598
def test_single_store_unmapped(self, mocker: MockerFixture) -> None:
1599-
"""Test resolution with single vector store without mapping falls back to filename."""
1599+
"""Test resolution with single vector store without mapping returns raw store ID."""
16001600
mock_result = mocker.Mock()
16011601
mock_result.filename = "file-abc123"
16021602

16031603
source = _resolve_source_for_result(mock_result, ["vs-unknown"], {})
1604-
assert source == "file-abc123"
1604+
assert source == "vs-unknown"
16051605

16061606
def test_multiple_stores_with_attribute(self, mocker: MockerFixture) -> None:
16071607
"""Test resolution with multiple stores using result attributes."""
@@ -1617,7 +1617,7 @@ def test_multiple_stores_with_attribute(self, mocker: MockerFixture) -> None:
16171617
assert source == "rhel-9-docs"
16181618

16191619
def test_multiple_stores_no_attribute(self, mocker: MockerFixture) -> None:
1620-
"""Test resolution with multiple stores and no vector_store_id attribute."""
1620+
"""Test resolution with multiple stores and no vector_store_id attribute returns None."""
16211621
mock_result = mocker.Mock()
16221622
mock_result.filename = "file-abc123"
16231623
mock_result.attributes = {}
@@ -1627,20 +1627,20 @@ def test_multiple_stores_no_attribute(self, mocker: MockerFixture) -> None:
16271627
["vs-001", "vs-002"],
16281628
{"vs-001": "ocp-4.18-docs", "vs-002": "rhel-9-docs"},
16291629
)
1630-
assert source == "file-abc123"
1630+
assert source is None
16311631

16321632
def test_no_stores(self, mocker: MockerFixture) -> None:
1633-
"""Test resolution with no vector stores falls back to filename."""
1633+
"""Test resolution with no vector stores returns None."""
16341634
mock_result = mocker.Mock()
16351635
mock_result.filename = "file-abc123"
16361636

16371637
source = _resolve_source_for_result(mock_result, [], {})
1638-
assert source == "file-abc123"
1638+
assert source is None
16391639

16401640
def test_multiple_stores_attribute_not_in_mapping(
16411641
self, mocker: MockerFixture
16421642
) -> None:
1643-
"""Test resolution when attribute store ID is not in mapping."""
1643+
"""Test resolution when attribute store ID is not in mapping returns raw store ID."""
16441644
mock_result = mocker.Mock()
16451645
mock_result.filename = "file-abc123"
16461646
mock_result.attributes = {"vector_store_id": "vs-unknown"}
@@ -1650,7 +1650,7 @@ def test_multiple_stores_attribute_not_in_mapping(
16501650
["vs-001", "vs-002"],
16511651
{"vs-001": "ocp-docs"},
16521652
)
1653-
assert source == "file-abc123"
1653+
assert source == "vs-unknown"
16541654

16551655

16561656
class TestBuildChunkAttributes:
@@ -1759,7 +1759,7 @@ def test_chunks_no_mapping_falls_back(self, mocker: MockerFixture) -> None:
17591759
extract_rag_chunks_from_file_search_item(mock_item, rag_chunks)
17601760

17611761
assert len(rag_chunks) == 1
1762-
assert rag_chunks[0].source == "file-abc"
1762+
assert rag_chunks[0].source is None
17631763
assert rag_chunks[0].attributes is None
17641764

17651765
def test_chunks_multiple_stores_attribute_resolution(
@@ -1853,7 +1853,7 @@ def test_file_search_without_mapping(self, mocker: MockerFixture) -> None:
18531853
call_summary, _ = build_tool_call_summary(mock_item, rag_chunks)
18541854

18551855
assert len(rag_chunks) == 1
1856-
assert rag_chunks[0].source == "doc.pdf"
1856+
assert rag_chunks[0].source is None
18571857
assert rag_chunks[0].attributes is None
18581858
assert call_summary is not None
18591859

0 commit comments

Comments
 (0)