feat: Add automatic retry with simpler query for sparse Reddit results

If initial search returns <5 threads, extract core subject and retry:
- "best nano banana prompting practices" → retry with "nano banana"
- Combines results from both searches, deduped by URL

Note: OpenAI's web_search still tends to find old content. This retry
helps cast a wider net but doesn't fully solve the recency issue.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Matt Van Horn
2026-01-25 10:44:29 -08:00
parent 2d43571875
commit e1d3570667
2 changed files with 32 additions and 0 deletions
+21
View File
@@ -120,6 +120,27 @@ def run_research(
# Parse response # Parse response
reddit_items = openai_reddit.parse_reddit_response(raw_openai) reddit_items = openai_reddit.parse_reddit_response(raw_openai)
# Quick retry with simpler query if few results
if len(reddit_items) < 5 and not mock:
core = openai_reddit._extract_core_subject(topic)
if core.lower() != topic.lower():
try:
retry_raw = openai_reddit.search_reddit(
config["OPENAI_API_KEY"],
selected_models["openai"],
core,
from_date, to_date,
depth=depth,
)
retry_items = openai_reddit.parse_reddit_response(retry_raw)
# Add items not already found (by URL)
existing_urls = {item.get("url") for item in reddit_items}
for item in retry_items:
if item.get("url") not in existing_urls:
reddit_items.append(item)
except Exception:
pass
if progress: if progress:
progress.end_reddit(len(reddit_items)) progress.end_reddit(len(reddit_items))
+11
View File
@@ -66,6 +66,16 @@ Return JSON:
}}""" }}"""
def _extract_core_subject(topic: str) -> str:
"""Extract core subject from verbose query for retry."""
noise = ['best', 'top', 'how to', 'tips for', 'practices', 'features',
'killer', 'guide', 'tutorial', 'recommendations', 'advice',
'prompting', 'using', 'for', 'with', 'the', 'of', 'in', 'on']
words = topic.lower().split()
result = [w for w in words if w not in noise]
return ' '.join(result[:3]) or topic # Keep max 3 words
def search_reddit( def search_reddit(
api_key: str, api_key: str,
model: str, model: str,
@@ -74,6 +84,7 @@ def search_reddit(
to_date: str, to_date: str,
depth: str = "default", depth: str = "default",
mock_response: Optional[Dict] = None, mock_response: Optional[Dict] = None,
_retry: bool = False,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""Search Reddit for relevant threads using OpenAI Responses API. """Search Reddit for relevant threads using OpenAI Responses API.