How can I append multiple image URLs to an Airtable cell when responses are processed in bulk with delayed updates?

I’m building a WhatsApp bot that lets users send several images at once, and I use an API to manage the responses. When images are received, they all come in together and it takes a few seconds for the Airtable cell to update. Right now, my code only saves the last image URL instead of adding the new ones to the list already stored. Here’s a sample of my updated code:

@app.route('/image_handler', methods=['POST', 'GET'])
def process_images():
    data = request.json
    user_id = data['userId']
    
    if data['type'] == 'image':
        new_url = data.get('imageData')
        record_id, current_urls = db.fetch_image_urls(user_id)
        
        if not current_urls:
            current_urls = ''
        
        url_list = current_urls.split('\n')
        url_list.append(new_url)
        updated_urls = '\n'.join(url_list)
        
        db.save_image_urls(record_id, updated_urls)
    
    return data

How can I modify this so every image URL is combined correctly in the Airtable cell despite the update delay?

Having worked extensively with Airtable and bulk image processing, I can suggest a robust solution. Consider implementing a batching mechanism. Instead of updating Airtable for each image, accumulate the URLs in memory or a temporary storage. Set a timer or a count threshold. When either is reached, perform a single Airtable update with all accumulated URLs.

This approach significantly reduces API calls and mitigates race conditions. You’ll need to modify your code to maintain a local buffer of URLs, possibly using a global variable or a more sophisticated caching system depending on your application’s architecture. Remember to implement proper error handling and retries for network issues.

Additionally, ensure you’re respecting Airtable’s rate limits and field size constraints. If dealing with a high volume of images, you might need to split across multiple fields or records.

I’ve faced a similar challenge when working with bulk image uploads and Airtable. One effective solution I found is to implement a queue system for processing the image URLs. Instead of trying to update Airtable immediately, you can store the incoming URLs in a temporary queue such as Redis or even in memory, depending on your scale.

A rough idea to modify your code: when images are received, add their URLs to a queue rather than directly updating Airtable. Then, use a background job that periodically processes the queued URLs by fetching the current URLs from Airtable, appending all new ones, and performing a single update. This approach mitigates update delays and minimizes API calls. Also consider possible edge cases like duplicate URLs or maximum field size, and include error handling and retry logic where necessary.

yo, i’ve been there. what about using a temporary list to store urls before updating airtable? like this:

temp_urls =

def process_images():
# … existing code …
temp_urls.append(new_url)
if len(temp_urls) >= 5 or some_time_condition:
update_airtable(temp_urls)
temp_urls.clear()

this way u batch updates and avoid race conditions. just my 2 cents!