ZhuLinsen/daily_stock_analysis · error · ValueError

invalid persisted market light snapshot for {normalized_regi

Error message

invalid persisted market light snapshot for {normalized_region} on {best_trade_date}

What it means

ValueError(f'invalid persisted market light snapshot for {normalized_region} on {best_trade_date}') is raised by the snapshot-history loader in src/services/market_light_service.py when at least one persisted snapshot for the best available trade date exists, but every candidate failed to deserialize/validate (the stored JSON does not match the expected schema), and no valid snapshot for that date survived. The original deserialization error is chained via 'from invalid_target_error'.

Source

Thrown at src/services/market_light_service.py:114

                candidate = MarketLightSnapshot.model_validate(snapshot).model_dump()
            except Exception as exc:
                logger.warning(
                    "invalid persisted market light snapshot: row_id=%s region=%s trade_date=%s error=%s",
                    getattr(row, "id", "?"),
                    normalized_region,
                    trade_date,
                    exc,
                )
                if best_snapshot is None:
                    invalid_target_error = exc
                continue
            if best_snapshot is None:
                best_snapshot = candidate

    if best_snapshot is not None:
        return best_snapshot
    if best_trade_date is not None and invalid_target_error is not None:
        raise ValueError(
            f"invalid persisted market light snapshot for {normalized_region} on {best_trade_date}"
        ) from invalid_target_error
    return None


def _extract_region_snapshot(raw_context_snapshot: Any, region: str) -> Optional[Dict[str, Any]]:
    if not raw_context_snapshot:
        return None
    try:
        payload = (
            json.loads(raw_context_snapshot)
            if isinstance(raw_context_snapshot, str)
            else raw_context_snapshot
        )
    except (TypeError, json.JSONDecodeError):
        return None
    if not isinstance(payload, dict):
        return None

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Inspect the persisted payload for that region/date: select the raw context_snapshot JSON and try parsing it against the current MarketLightSnapshot schema
  2. If written by an old schema, run the migration/backfill that rewrites or re-derives snapshots, or delete the bad rows so the loader treats the date as absent
  3. Re-generate the snapshot for that trade date via the normal analysis flow and persist it
  4. Wrap history loads in try/except at the caller to degrade to 'no history' rather than failing the whole market light view

Example fix

# before: corrupt row for 2026-08-13 breaks every load
# (persisted context_snapshot is truncated JSON)

# after: remove/repair the bad row, then reload
-- DELETE FROM market_light_history
-- WHERE region = 'cn' AND trade_date = '2026-08-13';
# next scheduled run re-persists a valid snapshot
Defensive patterns

Strategy: try-catch

Validate before calling

from src.schemas.market_light import MarketLightSnapshot

def is_valid_persisted_snapshot(raw_json: str) -> bool:
    try:
        MarketLightSnapshot.model_validate_json(raw_json)
        return True
    except Exception:
        return False

Try / catch

try:
    snapshot = load_latest_snapshot(region)
except ValueError as exc:
    if "invalid persisted market light snapshot" in str(exc):
        logger.error("corrupt snapshot for %s; rebuilding", region)
        snapshot = build_current_snapshot(region)  # regenerate fresh

Prevention

When it happens

Trigger: Loading market light history where the stored context_snapshot JSON for the target trade date is corrupt, truncated, or was written by an older schema version, and no alternate valid snapshot for that date exists; only then does the loader escalate from 'skip bad snapshot' to raising.

Common situations: Schema migrations after upgrading the Market Light snapshot model, partially written rows from a crash mid-save, encoding corruption in the persistence layer, or manual DB edits breaking the JSON structure.

Related errors


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