ZhuLinsen/daily_stock_analysis · error · ValueError

invalid price: {rule.get('price')}

Error message

invalid price: {rule.get('price')}

What it means

Raised for PRICE_CROSS rules when the required 'price' field cannot be converted to float (TypeError when the key is missing — float(None) — or the value is a list/dict; ValueError for strings like '1,800' or 'N/A'). Unlike direction, price has NO default, so omitting it is itself an error.

Source

Thrown at src/agent/events.py:501

            raise ValueError(f"invalid status: {status}") from exc

    ttl_hours = rule.get("ttl_hours")
    if ttl_hours is not None:
        try:
            ttl_value = float(ttl_hours)
        except (TypeError, ValueError) as exc:
            raise ValueError(f"invalid ttl_hours: {ttl_hours}") from exc
        if ttl_value <= 0:
            raise ValueError("ttl_hours must be > 0")

    if alert_type == AlertType.PRICE_CROSS:
        direction = str(rule.get("direction", "above")).lower()
        if direction not in {"above", "below"}:
            raise ValueError(f"invalid direction: {direction}")
        try:
            price = float(rule.get("price"))
        except (TypeError, ValueError) as exc:
            raise ValueError(f"invalid price: {rule.get('price')}") from exc
        if price <= 0:
            raise ValueError("price must be > 0")
    elif alert_type == AlertType.PRICE_CHANGE_PERCENT:
        direction = str(rule.get("direction", "up")).lower()
        if direction not in {"up", "down"}:
            raise ValueError(f"invalid direction: {direction}")
        try:
            change_pct = float(rule.get("change_pct"))
        except (TypeError, ValueError) as exc:
            raise ValueError(f"invalid change_pct: {rule.get('change_pct')}") from exc
        if change_pct <= 0:
            raise ValueError("change_pct must be > 0")
    elif alert_type == AlertType.VOLUME_SPIKE:
        try:
            multiplier = float(rule.get("multiplier", 2.0))
        except (TypeError, ValueError) as exc:
            raise ValueError(f"invalid multiplier: {rule.get('multiplier')}") from exc
        if multiplier <= 0:

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Include a plain numeric 'price' value (number or simple numeric string) in every price_cross rule.
  2. Strip currency symbols and thousands separators, then parse, before constructing the rule from free-form text.
  3. Treat a missing price on a price_cross rule as a hard schema error at the extraction/prompt layer, not at validation.

Example fix

// before
rule = {"stock_code": "600519", "alert_type": "price_cross", "direction": "above", "price": "1,800"}
# ValueError: invalid price: 1,800

// after
rule = {"stock_code": "600519", "alert_type": "price_cross", "direction": "above", "price": 1800.0}
validate_event_alert_rule(rule)
Defensive patterns

Strategy: validation

Validate before calling

def parse_price(raw):
    try:
        return float(raw)
    except (TypeError, ValueError):
        cleaned = str(raw).replace(",", "").replace("$", "").strip()
        try:
            return float(cleaned)
        except (TypeError, ValueError):
            return None

Type guard

def has_parseable_price(rule: dict) -> bool:
    return parse_price(rule.get("price")) is not None

Try / catch

try:
    validate_event_alert_rule(rule)
except ValueError as e:
    if str(e).startswith("invalid price"):
        p = parse_price(rule.get("price"))
        if p is not None:
            rule["price"] = p
            validate_event_alert_rule(rule)
        else:
            raise

Prevention

When it happens

Trigger: validate_event_alert_rule with alert_type='price_cross' and price absent, None, '1800.5.1', '1,800', or [1800]. Plain numeric strings like '1800.0' are accepted.

Common situations: Locale-formatted numbers with thousands separators or currency symbols from LLM output or scraped text; rule templates that forgot to fill the price; a None placeholder from an optional form field passed through unchecked.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/6043ae843ce681e0. Report an issue: GitHub.