ZhuLinsen/daily_stock_analysis · warning · ValueError

unsupported alert_type: {alert_type}

Error message

unsupported alert_type: {alert_type}

What it means

While deserializing stored monitor state (from_json-ish loop around events.py:403-421), each entry's alert_type string is matched against the three implemented values; an unknown value falls to the else branch and raises ValueError('unsupported alert_type: ...'). The surrounding loop catches Exception per entry and logs 'Skip invalid rule #N', so one bad rule does not kill the whole monitor load.

Source

Thrown at src/agent/events.py:421

                if alert_type == AlertType.PRICE_CROSS.value:
                    rule = PriceAlert(
                        stock_code=stock_code,
                        direction=entry.get("direction", "above").lower(),
                        price=float(entry.get("price", 0.0)),
                    )
                elif alert_type == AlertType.PRICE_CHANGE_PERCENT.value:
                    rule = PriceChangeAlert(
                        stock_code=stock_code,
                        direction=entry.get("direction", "up").lower(),
                        change_pct=float(entry["change_pct"]),
                    )
                elif alert_type == AlertType.VOLUME_SPIKE.value:
                    rule = VolumeAlert(
                        stock_code=stock_code,
                        multiplier=float(entry.get("multiplier", 2.0)),
                    )
                else:
                    raise ValueError(f"unsupported alert_type: {alert_type}")
                rule.status = AlertStatus(entry.get("status", "active"))
                raw_created = entry.get("created_at")
                try:
                    rule.created_at = float(raw_created) if raw_created is not None else time.time()
                except (TypeError, ValueError):
                    rule.created_at = time.time()
                rule.ttl_hours = float(entry.get("ttl_hours", 24.0))
                monitor.add_alert(rule)
            except Exception as exc:
                logger.warning("[EventMonitor] Skip invalid rule #%d: %s", index, exc)
        return monitor


def parse_event_alert_rules(raw_rules: Any) -> List[Dict[str, Any]]:
    """Parse event alert rules from config JSON or already-loaded objects."""
    if raw_rules is None:
        return []

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Fix or remove the offending entry in the stored rules JSON; use lowercase values: price_cross, price_change_percent, volume_spike
  2. If rules come from another system, normalize alert_type to lowercase values before saving
  3. When adding new alert types, extend both the loop here and _RUNTIME_SUPPORTED_ALERT_TYPES, then migrate stored data
  4. Check logs for '[EventMonitor] Skip invalid rule #N' to find which index was dropped

Example fix

// before
{"rules": [{"stock_code": "AAPL", "alert_type": "PRICE_CROSS", "price": 100}]}

// after
{"rules": [{"stock_code": "AAPL", "alert_type": "price_cross", "price": 100, "direction": "above"}]}
Defensive patterns

Strategy: validation

Validate before calling

SUPPORTED = {"price_cross", "price_change_percent", "volume_spike"}

rules = [r for r in stored_rules if r.get("alert_type") in SUPPORTED]
# log dropped ones explicitly

Type guard

def is_serializable_alert_rule(entry: dict) -> bool:
    return isinstance(entry, dict) and entry.get("alert_type") in {
        "price_cross", "price_change_percent", "volume_spike"
    }

Prevention

When it happens

Trigger: Restoring an EventMonitor from a saved rules array containing an alert_type string outside {price_cross, price_change_percent, volume_spike} — e.g. a typo, a value from a newer/older schema, or an unimplemented enum member stored earlier.

Common situations: Migrating between versions where alert types were added; hand-edited rule files; a producer writing alert_type as the enum's Python name ('PRICE_CROSS') instead of its value ('price_cross').

Related errors


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