ZhuLinsen/daily_stock_analysis · error · ValueError

validation_error

validation_error

Error message

market target must be one of cn, hk, us, jp, kr: {region}

What it means

ValueError(f'market target must be one of cn, hk, us, jp, kr: {region}') with code validation_error is raised by normalize_market_region (src/services/market_light_service.py:24) when the requested market region, after strip().lower(), is not in MARKET_LIGHT_REGIONS = {'cn','hk','us','jp','kr'}. This is the entry-point validation for building/loading Market Light snapshots.

Source

Thrown at src/services/market_light_service.py:28

from sqlalchemy import desc

from src.core.market_review import MARKET_REVIEW_HISTORY_CODE, MARKET_REVIEW_REPORT_TYPE
from src.market_analyzer import MarketAnalyzer
from src.schemas.market_light import MarketLightSnapshot
from src.storage import AnalysisHistory, DatabaseManager


logger = logging.getLogger(__name__)

MARKET_LIGHT_REGIONS = frozenset({"cn", "hk", "us", "jp", "kr"})
MARKET_LIGHT_ALERT_REGIONS = frozenset({"cn", "hk", "us"})
MARKET_LIGHT_HISTORY_BATCH_SIZE = 100


def normalize_market_region(region: str) -> str:
    value = str(region or "").strip().lower()
    if value not in MARKET_LIGHT_REGIONS:
        raise ValueError(f"market target must be one of cn, hk, us, jp, kr: {region}")
    return value


def normalize_market_alert_region(region: str) -> str:
    value = str(region or "").strip().lower()
    if value not in MARKET_LIGHT_ALERT_REGIONS:
        raise ValueError(f"market alert target must be one of cn, hk, us: {region}")
    return value


def build_current_snapshot(region: str) -> Dict[str, Any]:
    """Build the current structured Market Light snapshot without LLM review."""

    normalized_region = normalize_market_region(region)
    analyzer = MarketAnalyzer(region=normalized_region)
    overview = analyzer.get_market_overview()
    return analyzer.build_market_light_snapshot(overview)

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Use one of the five supported region codes: cn, hk, us, jp, kr (case-insensitive, whitespace tolerated)
  2. Map upstream market identifiers to these codes before calling (e.g. 'SHA'/'Shanghai' -> 'cn')
  3. If you need another region, extend MARKET_LIGHT_REGIONS and the analyzer support deliberately — not by bypassing the check

Example fix

# before
build_current_snapshot("china")

# after
build_current_snapshot("cn")
Defensive patterns

Strategy: type-guard

Validate before calling

from src.services.market_light_service import MARKET_LIGHT_REGIONS

region = region.strip().lower()
if region not in MARKET_LIGHT_REGIONS:
    raise ValueError(f"region must be one of {sorted(MARKET_LIGHT_REGIONS)}")

Type guard

from src.services.market_light_service import MARKET_LIGHT_REGIONS

def is_supported_market_region(region: str) -> bool:
    return isinstance(region, str) and region.strip().lower() in MARKET_LIGHT_REGIONS

Try / catch

try:
    snapshot = build_current_snapshot(region)
except ValueError as exc:
    if "market target must be one of" in str(exc):
        return api_error(400, "supported regions: cn, hk, us, jp, kr")

Prevention

When it happens

Trigger: Calling build_current_snapshot or snapshot-loading helpers with region values like 'a', 'CN ' (fine after trim), 'sh', 'china', 'gb', or an empty string/None (str(None) -> 'none').

Common situations: Passing stock-exchange codes instead of region codes, localized country names ('日本'), or regions added in a newer version being sent to an older deployment.

Related errors


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