·3 min read

Announcing Upstash Redis Python SDK v1.0.0

Yusuf KimirtiYusuf KimirtiSofware Engineer @Upstash

Today we are releasing v1.0.0 version of the upstash-redis python package. It is now available on GitHub and PyPi.

The package is a connectionless HTTP-based client for Upstash Redis, designed to be usable in serverless environments like AWS Lambda, Google Cloud Functions or any environment where HTTP is preferred over TCP.

What's new?

With the new versions the package comes with python docstrings for every command with example usages in them.

img.png

You can also find the documentation and examples for every command in the upstash redis python docs.

Quick Start

Install the package

pip install upstash-redis
pip install upstash-redis

Create a Redis client

from upstash_redis import UpstashRedis
 
redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")
 
redis.set("key", "value")
from upstash_redis import UpstashRedis
 
redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")
 
redis.set("key", "value")

You can also use the environment variables UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN instead of passing them to the constructor.

from upstash_redis import UpstashRedis
 
redis = Redis.from_env()
 
redis.set("key", "value")
from upstash_redis import UpstashRedis
 
redis = Redis.from_env()
 
redis.set("key", "value")

The sdk also supports async functions with asyncio and aiohttp packages.

import asyncio
from upstash_redis.asyncio import Redis
 
async def main():
    redis = Redis.from_env()
 
    await redis.set("key", "value")
 
asyncio.run(main())
import asyncio
from upstash_redis.asyncio import Redis
 
async def main():
    redis = Redis.from_env()
 
    await redis.set("key", "value")
 
asyncio.run(main())

If you are in a serverless environment that allows it, it's recommended to initialise the client outside the request handler to be reused while your function is still hot.

Value type change in some commands

Some commands like set or hset accepted Any as a value type. The value was then converted to a string using json.dumps. This leaded to some confusion in some commands.

This is now change to a new type, ValueT that only accepts str, int, float and bool.

ValueT = Union[str, int, float, bool]
 
def set(
    self,
    key: str,
    value: ValueT,
    ...
) -> Optional[str]: ...
ValueT = Union[str, int, float, bool]
 
def set(
    self,
    key: str,
    value: ValueT,
    ...
) -> Optional[str]: ...

The sdk still supports the old behaviour but the typing is changed.

# Works, but gives a type error
redis.set("key", {"foo": "bar"})
 
# Works
redis.set("key", json.dumps({"foo": "bar"}))
# Works, but gives a type error
redis.set("key", {"foo": "bar"})
 
# Works
redis.set("key", json.dumps({"foo": "bar"}))

Geo commands returning GeoSearchResult instead of Dict

The geosearch and georadius commands now return a GeoSearchResult object instead of a dictionary which is more convenient to use.

Set commands returning List instead of Set

Changed return types of sdiff, sunion, sinter, and smembers method to List from Set. This eliminates extra set allocation for users doing iteration over the result. If they want, they can return the list into set themselves.

Conclusion

We hope you enjoy the new version of the package. We are planning on adding pipeline and json commands support to the python sdk.

You can also check out our python ratelimiting library or our other blog posts about python.

If you have any questions or feedback, please reach out to us on X or Discord.