hsliuping/TradingAgents-CN · error · AlphaVantageAPIError

Failed to get data from Alpha Vantage after {max_retries} at

Error message

Failed to get data from Alpha Vantage after {max_retries} attempts

What it means

Raised as AlphaVantageAPIError after the retry loop finishes without returning data and without a more specific exception — the theoretical 'all retries exhausted' fallback at the end of _make_api_request. In practice it appears when every attempt hits a path that continues rather than raises (e.g. benign rate-limit Notes exhausted the budget).

Source

Thrown at tradingagents/dataflows/providers/us/alpha_vantage_common.py:267

                time.sleep(retry_delay)
                continue
            else:
                raise AlphaVantageAPIError("Alpha Vantage API request timeout")
                
        except requests.exceptions.RequestException as e:
            logger.error(f"❌ [Alpha Vantage] 请求失败: {e}")
            if attempt < max_retries - 1:
                time.sleep(retry_delay)
                continue
            else:
                raise AlphaVantageAPIError(f"Alpha Vantage API request failed: {e}")
        
        except json.JSONDecodeError as e:
            logger.error(f"❌ [Alpha Vantage] JSON 解析失败: {e}")
            raise AlphaVantageAPIError(f"Failed to parse Alpha Vantage API response: {e}")
    
    # 所有重试都失败
    raise AlphaVantageAPIError(f"Failed to get data from Alpha Vantage after {max_retries} attempts")


def format_response_as_string(data: Dict[str, Any], title: str = "Alpha Vantage Data") -> str:
    """
    将 API 响应格式化为字符串
    
    Args:
        data: API 响应数据
        title: 数据标题
        
    Returns:
        格式化后的字符串
    """
    try:
        # 添加头部信息
        header = f"# {title}\n"
        header += f"# Data retrieved on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
        

View on GitHub (pinned to 74783e8817)

Solutions

  1. Treat it like a rate-limit/connectivity failure: back off substantially and retry the whole request later
  2. Increase retry_delay and max_retries so throttled attempts eventually succeed
  3. Reduce concurrent Alpha Vantage usage or upgrade the key plan

Example fix

# before
max_retries, retry_delay = 1, 0
# after
max_retries, retry_delay = 5, 12  # survive throttled windows
Defensive patterns

Strategy: retry

Try / catch

from tradingagents.dataflows.providers.us.alpha_vantage_common import AlphaVantageAPIError
for delay in (60, 300, 900):
    try:
        data = _make_api_request(params); break
    except AlphaVantageAPIError as e:
        if 'after' in str(e) and 'attempts' in str(e):
            time.sleep(delay)
        else:
            raise

Prevention

When it happens

Trigger: max_retries attempts all take the `continue` branch (rate-limit Note with wait then retry) and the loop ends, reaching the trailing raise instead of the dedicated rate-limit error.

Common situations: Tuning max_retries/retry_delay down so the loop drains on retryable conditions; concurrent callers sharing a quota so every attempt is throttled.

Related errors


AI-assisted analysis of hsliuping/TradingAgents-CN@74783e8817 (2026-08-28). Data as JSON: /api/errors/e074e2831be45717. Report an issue: GitHub.