BERT-Based Sequence Models for Contextual Product Recommendations

instacart · E-commerce · 2024 · source Agentic: no · promotional bias: low · source-backed · extracted with prompt 0.5.4 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: Contextual product recommendation retrieval

Instacart needed one contextual retrieval system that could power multiple ads and organic recommendation surfaces using in-session shopping behavior instead of maintaining many ad hoc retrieval systems.

Constraints:

Architecture

Instacart built a centralized contextual retrieval layer that uses a BERT-like sequence model trained with masked language modeling over historical product-ID sequences. At inference time, the transformer encodes a session sequence and predicts probabilities over product IDs, returning top-K candidates for downstream ranking and re-ranking across search, item details, cart, pre-checkout, post-checkout, ads, and organic surfaces. The preliminary version uses product-ID sequences only, with future work on ANN scaling, canonical identity, popularity bias, and richer context.

Diagram confidence: reconstructed (reconstructed from prose).

flowchart LR
  session-events[Session events] --> product-sequence-encoder[Product sequence encoder]
  historical-interaction-data[Historical interaction data] --> mlm-training-pipeline[MLM training pipeline]
  mlm-training-pipeline --> product-sequence-encoder
  product-sequence-encoder --> next-product-predictor[Next-product predictor]
  next-product-predictor --> unified-retrieval-layer[Unified retrieval layer]
  unified-retrieval-layer --> downstream-rankers[Downstream rankers]
  downstream-rankers --> recommendation-surfaces[Recommendation surfaces]
  historical-interaction-data --> recall-k-evaluator[Recall@K evaluator]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Session events Ordered cart adds and product views within a shopping session. Product sequence encoder
Product sequence encoder BERT-like transformer that encodes product-ID sequences. Session events, Next-product predictor
MLM training pipeline Trains the sequence model with masked language modeling on product-ID sequences. Product sequence encoder, Historical interaction data
Historical interaction data Session sequences used for training and offline evaluation. MLM training pipeline, Recall@K evaluator
Next-product predictor Computes product probabilities and top-K retrieval candidates. Product sequence encoder, Unified retrieval layer
Unified retrieval layer Shared candidate-generation service for ads and organic recommendation surfaces. Next-product predictor, Downstream rankers
Downstream rankers Surface-specific ranking, re-ranking, ad, and organic optimization layers. Unified retrieval layer, Recommendation surfaces
Recommendation surfaces Search, item details, cart, pre-checkout, and post-checkout placements. Downstream rankers
Recall@K evaluator Offline metric for next-product prediction quality. Historical interaction data

Data flow

User session actions such as cart adds and product page views become ordered product-ID sequences. The BERT-like model encodes the sequence and predicts next-product probabilities over the catalog vocabulary. The retrieval layer returns top-K candidates to downstream ranking/re-ranking systems for many Instacart surfaces, and offline Recall@K evaluates whether the held-out last product is retrieved.

3 supporting quotes
“Our contextual retrieval system reacts in real time to a user’s actions within a shopping session and retrieves products relevant to that session.”article, stated
“Given a sequence of products that the user interacted with, we predict the probabilities for products that the user may interact with next.”article, stated
“Once we predict the probabilities across all product ids, we pick the top K products based on predicted probabilities for further downstream ranking.”article, stated

Why this architecture

Instacart unified retrieval because ads and organic systems shared a need for session-aware candidates even though their rankers optimize different goals. A BERT-like MLM setup was chosen after evaluating language-model architectures, and product-ID-only sequences were enough for a strong preliminary system before adding richer features.

3 supporting quotes
“Having a common retrieval system across both ads and organic surfaces has lowered our maintenance costs and allowed us to deprecate many legacy ad hoc retrieval systems.”article, stated
“We tried different Language model architectures like XLNet and BERT, and based on offline evaluation we converged on a simple BERT-like model”article, stated
“Just using product ID sequences without any additional user or product features has shown significant impact and we limit ourselves to describing a preliminary version of this system that does not use any other features.”article, stated

Models used: BERT-like model · BERT4Rec · Transformers4Rec · XLNet

Scale

Alternatives considered (roads not taken)

Maintain separate ad hoc retrieval systems per surface

Rejected because: Unified retrieval lowered maintenance cost and deprecated legacy systems.

“Prior to building this contextual retrieval system, we had a disparate set of retrieval systems for different recommendation surfaces across ads and organic surfaces.”article, stated
“By leveraging a unified retrieval system, we were able to deprecate old ad hoc systems. ## Get pradeep karuturi’s stories in your inbox”article, stated

Use co-occurrence, similarity, and popularity signals only

Rejected because: Next-product prediction was a more principled and empirically successful path.

“mostly based on an ad-hoc combination of product co-occurrence, similarity and popularity signals”article, stated
“the next product purchase prediction turns out to be a principled path forward as well as empirically more successful.”article, stated

Ignore product order and treat sessions as product bags

Rejected because: Randomized training/evaluation showed sequence order materially affects quality.

“This indicates that sequence information at training has a meaningful impact on the quality of next product predictions.”article, stated

Patterns

retrieval-ranking (recommendations)

The BERT-like model retrieves top-K product candidates before downstream rankers and re-rankers.

“we pick the top K products based on predicted probabilities for further downstream ranking.”article, stated

model-routing (ranking-stack)

A common retrieval layer feeds diverse downstream ranking systems with different ads/organic objectives.

“even though their end goals and ranking layers are different.”article, stated

eval-driven-development (model-selection)

Offline Recall@K and sequence-ablation experiments drove architecture and sequence-length decisions.

“We evaluate the models offline using Recall@K metric.”article, stated

Tradeoffs

Start with product-ID sequences only

Chose simpler sequence model over including all user/product/context features immediately.

Rationale: The preliminary version already showed significant impact and reduced complexity.

“we limit ourselves to describing a preliminary version of this system that does not use any other features.”article, stated

Use sequence length of 20

Chose bounded recent session context over unbounded full-session history.

Rationale: Recent 3-5 products matter most and prior products diminish with sequence length.

“The last 3–5 products in the sequence have an outsized influence on the recommendations”article, stated
“we resorted to using sequence length of 20 for training and inference.”article, stated

Use unified retrieval across ads and organic

Chose shared contextual retrieval layer over surface-specific retrieval stacks.

Rationale: Shared retrieval reduces maintenance while downstream rankers still optimize surface-specific goals.

“Having a common retrieval system across both ads and organic surfaces has lowered our maintenance costs”article, stated

Gotchas (what bit them)

Catalog scale makes naive BERT-style retrieval difficult.

Root cause: Instacart has millions of products across thousands of retailers, far beyond tens-of-thousands product benchmarks.

Resolution: The first iteration restricts vocabulary under one million and explores ANN scaling.

“For the first iteration of our system, we restricted our models to have a product-id vocabulary of under one million.”article, stated
“We have explored ANN approaches to scale our model to millions of products.”article, stated

Product identity varies across retailers.

Root cause: Some products share canonical identity but others, like non-branded produce, do not.

Resolution: Instacart points to text-content-inclusive approaches such as TiGER.

“Some of the products share a common identity across retailers, but some products, such as non-branded strawberries, may not share a common product identity.”article, stated

Popularity bias can dominate recommendations.

Root cause: Most interaction data comes from a small subset of the catalog.

Resolution: Instacart gets top-K products for a particular retailer and continues to iterate.

“the majority of our user interaction data is with a small subset of our catalog”article, stated

Evaluation

Instacart evaluates with offline Recall@K next-product prediction, randomized sequence training/evaluation ablations, online cart-add lift, and impact across transaction volume and ad marketplace metrics.

Metrics: Recall@K · 10-40% worse Recall@K from randomized training sequence · 20-45% worse metrics from randomized evaluation order · 30% lift in user cart additions · last 3-5 products have outsized influence

3 supporting quotes
“This metric roughly gives us the percentage of times when the last token in the test sequence dataset is present in the top K predictions”article, stated
“Our initial offline evaluation showed a significant uplift over prior systems”article, stated
“We also evaluated the impact of more recent products in the user interaction sequence on recommendations.”article, stated

Outcomes

Non-agentic ML/retrieval case: millions of products, thousands of retailers, multiple surfaces, product vocabulary constrained under one million in v1, and explicit lift/ablation metrics.

3 supporting quotes
“Implementing transformer-based sequence models in our contextual retrieval layer has significantly enhanced the performance”article, stated
“by adopting a unified retrieval layer that serves both ads and organic surfaces, we have been able to phase out legacy systems”article, stated
“Leveraging user interaction sequences has enabled us to develop more effective models than previous ones.”article, stated