fix(notion-search): polish expand_query (warn on shape error, lint fixes)
Code review polish: - Add stderr warning for the fourth failure path (LLM returned valid JSON but it's not a list-of-strings). Previously this fell back silently, making debugging harder when Haiku produces unexpected shapes. - Drop unused f-string prefixes from two warnings (no interpolation). - Type hint: Callable[..., str] = None → Optional[Callable[..., str]] = None for strict type-checker compatibility. 12/12 tests still pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,7 @@ from __future__ import annotations
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from typing import Callable, List
|
from typing import Callable, List, Optional
|
||||||
|
|
||||||
from _search_llm import call_claude as default_llm_caller
|
from _search_llm import call_claude as default_llm_caller
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ Return ONLY a JSON array of strings, no prose. Example:
|
|||||||
def expand_query(
|
def expand_query(
|
||||||
query: str,
|
query: str,
|
||||||
*,
|
*,
|
||||||
llm_caller: Callable[..., str] = None,
|
llm_caller: Optional[Callable[..., str]] = None,
|
||||||
max_variants: int = 5,
|
max_variants: int = 5,
|
||||||
) -> List[str]:
|
) -> List[str]:
|
||||||
"""Expand a query into related variants. Returns [query] on any failure."""
|
"""Expand a query into related variants. Returns [query] on any failure."""
|
||||||
@@ -51,18 +51,20 @@ def expand_query(
|
|||||||
# Be permissive: extract the first JSON array from the response.
|
# Be permissive: extract the first JSON array from the response.
|
||||||
match = re.search(r'\[.*\]', response, re.DOTALL)
|
match = re.search(r'\[.*\]', response, re.DOTALL)
|
||||||
if not match:
|
if not match:
|
||||||
print(f"Warning: query expansion returned no JSON array; using original query only",
|
print("Warning: query expansion returned no JSON array; using original query only",
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
return [query]
|
return [query]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
variants = json.loads(match.group(0))
|
variants = json.loads(match.group(0))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print(f"Warning: query expansion returned invalid JSON; using original query only",
|
print("Warning: query expansion returned invalid JSON; using original query only",
|
||||||
file=sys.stderr)
|
file=sys.stderr)
|
||||||
return [query]
|
return [query]
|
||||||
|
|
||||||
if not isinstance(variants, list) or not all(isinstance(v, str) for v in variants):
|
if not isinstance(variants, list) or not all(isinstance(v, str) for v in variants):
|
||||||
|
print("Warning: query expansion returned non-string-list shape; using original query only",
|
||||||
|
file=sys.stderr)
|
||||||
return [query]
|
return [query]
|
||||||
|
|
||||||
# Always include the original query first; dedupe; cap at max_variants.
|
# Always include the original query first; dedupe; cap at max_variants.
|
||||||
|
|||||||
Reference in New Issue
Block a user