Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:24mlight A Share investment Agent TavilyNewsSearch search

From Leeroopedia
Knowledge Sources
Domains Web_Scraping, Data_Engineering
Last Updated 2026-10-10 14:00 GMT

Overview

Concrete tool for Tavily API news search with automatic API key rotation, payload construction, and result parsing, provided by the A_Share_investment_Agent repository.

Description

The TavilyNewsSearch class implements the NewsSearchProvider abstract interface for the Tavily news search API. It collects API keys from constructor arguments and environment variables (TAVILY_API_KEY, TAVILY_API_KEYS), rotates through keys on failure, and parses raw API responses into structured NewsSearchResult dataclass instances.

Key methods:

  • __init__: Collects and deduplicates API keys from args and env
  • is_available: Returns True if any keys configured
  • search: Executes search with key rotation
  • _build_payload: Builds Tavily request (topic="news", max_results capped at 10)
  • _parse_results: Parses Tavily response into NewsSearchResult objects

Usage

Import when you need to:

  • Search for news via the Tavily API
  • Support API key rotation for reliability

Code Reference

Source Location

  • Repository: A_Share_investment_Agent
  • File: src/crawler/news_search/tavily_impl.py
  • Lines: L24-178 (class), L77-120 (search), L122-150 (_build_payload), L152-178 (_parse_results)

Signature

class TavilyNewsSearch(NewsSearchProvider):
    def __init__(
        self,
        api_key: Optional[str] = None,
        api_keys: Optional[List[str]] = None,
        timeout_seconds: int = 30,
    ):
        """Initialize with API keys from args and environment."""

    def is_available(self) -> bool:
        """Check if Tavily is available (has API keys)."""

    def search(self, query: SearchQuery) -> List[NewsSearchResult]:
        """Execute Tavily search with key rotation."""

    def _build_payload(self, query: SearchQuery) -> Dict[str, Any]:
        """Build Tavily API request payload."""

    def _parse_results(self, raw_results: List[Any]) -> List[NewsSearchResult]:
        """Parse Tavily API response into NewsSearchResult objects."""

Import

from src.crawler.news_search.tavily_impl import TavilyNewsSearch
from src.crawler.news_search.base import SearchQuery, NewsSearchResult, convert_to_news_format

External Reference

I/O Contract

Inputs

Name Type Required Description
query SearchQuery Yes Dataclass with query string, max_results, topic, days_back, start_date, end_date

Outputs

Name Type Description
return List[NewsSearchResult] Results with title, url, content, source, published_date, score

Usage Examples

Basic Search

from src.crawler.news_search.tavily_impl import TavilyNewsSearch
from src.crawler.news_search.base import SearchQuery, convert_to_news_format

search = TavilyNewsSearch()
if search.is_available():
    query = SearchQuery(
        query="贵州茅台 股票新闻",
        max_results=10,
        days_back=7,
    )
    results = search.search(query)
    news_list = convert_to_news_format(results, symbol="600519")

With Date Range

query = SearchQuery(
    query="沪深300 宏观新闻",
    max_results=10,
    start_date="2025-01-08",
    end_date="2025-01-15",
)
results = search.search(query)

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment