{"record":{"id":"8c2feaa8c65e83da","repo":"OpenBMB/ChatDev","slug":"register-bytes-expects-bytes-or-bytearray-data","errorCode":null,"errorMessage":"register_bytes expects bytes or bytearray data","messagePattern":"register_bytes expects bytes or bytearray data","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"utils/attachments.py","lineNumber":157,"sourceCode":"            self._persistent_ids.discard(attachment_id)\n        return record\n\n    def register_bytes(\n        self,\n        data: bytes | bytearray,\n        *,\n        kind: MessageBlockType = MessageBlockType.FILE,\n        mime_type: Optional[str] = None,\n        display_name: Optional[str] = None,\n        attachment_id: Optional[str] = None,\n        description: Optional[str] = None,\n        extra: Optional[Dict[str, Any]] = None,\n        persist: bool = True,\n    ) -> AttachmentRecord:\n        \"\"\"Register an in-memory payload as an attachment.\"\"\"\n\n        if not isinstance(data, (bytes, bytearray)):\n            raise TypeError(\"register_bytes expects bytes or bytearray data\")\n\n        attachment_id = attachment_id or uuid.uuid4().hex\n        filename = display_name or _default_filename_for_mime(mime_type)\n\n        target_dir = self.root / attachment_id\n        target_dir.mkdir(parents=True, exist_ok=True)\n        target_path = target_dir / filename\n        with target_path.open(\"wb\") as handle:\n            handle.write(bytes(data))\n\n        return self.register_file(\n            target_path,\n            kind=kind,\n            display_name=display_name or filename,\n            mime_type=mime_type,\n            attachment_id=attachment_id,\n            copy_file=False,\n            description=description,","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/OpenBMB/ChatDev/blob/4fb2db0ea90375ce1059f44fe03ffbd191a7a169/utils/attachments.py#L139-L175","documentation":"AttachmentManager.register_bytes raises TypeError when the data argument is not bytes or bytearray. It is a strict type guard against passing str, memoryview, file objects, or None where a raw payload is required.","triggerScenarios":"Calling register_bytes with a str (e.g. forgetting .encode()), a memoryview, a generator, or None; passing an already-decoded JSON object.","commonSituations":"Reading text from an API response and passing it directly; numpy/array buffer passed without tobytes(); optional data parameter defaulting to None on a missing upload.","solutions":["Encode strings: data.encode('utf-8')","Convert buffers: bytes(memoryview_obj) or arr.tobytes()","Guard the call: isinstance(data, (bytes, bytearray)) with a fallback"],"exampleFix":"# before\nrec = manager.register_bytes(text_payload, mime_type=\"text/plain\")\n# after\nrec = manager.register_bytes(text_payload.encode(\"utf-8\"), mime_type=\"text/plain\")","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, (bytes, bytearray)):\n    data = data.encode('utf-8') if isinstance(data, str) else bytes(data)","typeGuard":"def is_raw_bytes(d: object) -> bool:\n    return isinstance(d, (bytes, bytearray))","tryCatchPattern":"try:\n    rec = manager.register_bytes(data)\nexcept TypeError:\n    rec = manager.register_bytes(coerce_to_bytes(data))","preventionTips":["Encode str payloads with .encode()","Convert memoryview/array via bytes()/tobytes()","Assert type at API boundaries"],"tags":["attachments","type-error","bytes"],"backgroundTag":"wrong-argument-type","analyzedSha":"4fb2db0ea90375ce1059f44fe03ffbd191a7a169","analyzedAt":"2026-08-27T14:35:29.622Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}