{"record":{"id":"a3496ce8dd582e0e","repo":"MemPalace/mempalace","slug":"metadata-is-not-valid-json-exc","errorCode":null,"errorMessage":"--metadata is not valid JSON: {exc}","messagePattern":"--metadata is not valid JSON: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mempalace/cli.py","lineNumber":1512,"sourceCode":"        raise ValueError(\"pass inline text or a file, not both\")\n    if file_arg is not None:\n        if file_arg == \"-\":\n            return _read_stdin_exact()\n        return Path(os.path.expanduser(file_arg)).read_bytes().decode(\"utf-8\")\n    if inline is not None:\n        return inline\n    return default\n\n\ndef _parse_metadata_arg(raw):\n    import json\n\n    if raw is None:\n        return None\n    try:\n        value = json.loads(raw)\n    except ValueError as exc:\n        raise ValueError(f\"--metadata is not valid JSON: {exc}\") from None\n    if not isinstance(value, dict):\n        raise ValueError(\"--metadata must be a JSON object\")\n    return value\n\n\ndef _print_event_line(event):\n    target = event[\"to_agent\"] or \"*\"\n    corr = f\" corr={event['correlation_id']}\" if event[\"correlation_id\"] else \"\"\n    status = f\" [{event['status']}]\" if event[\"status\"] else \"\"\n    arts = f\" artifacts={len(event['artifact_ids'])}\" if event[\"artifact_ids\"] else \"\"\n    body = event[\"body\"].replace(\"\\n\", \" \")\n    if len(body) > 80:\n        body = body[:77] + \"...\"\n    body = f\" :: {body}\" if body else \"\"\n    print(\n        f\"  {event['id']}  {event['created_at']}  {event['type']}  \"\n        f\"{event['stream']}/{event['room']}  {event['from_agent']}->{target}\"\n        f\"{status}{corr}{arts}{body}\"","sourceCodeStart":1494,"sourceCodeEnd":1530,"githubUrl":"https://github.com/MemPalace/mempalace/blob/06cb6987f02610784fefbad4b2bd5d026d164ba6/mempalace/cli.py#L1494-L1530","documentation":"Raised by _parse_metadata_arg() in the CLI when the --metadata option value cannot be parsed by json.loads. The metadata option expects a raw JSON document on the command line; any JSON syntax error (trailing comma, unquoted keys, smart quotes from shell paste, unterminated string) triggers this ValueError with the underlying parser message appended.","triggerScenarios":"Passing --metadata with malformed JSON, e.g. `--metadata \"{'a':1}\"` (single quotes), `--metadata \"{a:1}\"` (unquoted key), or a value mangled by shell quoting/escaping so the string reaching the CLI is not valid JSON.","commonSituations":"Single-quoted Python-style dicts pasted into shell; shell interpolation stripping inner quotes; smart quotes introduced by editors/terminals; truncated JSON from variable expansion.","solutions":["Quote the JSON correctly for your shell, using single quotes outside and double quotes inside: --metadata '{\"source\":\"cli\"}'","Validate the string with `echo \"$META\" | python3 -m json.tool` or jq before running the command","If the metadata lives in a variable, build it with jq -n or a here-doc to guarantee valid JSON"],"exampleFix":"# before\nmempalace event add ... --metadata \"{'source':'cli'}\"\n\n# after\nmempalace event add ... --metadata '{\"source\":\"cli\"}'","handlingStrategy":"validation","validationCode":"import json\n\n# Validate before passing --metadata\ntry:\n    json.loads(metadata_raw)\nexcept ValueError as exc:\n    raise SystemExit(f\"bad --metadata JSON: {exc}\")","typeGuard":"def is_valid_metadata_json(raw: str) -> bool:\n    try:\n        json.loads(raw)\n        return True\n    except (ValueError, TypeError):\n        return False","tryCatchPattern":"try:\n    meta = _parse_metadata_arg(raw)\nexcept ValueError as exc:\n    if \"not valid JSON\" in str(exc):\n        meta = _parse_metadata_arg(json.dumps(eval_shorthand(raw)))  # or re-prompt user\n    else:\n        raise","preventionTips":["Build metadata with jq -n or json.dumps, never string concatenation","Shell-quote with single quotes outside, double quotes inside the JSON","Pipe draft JSON through `jq .` before pasting into commands"],"tags":["cli","json","validation","mempalace"],"backgroundTag":null,"analyzedSha":"06cb6987f02610784fefbad4b2bd5d026d164ba6","analyzedAt":"2026-08-15T03:03:36.213Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}