OpenBMB/ChatDev · error · TypeError
register_bytes expects bytes or bytearray data
Error message
register_bytes expects bytes or bytearray data
What it means
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.
Source
Thrown at utils/attachments.py:157
self._persistent_ids.discard(attachment_id)
return record
def register_bytes(
self,
data: bytes | bytearray,
*,
kind: MessageBlockType = MessageBlockType.FILE,
mime_type: Optional[str] = None,
display_name: Optional[str] = None,
attachment_id: Optional[str] = None,
description: Optional[str] = None,
extra: Optional[Dict[str, Any]] = None,
persist: bool = True,
) -> AttachmentRecord:
"""Register an in-memory payload as an attachment."""
if not isinstance(data, (bytes, bytearray)):
raise TypeError("register_bytes expects bytes or bytearray data")
attachment_id = attachment_id or uuid.uuid4().hex
filename = display_name or _default_filename_for_mime(mime_type)
target_dir = self.root / attachment_id
target_dir.mkdir(parents=True, exist_ok=True)
target_path = target_dir / filename
with target_path.open("wb") as handle:
handle.write(bytes(data))
return self.register_file(
target_path,
kind=kind,
display_name=display_name or filename,
mime_type=mime_type,
attachment_id=attachment_id,
copy_file=False,
description=description,View on GitHub (pinned to 4fb2db0ea9)
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
Example fix
# before
rec = manager.register_bytes(text_payload, mime_type="text/plain")
# after
rec = manager.register_bytes(text_payload.encode("utf-8"), mime_type="text/plain") Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(data, (bytes, bytearray)):
data = data.encode('utf-8') if isinstance(data, str) else bytes(data) Type guard
def is_raw_bytes(d: object) -> bool:
return isinstance(d, (bytes, bytearray)) Try / catch
try:
rec = manager.register_bytes(data)
except TypeError:
rec = manager.register_bytes(coerce_to_bytes(data)) Prevention
- Encode str payloads with .encode()
- Convert memoryview/array via bytes()/tobytes()
- Assert type at API boundaries
When it happens
Trigger: Calling register_bytes with a str (e.g. forgetting .encode()), a memoryview, a generator, or None; passing an already-decoded JSON object.
Common situations: 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.
Related errors
- memory store {attachment.name} not found
- Failed to persist attachment '{attachment.name or attachment
- Workspace or node context missing for attachment persistence
- Attachment missing data for persistence
- Attachment source not found: {source}
AI-assisted analysis of OpenBMB/ChatDev@4fb2db0ea9 (2026-08-27).
Data as JSON: /api/errors/8c2feaa8c65e83da.
Report an issue: GitHub.