# Redis and binary blobs

**URL:** <https://swi-prolog.discourse.group/t/redis-and-binary-blobs/7453>\
**Category:** Data Structure\
**Tags:** redis\
**Created:** [May 15, 2024, 8:24pm UTC](https://swi-prolog.discourse.group/t/redis-and-binary-blobs/7453 "2024-05-15T20:24:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![free-variation](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/free-variation/32/3363_2.png) [@free-variation](https://swi-prolog.discourse.group/u/free-variation)\
**Post date:** [May 15, 2024, 8:24pm UTC](https://swi-prolog.discourse.group/t/redis-and-binary-blobs/7453/1 "2024-05-15T20:24:56Z")

</div>

Hi,

I’m working with vector searches in Redis. I’ve been able to use the Redis library and Janus to create document vectors (using sentence\_transformers) in Redis as JSON objects. And I’m able to create a Redis index.

When it comes to searching, Redis requires that the query vector be provided as a binary blob. In Python it looks like this:  
`res = r.ft("doc_idx").search(q, query_params={"vec": model.encode(sentence).astype(np.float32).tobytes()})`

Note `tobytes()` which returns a Python `bytes` object.

Is there an equivalent in SWI Prolog? Alternatively, if I have a ref to a Python blob (via Janus, obtained by `py_call` with `py_object(true)` option), I’m not clear on how I can serialize that out to Redis, in Prolog. If I don’t use `py_object(true)`, then I get a list of integers (the bytes) back from Janus. Can that be blobbified somehow?

Thanks.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 16, 2024, 7:54am UTC](https://swi-prolog.discourse.group/t/redis-and-binary-blobs/7453/2 "2024-05-16T07:54:14Z")

</div>

As is, the SWI-Prolog Redis interface does not support Redis binary blobs. I guess we could add that by supporting e.g., `Data as blob` in the same context as where we now support `Term as prolog`. Data would then be bytes, encoded as list of integers or as an 8-bit string. That should be fairly trivial to add. If you provide a PR, I’m happy to accept that.

Second problem is how to do binary encoding. There are many options for binary encoding data. Some of them are implemented by the protobufs package. If you can get a list of bytes from Python, the above should be enough. If this is some specific encoding for Redis, we could consider adding a support predicate to the redis package.
