{"record":{"id":"85f8e5815b0b0a85","repo":"TauricResearch/TradingAgents","slug":"alpha-vantage-api-key-environment-variable-is-not","errorCode":null,"errorMessage":"ALPHA_VANTAGE_API_KEY environment variable is not set.","messagePattern":"ALPHA_VANTAGE_API_KEY environment variable is not set\\.","errorType":"exception","errorClass":"AlphaVantageNotConfiguredError","httpStatus":null,"severity":"error","filePath":"tradingagents/dataflows/alpha_vantage_common.py","lineNumber":32,"sourceCode":"# CLI/agents indefinitely (#990).\nREQUEST_TIMEOUT = 30\n\n\nclass AlphaVantageNotConfiguredError(VendorNotConfiguredError):\n    \"\"\"Raised when Alpha Vantage is selected but no API key is configured.\n\n    A VendorNotConfiguredError (and thus still a ValueError), so the routing\n    layer's \"vendor unavailable\" handling and existing ValueError callers both\n    keep working.\n    \"\"\"\n    pass\n\n\ndef get_api_key() -> str:\n    \"\"\"Retrieve the API key for Alpha Vantage from environment variables.\"\"\"\n    api_key = os.getenv(\"ALPHA_VANTAGE_API_KEY\")\n    if not api_key:\n        raise AlphaVantageNotConfiguredError(\n            \"ALPHA_VANTAGE_API_KEY environment variable is not set.\"\n        )\n    return api_key\n\ndef format_datetime_for_api(date_input) -> str:\n    \"\"\"Convert various date formats to YYYYMMDDTHHMM format required by Alpha Vantage API.\"\"\"\n    if isinstance(date_input, str):\n        # If already in correct format, return as-is\n        if len(date_input) == 13 and 'T' in date_input:\n            return date_input\n        # Try to parse common date formats\n        try:\n            dt = datetime.strptime(date_input, \"%Y-%m-%d\")\n            return dt.strftime(\"%Y%m%dT0000\")\n        except ValueError:\n            try:\n                dt = datetime.strptime(date_input, \"%Y-%m-%d %H:%M\")\n                return dt.strftime(\"%Y%m%dT%H%M\")","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TauricResearch/TradingAgents/blob/a33fd4c0f134485a43553a2c23a63cb14adbd88f/tradingagents/dataflows/alpha_vantage_common.py#L14-L50","documentation":"Raised by get_api_key() in tradingagents/dataflows/alpha_vantage_common.py when the ALPHA_VANTAGE_API_KEY environment variable is unset or empty. It is an AlphaVantageNotConfiguredError, a subclass of VendorNotConfiguredError (which is also a ValueError), so the router treats it as 'vendor unavailable' and tries the next vendor in the chain. It only surfaces to you when no configured vendor can serve the request.","triggerScenarios":"Calling any Alpha Vantage-backed tool (get_stock_data_indicators, get_historical_prices, etc.) with data_vendors containing 'alpha_vantage' while the ALPHA_VANTAGE_API_KEY env var is not exported, is set to an empty string, or is only set in a shell where the process wasn't restarted.","commonSituations":"New machine/container without the key in .env or the shell profile; running in CI or Docker where the env var was not passed (--env/-e omitted); typos in the variable name; assuming yfinance works the same without keys after switching the data_vendors config to alpha_vantage.","solutions":["Export the key in the shell where you run the app: export ALPHA_VANTAGE_API_KEY=\"yourkey\" (get a free key at https://www.alphavantage.co/support/#api-key)","If the project uses a .env file, add ALPHA_VANTAGE_API_KEY=... there and confirm it is loaded before dataflows code runs","For Docker/CI, pass the variable explicitly: docker run -e ALPHA_VANTAGE_API_KEY=... or set it in the pipeline secrets","If you do not want Alpha Vantage, change config data_vendors (or tool_vendors) for the category back to 'yfinance' or 'default' so alpha_vantage is never attempted"],"exampleFix":"# before\n# (ALPHA_VANTAGE_API_KEY not set, data_vendors=\"alpha_vantage\")\n# -> AlphaVantageNotConfiguredError: ALPHA_VANTAGE_API_KEY environment variable is not set.\n\n# after\nexport ALPHA_VANTAGE_API_KEY=\"yourkey\"   # in shell / .env / docker -e\n# or switch the vendor\n# config: {\"data_vendors\": {\"stock_data\": \"yfinance\"}}","handlingStrategy":"validation","validationCode":"import os\nfrom tradingagents.dataflows.alpha_vantage_common import AlphaVantageNotConfiguredError\n\ndef alpha_vantage_ready() -> bool:\n    return bool(os.getenv(\"ALPHA_VANTAGE_API_KEY\"))","typeGuard":"def is_configured_env(name: str) -> bool:\n    v = os.getenv(name)\n    return isinstance(v, str) and v.strip() != \"\"","tryCatchPattern":"try:\n    get_historical_prices(\"AAPL\", \"2025-01-10\", \"2025-01-15\")\nexcept AlphaVantageNotConfiguredError as e:  # also a ValueError\n    logger.warning(\"Alpha Vantage skipped: %s\", e)\n    # fall back to yfinance explicitly, or report data unavailable","preventionTips":["Put ALPHA_VANTAGE_API_KEY in your .env / shell profile and confirm with printenv before running","Pass env vars explicitly in Docker (docker run -e) and CI secrets","Configure a vendor chain (data_vendors=\"alpha_vantage,yfinance\") so a missing key degrades instead of aborting"],"tags":["configuration","api-key","alpha-vantage","environment"],"backgroundTag":null,"analyzedSha":"a33fd4c0f134485a43553a2c23a63cb14adbd88f","analyzedAt":"2026-08-14T19:45:16.920Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}