datawhalechina/hello-agents · warning · ValueError

site_id not found: {site_id}

Error message

site_id not found: {site_id}

What it means

Orchestrator.get_site() delegates to repo.get_site(site_id) and raises ValueError(f"site_id not found: {site_id}") when the repository returns nothing. It is the single source of truth for unknown-site errors in NetworkHealthReportAgent: build_report and the QA path call get_site first, so an invalid site_id surfaces here before any log/inventory/compliance queries run.

Source

Thrown at Co-creation-projects/monkeyhlj-NetworkHealthReportAgent/src/agents/orchestrator.py:29

from src.tools.data_repository import DataRepository


class NetworkHealthOrchestrator:
    def __init__(self) -> None:
        self.repo = DataRepository()
        self.log_agent = LogAnalysisAgent()
        self.device_agent = DeviceStatusAgent()
        self.user_agent = UserStatusAgent()
        self.report_agent = NetworkHealthReportAgent()
        self.qa_agent = SiteQAAgent()

    def list_sites(self) -> List[Dict]:
        return self.repo.list_sites()

    def get_site(self, site_id: str) -> Dict:
        site = self.repo.get_site(site_id)
        if not site:
            raise ValueError(f"site_id not found: {site_id}")
        return site

    def build_report(self, site_id: str, start: date, end: date) -> Dict:
        site = self.get_site(site_id)
        start_str = start.strftime("%Y-%m-%d")
        end_str = end.strftime("%Y-%m-%d")

        logs = self.repo.list_logs(site_id=site_id)
        inventory = self.repo.list_device_inventory(site_id=site_id)
        status_series = self.repo.list_device_status(site_id=site_id, start_date=start_str, end_date=end_str)
        compliance = self.repo.latest_terminal_compliance(site_id=site_id)

        log_result = self.log_agent.analyze(logs)
        device_result = self.device_agent.analyze(inventory=inventory, status_series=status_series)
        user_result = self.user_agent.analyze(terminal_row=compliance)

        return self.report_agent.synthesize(
            site=site,

View on GitHub (pinned to 606a07d341)

Solutions

  1. List valid ids first: orchestrator.list_sites() and use one of the returned site_id values verbatim.
  2. Check exact spelling/case/whitespace of the id you pass; strip and match case if the repo is case-sensitive.
  3. If the site should exist, verify the underlying data store loaded the sites file (path/env correct, JSON parsed).
  4. In callers, translate this ValueError into a 404 (the API layer in src/api/main.py already does this) or a user-facing message.

Example fix

# before
report = orchestrator.build_report(site_id="site-9", start=start, end=end)
# after
valid_ids = {s["site_id"] for s in orchestrator.list_sites()}
if "site-9" not in valid_ids:
    raise SystemExit(f"site-9 不存在,可选: {sorted(valid_ids)}")
report = orchestrator.build_report(site_id="site-9", start=start, end=end)
Defensive patterns

Strategy: validation

Validate before calling

valid_ids = {s["site_id"] for s in orchestrator.list_sites()}
if site_id not in valid_ids:
    raise ValueError(f"unknown site_id {site_id!r}; valid: {sorted(valid_ids)}")
site = orchestrator.get_site(site_id)

Type guard

def is_known_site(site_id: str) -> bool:
    return any(s["site_id"] == site_id for s in orchestrator.list_sites())

Try / catch

try:
    site = orchestrator.get_site(site_id)
except ValueError as e:
    # unknown id is a caller error — surface it, do not retry
    raise KeyError(str(e)) from e

Prevention

When it happens

Trigger: Calling build_report(site_id=...) or ask_global_question(site_id=...) with an id that is not in the data repository; typos or case mismatch ('SITE-01' vs 'site-01'); site removed from the dataset between listing the sites and requesting a report; frontend sending an empty or default site_id.

Common situations: Stale dropdown in the UI after the sites data file changed; scripts hardcoding a site_id from an older dataset; id normalization differences (leading zeros, uppercase) between producer and consumer; tests using fixtures with ids absent from the test repo.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/a760faf9b4a14c45. Report an issue: GitHub.