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:
2026-04-28 07:50:03 +09:00
parent fffbab201d
commit 5f6438ecf3

View File

@@ -11,7 +11,7 @@ from __future__ import annotations
import json
import re
import sys
from typing import Callable, List
from typing import Callable, List, Optional
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(
query: str,
*,
llm_caller: Callable[..., str] = None,
llm_caller: Optional[Callable[..., str]] = None,
max_variants: int = 5,
) -> List[str]:
"""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.
match = re.search(r'\[.*\]', response, re.DOTALL)
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)
return [query]
try:
variants = json.loads(match.group(0))
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)
return [query]
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]
# Always include the original query first; dedupe; cap at max_variants.