fix(api): handle dict vs object in build_product_context_summary

When products are loaded from PostgreSQL, JSON columns (effect_profile,
context_rules) are deserialized as plain dicts, not Pydantic models.

The build_product_context_summary function was accessing these fields
as object attributes (.safe_with_compromised_barrier) which caused:
AttributeError: 'dict' object has no attribute 'safe_with_compromised_barrier'

Fix: Add isinstance(dict) checks like build_product_context_detailed already does.
Handle both dict (from DB) and object (from Pydantic) cases.

Traceback from production:
  File "llm_context.py", line 91, in build_product_context_summary
    if product.context_rules.safe_with_compromised_barrier:
  AttributeError: 'dict' object has no attribute...
This commit is contained in:
Piotr Oleszczyk 2026-03-06 10:34:51 +01:00
parent 594dae474b
commit 3ef1f249b6

View file

@ -72,6 +72,19 @@ def build_product_context_summary(product: Product, has_inventory: bool = False)
if hasattr(product, "effect_profile") and product.effect_profile:
profile = product.effect_profile
# Only include notable effects (score > 0)
# Handle both dict (from DB) and object (from Pydantic)
if isinstance(profile, dict):
if profile.get("hydration_immediate", 0) > 0:
effects.append(f"hydration={profile['hydration_immediate']}")
if profile.get("exfoliation_strength", 0) > 0:
effects.append(f"exfoliation={profile['exfoliation_strength']}")
if profile.get("retinoid_strength", 0) > 0:
effects.append(f"retinoid={profile['retinoid_strength']}")
if profile.get("irritation_risk", 0) > 0:
effects.append(f"irritation_risk={profile['irritation_risk']}")
if profile.get("barrier_disruption_risk", 0) > 0:
effects.append(f"barrier_risk={profile['barrier_disruption_risk']}")
else:
if profile.hydration_immediate and profile.hydration_immediate > 0:
effects.append(f"hydration={profile.hydration_immediate}")
if profile.exfoliation_strength and profile.exfoliation_strength > 0:
@ -88,9 +101,17 @@ def build_product_context_summary(product: Product, has_inventory: bool = False)
# Safety flags
safety_flags = []
if hasattr(product, "context_rules") and product.context_rules:
if product.context_rules.safe_with_compromised_barrier:
rules = product.context_rules
# Handle both dict (from DB) and object (from Pydantic)
if isinstance(rules, dict):
if rules.get("safe_with_compromised_barrier"):
safety_flags.append("barrier_ok")
if not product.context_rules.safe_after_shaving:
if not rules.get("safe_after_shaving", True):
safety_flags.append("!post_shave")
else:
if rules.safe_with_compromised_barrier:
safety_flags.append("barrier_ok")
if not rules.safe_after_shaving:
safety_flags.append("!post_shave")
safety_str = f" safety={{{','.join(safety_flags)}}}" if safety_flags else ""