{"record":{"id":"ca3d66b3567c075c","repo":"langchain-ai/deepagents","slug":"external-event-must-be-valid-json-exc-msg","errorCode":null,"errorMessage":"External event must be valid JSON: {exc.msg}","messagePattern":"External event must be valid JSON: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/event_bus.py","lineNumber":378,"sourceCode":"    \"\"\"Decode one newline-delimited JSON external event.\n\n    Args:\n        data: Raw JSON line.\n        source: Transport-specific source label attached to the event.\n\n    Returns:\n        Parsed external event.\n\n    Raises:\n        TypeError: If the envelope is not a JSON object.\n        ValueError: If any envelope field is missing, of the wrong type, or\n            otherwise invalid.\n    \"\"\"\n    try:\n        raw = json.loads(data)\n    except json.JSONDecodeError as exc:\n        msg = f\"External event must be valid JSON: {exc.msg}\"\n        raise ValueError(msg) from exc\n    if not isinstance(raw, dict):\n        msg = \"External event must be a JSON object\"\n        raise TypeError(msg)\n\n    kind = raw.get(\"kind\")\n    if kind not in _VALID_KINDS:\n        msg = f\"External event kind must be one of {sorted(_VALID_KINDS)}; got {kind!r}\"\n        raise ValueError(msg)\n\n    payload = raw.get(\"payload\")\n    if not isinstance(payload, str) or not payload.strip():\n        msg = \"External event payload must be a non-empty string\"\n        raise ValueError(msg)\n\n    bypass = raw.get(\"bypass\", BypassTier.QUEUED.value)\n    try:\n        bypass_tier = BypassTier(bypass)\n    except ValueError as exc:","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/event_bus.py#L360-L396","documentation":"`decode_external_event` parses one newline-delimited JSON line from the external event transport. If `json.loads` fails, the malformed line is rejected with a `ValueError` that carries the JSON parser's message. This guards the event bus against corrupted, truncated, or non-JSON input written to the socket stream.","triggerScenarios":"A writer sends a line to the external event socket that is not valid JSON: truncated writes, concatenated objects without a newline separator, raw text/printf output, or binary bytes piped into the socket.","commonSituations":"Scripts `echo`-ing events with unquoted braces; a partial write from a crashed producer; piping command output directly into the event socket; an old producer version emitting a different serialization format.","solutions":["Fix the producer to emit one complete JSON object per line terminated by `\\n`.","Use `json.dumps` on the producer side instead of hand-built strings.","Validate the payload with a JSON linter/parser before writing to the socket.","Catch `ValueError` in the reader wrapper and skip/log the malformed line if resyncing is acceptable."],"exampleFix":"// before\necho kind: noop, payload: hi > /tmp/deepagents/events-1234.sock\n\n// after\necho '{\"kind\":\"noop\",\"payload\":\"hi\"}' | socat - UNIX-CONNECT:/tmp/deepagents/events-1234.sock\n","handlingStrategy":"validation","validationCode":"import json\n\ndef emit_event(sock_line: bytes) -> bool:\n    try:\n        value = json.loads(sock_line)\n    except json.JSONDecodeError:\n        return False\n    return isinstance(value, dict)  # also satisfies the object check\n","typeGuard":"def is_json_object(data: bytes) -> bool:\n    try:\n        return isinstance(json.loads(data), dict)\n    except (json.JSONDecodeError, UnicodeDecodeError):\n        return False\n","tryCatchPattern":"try:\n    event = decode_external_event(line, source=source)\nexcept ValueError as exc:\n    logger.warning(\"dropping malformed external event line: %s\", exc)\n    return None\n","preventionTips":["Always serialize with `json.dumps` and terminate each event with a newline.","Never pipe raw command output into the event socket.","Test producer output with a JSON parser before deploying.","Keep one JSON object per line (NDJSON)."],"tags":["json","ipc","event-bus","validation"],"backgroundTag":"invalid-json-payload","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}