Skip to content

Commit d136813

Browse files
fix: ignore non-list QueryExpander replies (#11630)
Co-authored-by: Stefano Fiorucci <stefanofiorucci@gmail.com>
1 parent 3bc4aaf commit d136813

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

haystack/components/query/query_expander.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ def _parse_expanded_queries(generator_response: str) -> list[str]:
265265
if parsed is None:
266266
return []
267267

268+
if not isinstance(parsed["queries"], list):
269+
logger.warning(
270+
"Expected 'queries' to be a list but got {type}. Returning no expanded queries.",
271+
type=type(parsed["queries"]).__name__,
272+
)
273+
return []
274+
268275
queries = []
269276
for item in parsed["queries"]:
270277
if isinstance(item, str) and item.strip():
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
``QueryExpander`` now ignores malformed responses where ``queries`` is not a list, so a string response is no longer
5+
split into single-character queries.

test/components/query/test_query_expander.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ def test_parse_expanded_queries_non_list_json(self, monkeypatch):
193193
queries = expander._parse_expanded_queries('{"not": "a list"}')
194194
assert queries == []
195195

196+
@pytest.mark.parametrize("queries_value", ['"single query"', '{"query": "value"}', "null"])
197+
def test_parse_expanded_queries_rejects_non_list_queries_value(self, monkeypatch, caplog, queries_value):
198+
monkeypatch.setenv("OPENAI_API_KEY", "test-key-12345")
199+
expander = QueryExpander()
200+
201+
with caplog.at_level(logging.WARNING):
202+
queries = expander._parse_expanded_queries(f'{{"queries": {queries_value}}}')
203+
204+
assert queries == []
205+
assert "Expected 'queries' to be a list" in caplog.text
206+
196207
def test_parse_expanded_queries_mixed_types(self, monkeypatch):
197208
monkeypatch.setenv("OPENAI_API_KEY", "test-key-12345")
198209
expander = QueryExpander()

0 commit comments

Comments
 (0)