HKUDS/Vibe-Trading · error · ValueError

period {period!r} must be YYYY-YYYY or YYYY-MM-DD/YYYY-MM-DD

Error message

period {period!r} must be YYYY-YYYY or YYYY-MM-DD/YYYY-MM-DD

What it means

_parse_period only accepts two formats: 'YYYY-YYYY' (year range, expanded to Jan 1 – Dec 31) or 'YYYY-MM-DD/YYYY-MM-DD'. Anything else that is a string but matches neither regex raises this.

Source

Thrown at agent/src/tools/alpha_bench_tool.py:92

    "csi300": "equity_cn",
    "sp500": "equity_us",
    "btc-usdt": "crypto",
}


def _parse_period(period: str) -> tuple[str, str]:
    """Return (start_date, end_date) as YYYY-MM-DD strings."""
    if not isinstance(period, str):
        raise ValueError(f"period must be string, got {type(period).__name__}")
    m = _PERIOD_DATE.match(period)
    if m:
        start, end = m.group(1), m.group(2)
    else:
        m = _PERIOD_YEAR.match(period)
        if m:
            start, end = f"{m.group(1)}-01-01", f"{m.group(2)}-12-31"
        else:
            raise ValueError(
                f"period {period!r} must be YYYY-YYYY or YYYY-MM-DD/YYYY-MM-DD"
            )
    # Match backtest loaders.validate_date_range: reject inverted ranges.
    if pd.Timestamp(start) > pd.Timestamp(end):
        raise ValueError(f"start_date ({start}) > end_date ({end})")
    return start, end


def _load_universe_panel(
    universe: str, period: str, *, use_cache: bool = True
) -> dict[str, pd.DataFrame]:
    """Load OHLCV(+amount, +vwap) wide panel for the requested universe.

    Returns a dict keyed by panel column (open/high/low/close/volume/amount/vwap)
    where each value is a wide ``pd.DataFrame`` indexed by date (DatetimeIndex)
    with one column per instrument.

    Args:

View on GitHub (pinned to 80ffdda44c)

Solutions

  1. Use 'YYYY-YYYY' for whole years (e.g. '2020-2023')
  2. Use 'YYYY-MM-DD/YYYY-MM-DD' with a slash separator for exact ranges
  3. Add a pre-call regex check mirroring _PERIOD_YEAR/_PERIOD_DATE to give a better message

Example fix

// before
alpha_bench(universe='csi300', period='2020 to 2023')
// after
alpha_bench(universe='csi300', period='2020-2023')
Defensive patterns

Strategy: validation

Validate before calling

import re
assert re.fullmatch(r'\d{4}-\d{4}|\d{4}-\d{2}-\d{2}/\d{4}-\d{2}-\d{2}', period), 'bad period format'

Type guard

import re
def is_valid_period(p: str) -> bool:
    return bool(re.fullmatch(r'\d{4}(-\d{2}-\d{2})?/\d{4}(-\d{2}-\d{2})?|\d{4}-\d{4}', p))

Try / catch

try:
    _parse_period(period)
except ValueError as e:
    raise ToolInputError(f'fix period format: {e}') from e

Prevention

When it happens

Trigger: Calling alpha_bench-family tools with period='2020', '2020/2021', '2020-01-01 - 2023-12-31', 'last 3 years', or other free-form strings.

Common situations: LLM tool calls with natural-language periods; users copying formats from other tools (dash instead of slash separators); locale-formatted dates.

Related errors


AI-assisted analysis of HKUDS/Vibe-Trading@80ffdda44c (2026-08-28). Data as JSON: /api/errors/1c0d703763a47f31. Report an issue: GitHub.