Sync modeling WebSocket cannot receive BSON export frames, and send() crashes on commands containing bytes
Context: API Makeathon participant. Found while reviewing the sync modeling websocket wrapper.
1. Cannot receive BSON export frames
The sync ModelingCommandsWs.__iter__ and .recv() pass every received frame to WebSocketResponse.model_validate_json (kittycad/__init__.py:16905). But exports arrive as binary/BSON, not text/JSON: RawFile (kittycad/models/raw_file.py) documents this, and the wrapper supports sending BSON but has no BSON receive branch.
A modeling export therefore succeeds on the server, but when the export frame arrives the client raises a JSON/Pydantic decoding error, so the exported files are inaccessible through the wrapper.
2. send() crashes on byte payloads
send() does self.ws.send(json.dumps(data.model_dump(exclude_none=True))) (around __init__.py:16915-16918). ImportFile.data is bytes (kittycad/models/import_file.py), and json.dumps raises TypeError: Object of type bytes is not JSON serializable, so sending an import_files command crashes with an unrelated-looking error instead of base64-encoding (as model_dump_json would) or directing the user to send_binary().
Concrete failure
Exporting geometry over the sync websocket raises a decode error when the result frame arrives; sending an import_files command raises a bytes-serialization TypeError.
Verify
BSON receive: json.loads(b"\x05\x00\x00\x00\x00") (the canonical empty BSON document) is rejected as JSON. Send: build an import_files command with an ImportFile whose data is bytes and call send(): it raises the TypeError.
Suggested fix
Branch on frame type in __iter__/recv() and decode binary frames as BSON, and have send() use model_dump_json (which base64-encodes bytes) or route byte-bearing commands through send_binary().
Environment
Reviewed against the current main of KittyCAD/kittycad.py.
Sync modeling WebSocket cannot receive BSON export frames, and
send()crashes on commands containing bytesContext: API Makeathon participant. Found while reviewing the sync modeling websocket wrapper.
1. Cannot receive BSON export frames
The sync
ModelingCommandsWs.__iter__and.recv()pass every received frame toWebSocketResponse.model_validate_json(kittycad/__init__.py:16905). But exports arrive as binary/BSON, not text/JSON:RawFile(kittycad/models/raw_file.py) documents this, and the wrapper supports sending BSON but has no BSON receive branch.A modeling export therefore succeeds on the server, but when the export frame arrives the client raises a JSON/Pydantic decoding error, so the exported files are inaccessible through the wrapper.
2.
send()crashes on byte payloadssend()doesself.ws.send(json.dumps(data.model_dump(exclude_none=True)))(around__init__.py:16915-16918).ImportFile.dataisbytes(kittycad/models/import_file.py), andjson.dumpsraisesTypeError: Object of type bytes is not JSON serializable, so sending animport_filescommand crashes with an unrelated-looking error instead of base64-encoding (asmodel_dump_jsonwould) or directing the user tosend_binary().Concrete failure
Exporting geometry over the sync websocket raises a decode error when the result frame arrives; sending an
import_filescommand raises a bytes-serializationTypeError.Verify
BSON receive:
json.loads(b"\x05\x00\x00\x00\x00")(the canonical empty BSON document) is rejected as JSON. Send: build animport_filescommand with anImportFilewhosedatais bytes and callsend(): it raises theTypeError.Suggested fix
Branch on frame type in
__iter__/recv()and decode binary frames as BSON, and havesend()usemodel_dump_json(which base64-encodes bytes) or route byte-bearing commands throughsend_binary().Environment
Reviewed against the current
mainof KittyCAD/kittycad.py.