Skip to content

Working with Strings and Binary Data

Valkey strings store sequences of bytes, which may include text, serialized objects, or binary arrays. GLIDE clients provide language-specific ways to handle both UTF-8 encoded strings and binary data when passing arguments to commands or receiving responses.

Python uses TEncodable as a union type that accepts either:

  1. str: for UTF-8 encoded strings
  2. bytes: for binary data
TEncodable = Union[str, bytes]

Commands accept both str and bytes arguments. When passing strings, they are automatically encoded to UTF-8. When you need binary data, pass bytes directly.

# Using strings (automatically encoded)
await client.set("key", "value")
result = await client.get("key") # Returns bytes: b'value'
# Using bytes directly
await client.set(b"key", b"binary_value")
result = await client.get(b"key") # Returns bytes: b'binary_value'
# Converting between str and bytes
key = "my_key"
await client.set(key.encode(), b"value") # str to bytes with .encode()
result = await client.get(key.encode())
decoded = result.decode() # bytes to str with .decode()

Most commands return bytes by default. Use .decode() to convert to str when needed.