{"record":{"id":"9cf53745124b1b15","repo":"TauricResearch/TradingAgents","slug":"unsupported-date-format-date-input","errorCode":null,"errorMessage":"Unsupported date format: {date_input}","messagePattern":"Unsupported date format: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"tradingagents/dataflows/alpha_vantage_common.py","lineNumber":52,"sourceCode":"        )\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\")\n            except ValueError:\n                raise ValueError(f\"Unsupported date format: {date_input}\") from None\n    elif isinstance(date_input, datetime):\n        return date_input.strftime(\"%Y%m%dT%H%M\")\n    else:\n        raise ValueError(f\"Date must be string or datetime object, got {type(date_input)}\")\n\nclass AlphaVantageRateLimitError(VendorRateLimitError):\n    \"\"\"Raised when the Alpha Vantage API rate limit is exceeded.\"\"\"\n    pass\n\ndef _make_api_request(function_name: str, params: dict) -> dict | str:\n    \"\"\"Helper function to make API requests and handle responses.\n\n    Raises:\n        AlphaVantageRateLimitError: When API rate limit is exceeded\n    \"\"\"\n    # Create a copy of params to avoid modifying the original\n    api_params = params.copy()\n    api_params.update({","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/TauricResearch/TradingAgents/blob/a33fd4c0f134485a43553a2c23a63cb14adbd88f/tradingagents/dataflows/alpha_vantage_common.py#L34-L70","documentation":"Raised by format_datetime_for_api() in tradingagents/dataflows/alpha_vantage_common.py when a date string is neither already in the 13-char YYYYMMDDTHHMM form, nor parseable as '%Y-%m-%d', nor as '%Y-%m-%d %H:%M'. It is a plain ValueError (raised 'from None' so the underlying strptime error is suppressed). It exists to reject date strings the Alpha Vantage intraday API cannot accept before the request is made.","triggerScenarios":"Passing dates like '2025/01/15', 'Jan 15 2025', '2025-01-15T10:30', ISO strings with seconds/timezone ('2025-01-15T10:30:00Z'), or strings with stray whitespace to any code path that formats datetimes for Alpha Vantage (e.g. intraday range endpoints).","commonSituations":"LLM tool calls emitting ISO-8601 timestamps; frontend code forwarding browser Date.toJSON() output; users typing slash-formatted dates; mixing datetime.isoformat() output with the expected format.","solutions":["Pass the date as a datetime.datetime object instead of a string — the function formats it correctly itself","Use one of the accepted string forms: 'YYYY-MM-DD', 'YYYY-MM-DD HH:MM', or the pre-formatted 'YYYYMMDDTHHMM'","Normalize upstream: convert ISO strings with datetime.fromisoformat(s) (strip 'Z' first) before passing the datetime through","Pre-validate with a helper that parses the common formats and re-emits '%Y-%m-%d' before calling the library"],"exampleFix":"# before\nformat_datetime_for_api(\"2025-01-15T10:30:00Z\")\n# -> ValueError: Unsupported date format: 2025-01-15T10:30:00Z\n\n# after\nfrom datetime import datetime\ndt = datetime.fromisoformat(\"2025-01-15T10:30:00\".replace(\"Z\", \"+00:00\"))\nformat_datetime_for_api(dt)  # -> \"20250115T1030\"","handlingStrategy":"validation","validationCode":"from datetime import datetime\n\ndef to_api_date(value: str) -> str:\n    \"\"\"Normalize common date strings to 'YYYY-MM-DD' the formatter accepts.\"\"\n    for fmt in (\"%Y-%m-%d %H:%M\", \"%Y-%m-%d\"):\n        try:\n            return datetime.strptime(value.strip(), fmt).strftime(\"%Y-%m-%d\")\n        except ValueError:\n            continue\n    dt = datetime.fromisoformat(value.replace(\"Z\", \"+00:00\"))  # ISO-8601\n    return dt.strftime(\"%Y-%m-%d\")","typeGuard":"import re\nfrom datetime import datetime\n\ndef is_acceptable_date(v) -> bool:\n    if isinstance(v, datetime):\n        return True\n    return isinstance(v, str) and bool(\n        re.fullmatch(r\"\\d{8}T\\d{4}|\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2})?\", v.strip())\n    )","tryCatchPattern":"try:\n    ts = format_datetime_for_api(start)\nexcept ValueError as e:\n    raise ValueError(f\"Fix the date input {start!r}: use 'YYYY-MM-DD' or a datetime\") from e","preventionTips":["Pass datetime objects instead of strings wherever possible","Ban ISO-8601-with-Z strings at your API boundary; normalize them once on ingress","Unit-test date normalization with the formats your LLM/frontend actually emits"],"tags":["validation","date-format","alpha-vantage","valueerror"],"backgroundTag":null,"analyzedSha":"a33fd4c0f134485a43553a2c23a63cb14adbd88f","analyzedAt":"2026-08-14T19:45:16.920Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}