{"record":{"id":"211748ea58526095","repo":"HKUDS/Vibe-Trading","slug":"weights-must-be-an-object-mapping-symbol-number","errorCode":null,"errorMessage":"weights must be an object mapping symbol → number","messagePattern":"weights must be an object mapping symbol → number","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"agent/src/tools/portfolio_risk_tool.py","lineNumber":139,"sourceCode":"            \"status\": \"ok\",\n            \"data\": report,\n            \"meta\": {\n                \"start_date\": start_date,\n                \"end_date\": end_date,\n                \"interval\": interval,\n                \"source\": source,\n                \"unresolved_symbols\": list(unresolved or []),\n            },\n        }\n        return json.dumps(envelope, ensure_ascii=False, indent=2, allow_nan=False)\n\n    # ------------------------------------------------------------------\n    @staticmethod\n    def _parse_weights(raw: Any, symbols: list[str]) -> dict[str, float]:\n        if raw is None:\n            return {sym: 1.0 / len(symbols) for sym in symbols}\n        if not isinstance(raw, Mapping):\n            raise ValueError(\"weights must be an object mapping symbol → number\")\n        unknown = [sym for sym in raw if sym not in symbols]\n        if unknown:\n            raise ValueError(f\"weights name symbols not in the basket: {sorted(unknown)}\")\n        missing = [sym for sym in symbols if sym not in raw]\n        if missing:\n            raise ValueError(f\"weights missing basket symbols: {sorted(missing)}\")\n        return {sym: raw[sym] for sym in symbols}\n\n    @staticmethod\n    def _parse_dates(start_raw: Any, end_raw: Any) -> tuple[str, str]:\n        end = (\n            datetime.strptime(end_raw, \"%Y-%m-%d\").date()\n            if isinstance(end_raw, str) and end_raw\n            else datetime.now(timezone.utc).date()\n        )\n        start = (\n            datetime.strptime(start_raw, \"%Y-%m-%d\").date()\n            if isinstance(start_raw, str) and start_raw","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/tools/portfolio_risk_tool.py#L121-L157","documentation":"_parse_weights accepts either None (equal weights) or a Mapping from symbol to number. Passing a list, string, or scalar raises 'weights must be an object mapping symbol → number'.","triggerScenarios":"weights=[0.5, 0.5], weights='equal', weights=0.5, or a JSON array of {sym,w} objects.","commonSituations":"LLMs emitting arrays because JSON arrays are more common than objects, or callers reusing vector weights from numpy code.","solutions":["Send an object keyed by ticker: {\"AAPL\": 0.6, \"MSFT\": 0.4}","Omit weights entirely for equal weighting","Convert list weights: dict(zip(symbols, weights))"],"exampleFix":"# before\nexecute(symbols=[\"AAPL\",\"MSFT\"], weights=[0.6, 0.4])\n# after\nexecute(symbols=[\"AAPL\",\"MSFT\"], weights={\"AAPL\": 0.6, \"MSFT\": 0.4})","handlingStrategy":"type-guard","validationCode":"if weights is not None and not isinstance(weights, dict):\n    weights = dict(zip(symbols, weights)) if isinstance(weights, list) else None\n# None -> equal weights","typeGuard":"from collections.abc import Mapping\ndef is_weight_map(w: object) -> bool:\n    return w is None or (isinstance(w, Mapping) and all(isinstance(k, str) for k in w))","tryCatchPattern":"try:\n    out = tool.execute(symbols=symbols, weights=weights)\nexcept ValueError as e:\n    if \"object mapping\" in str(e):\n        out = tool.execute(symbols=symbols, weights=dict(zip(symbols, weights)))","preventionTips":["Use JSON objects keyed by ticker for weights","Omit weights for equal weighting","Convert numpy/pandas vectors to dicts first"],"tags":["portfolio-risk","weights","input-validation"],"backgroundTag":"argument-type-validation-failed","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}