Home page Forums Technical Support, Bugs and Fixes Not pulling data??! Reply To: Not pulling data??!

#8836
Matt WestfallMatt Westfall
Participant

    Gemini says this, but I don’t want to hacky up code and it get replaced on update:

    [code]
    1. How to Fix the “Write” Error in red.py
    When writing time-series data using RedisTimeSeries (usually via the TS.ADD or TS.INCRBY commands), Redis will often auto-create the key if it doesn’t exist. However, it will throw the TSDB: the key does not exist error if:

    You are trying to apply a rule (TS.CREATERULE) to a key that isn’t created yet.

    You are trying to alter a key (TS.ALTER) before it has data.

    You are using a specific client wrapper or parameters that disable auto-creation.

    The Fix: Wrap your write operation in a try/except block. If it fails because the key is missing, explicitly create the key (TS.CREATE) and then retry the write.

    Python
    import redis
    from redis.exceptions import ResponseError

    # Assuming ‘r’ is your Redis connection
    key_name = “my_metric_name”
    timestamp = “*” # * means current time in RedisTimeSeries
    value = 42

    try:
    # Attempt to write the data
    r.ts().add(key_name, timestamp, value)
    except ResponseError as e:
    if “TSDB: the key does not exist” in str(e):
    # 1. Create the key explicitly (you can add retention, labels, etc. here)
    r.ts().create(key_name)
    # 2. Retry the write
    r.ts().add(key_name, timestamp, value)
    else:
    # If it’s a different error, raise it
    raise e

    [/code]