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
- Use one of the five supported region codes: cn, hk, us, jp, kr (case-insensitive, whitespace tolerated)
- Map upstream market identifiers to these codes before calling (e.g. 'SHA'/'Shanghai' -> 'cn')
- 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
- Normalize region codes (strip/lower) before calling
- Maintain a mapping from exchange/country identifiers to the five region codes
- Source region pickers from MARKET_LIGHT_REGIONS
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
- validation_error
- parameters must be an object
- {task_name} ranking fetch timeout
- invalid persisted market light snapshot for {normalized_regi
- validation_error
AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15).
Data as JSON: /api/errors/0ea161f19cb6dcdf.
Report an issue: GitHub.