Skip to content

⚡ Bolt: [performance improvement] Memoize Printify variants API call#45

Open
merg357 wants to merge 1 commit into
mainfrom
bolt/memoize-get-variants-12171247976811877567
Open

⚡ Bolt: [performance improvement] Memoize Printify variants API call#45
merg357 wants to merge 1 commit into
mainfrom
bolt/memoize-get-variants-12171247976811877567

Conversation

@merg357

@merg357 merg357 commented May 23, 2026

Copy link
Copy Markdown
Owner

💡 What: Applied Python's @functools.lru_cache(maxsize=None) to get_variants in create_merch.py, merch_factory.py, and printify_manager.py. The return type of get_variants was updated from a list to an immutable tuple.

🎯 Why: During bulk product creation, the applications repeatedly queried Printify to get variants for the same blueprints and providers over and over (N+1 queries). By memoizing this result, redundant requests are bypassed.

📊 Impact: Significant reduction in API calls and network latency during bulk operations.

🔬 Measurement: Review logging outputs during bulk creation to observe lack of latency on subsequent variant fetches.


PR created automatically by Jules for task 12171247976811877567 started by @merg357

Replaced repetitive identical requests to Printify blueprint variants
across creation loops by wrapping the function with `functools.lru_cache`.
Mutations of responses are prevented by changing return types from lists
to tuples, which natively serialize to JSON. Added documentation comments.

Co-authored-by: merg357 <221854052+merg357@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings May 23, 2026 09:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves bulk Printify product-creation performance by memoizing repeated blueprint/provider variant lookups, avoiding N+1 network calls to the Printify variants.json endpoint.

Changes:

  • Added @functools.lru_cache(maxsize=None) to get_variants in multiple scripts to reuse prior variant fetch results.
  • Changed get_variants to return immutable tuples (and updated related type hints/signatures) to prevent cached-value mutation.
  • Added a short internal note in .jules/bolt.md documenting the “cache mutable vs immutable” lesson.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.

File Description
printify_manager.py Memoizes variant lookup and returns tuples to avoid repeated API calls during CLI-driven bulk creation.
merch_factory.py Memoizes variant lookup and updates product creation signature to accept tuple variant IDs.
create_merch.py Memoizes variant lookup and updates typing to use Tuple[int, ...] for variant IDs.
.jules/bolt.md Documents the rationale for caching immutable sequences with lru_cache.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread printify_manager.py
def get_variants(blueprint_id: int, provider_id: int) -> list:
# Why: Prevents repeated network calls to fetch blueprint variants for the same blueprint/provider in iterative product creation loops (N+1 queries).
# Impact: Significant reduction in API calls and network latency during bulk operations.
# Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved.
Comment thread printify_manager.py
# Impact: Significant reduction in API calls and network latency during bulk operations.
# Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved.
@functools.lru_cache(maxsize=None)
def get_variants(blueprint_id: int, provider_id: int) -> tuple:
Comment thread merch_factory.py
def get_variants(blueprint_id: int, provider_id: int) -> list:
# Why: Prevents repeated network calls to fetch blueprint variants for the same blueprint/provider in iterative product creation loops (N+1 queries).
# Impact: Significant reduction in API calls and network latency during bulk operations.
# Safety: Output type is cast to a tuple to ensure immutability, preventing unintended downstream modifications. Original slicing and fallback logic are strictly preserved.
Comment thread merch_factory.py
Comment on lines 48 to +51
url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json"
resp = requests.get(url, headers=HEADERS)
resp.raise_for_status()
return [v["id"] for v in resp.json().get("variants", [])[:4]]
return tuple([v["id"] for v in resp.json().get("variants", [])[:4]])
Comment thread merch_factory.py
Comment on lines +47 to 55
def get_variants(blueprint_id: int, provider_id: int) -> tuple:
url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{provider_id}/variants.json"
resp = requests.get(url, headers=HEADERS)
resp.raise_for_status()
return [v["id"] for v in resp.json().get("variants", [])[:4]]
return tuple([v["id"] for v in resp.json().get("variants", [])[:4]])

def create_product(title: str, blueprint_id: int, provider_id: int,
variant_ids: list, image_id: str) -> dict:
variant_ids: tuple, image_id: str) -> dict:
url = f"{BASE_URL}/shops/{SHOP_ID}/products.json"
Comment thread create_merch.py
Comment on lines +61 to 64
def get_variants(blueprint_id: int, print_provider_id: int = 29) -> Tuple[int, ...]:
url = f"{BASE_URL}/catalog/blueprints/{blueprint_id}/print_providers/{print_provider_id}/variants.json"
resp = requests.get(url, headers=get_headers())
resp.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants