Heuristic:24mlight A Share investment Agent Tavily Query Sanitization
| Knowledge Sources | |
|---|---|
| Domains | Optimization, LLMs |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Remove Google-style advanced search operators (after:, before:, site:) from LLM-generated search queries because Tavily API does not support them; use Tavily's native parameters instead.
Description
When using LLM to generate news search queries (NEWS_QUERY_MODE=llm), the LLM may produce queries containing Google-style operators like after:2025-01-01, before:2025-12-31, or site:finance.sina.com.cn. Tavily API does not support these operators and treats them as literal search terms, degrading result quality. The _remove_advanced_operators() function strips these operators from LLM output. Date filtering is instead handled via Tavily's native start_date/end_date parameters, and domain filtering via include_domains/exclude_domains.
Usage
Use this heuristic when:
- Using
NEWS_QUERY_MODE=llmto let LLM generate search queries - Migrating from Google search to Tavily search
- LLM-generated queries contain
after:,before:, orsite:operators
The Insight (Rule of Thumb)
- Action: Strip
after:,before:, andsite:from all LLM-generated queries before sending to Tavily. - Value: Use Tavily's
start_date/end_datefor date filtering andinclude_domains/exclude_domainsfor domain filtering. - Trade-off: LLM-generated queries are less expressive but compatible with Tavily; the rule-based query builder (
NEWS_QUERY_MODE=rule, default) avoids this issue entirely. - Compatibility: Applies only when
NEWS_QUERY_MODE=llm; rule-based queries are pre-sanitized.
Reasoning
Tavily's search API is not a Google search proxy — it has its own query language and parameters. Google operators passed as literal text confuse the search engine and reduce relevance. The codebase comment explicitly states: Tavily 不支持 after:/before: 操作符 and Tavily 用 include_domains 参数代替 site:. By stripping operators and using native Tavily parameters, queries produce relevant results. The default rule mode builds queries from templates that are already Tavily-compatible, making this a safety net for LLM mode.
Additionally, the query builder incorporates agent-specific templates: macro news queries focus on monetary policy and regulations, sentiment queries focus on company announcements and orders, and market snapshot queries focus on capital flows and institutional activity.
Code Evidence
Operator removal from src/tools/news_query_builder.py:169-178:
def _remove_advanced_operators(query: str) -> str:
"""移除 Google 高级搜索操作符(Tavily 不支持)"""
query = query.replace("after:", "")
query = query.replace("before:", "")
query = query.replace("site:", "")
query = " ".join(query.split())
return query.strip()
Tavily native date filtering from src/crawler/news_search/tavily_impl.py:134-140:
# 时间过滤(回测对齐)
# 优先使用明确日期区间;否则回退为 days 参数
if query.start_date and query.end_date:
payload["start_date"] = query.start_date
payload["end_date"] = query.end_date
elif query.days_back and int(query.days_back) > 0:
payload["days"] = int(query.days_back)
Mode selection from src/tools/news_query_builder.py:207-216:
mode = (os.getenv("NEWS_QUERY_MODE", "rule") or "rule").lower()
if mode == "llm":
query = _llm_query(symbol, agent_name, trace_state)
if query:
return query
# 默认使用增强版规则查询
return _build_enhanced_query(symbol, agent_name, company_name)