Fix: Stop after all primary vendors in multi-vendor config

When multiple primary vendors are configured (e.g., 'reddit,alpha_vantage'),
the system now correctly stops after attempting all primary vendors instead
of continuing through all fallback vendors.

Changes:
- Track which primary vendors have been attempted in a list
- Add stopping condition when all primary vendors are attempted
- Preserve existing single-vendor behavior (stop after first success)

This prevents unnecessary API calls and ensures predictable behavior.
This commit is contained in:
Youssef Aitousarrah 2025-11-23 17:00:01 -08:00
parent 13b826a31d
commit 9ee66746a5
1 changed files with 11 additions and 7 deletions

View File

@ -166,7 +166,7 @@ def route_to_vendor(method: str, *args, **kwargs):
# Track results and execution state
results = []
vendor_attempt_count = 0
any_primary_vendor_attempted = False
primary_vendors_attempted = []
successful_vendor = None
for vendor in fallback_vendors:
@ -179,10 +179,6 @@ def route_to_vendor(method: str, *args, **kwargs):
is_primary_vendor = vendor in primary_vendors
vendor_attempt_count += 1
# Track if we attempted any primary vendor
if is_primary_vendor:
any_primary_vendor_attempted = True
# Debug: Print current attempt
vendor_type = "PRIMARY" if is_primary_vendor else "FALLBACK"
print(f"DEBUG: Attempting {vendor_type} vendor '{vendor}' for {method} (attempt #{vendor_attempt_count})")
@ -214,6 +210,10 @@ def route_to_vendor(method: str, *args, **kwargs):
print(f"FAILED: {impl_func.__name__} from vendor '{vendor_name}' failed: {e}")
continue
# Track which primary vendors we've attempted
if is_primary_vendor:
primary_vendors_attempted.append(vendor)
# Add this vendor's results
if vendor_results:
results.extend(vendor_results)
@ -221,13 +221,17 @@ def route_to_vendor(method: str, *args, **kwargs):
result_summary = f"Got {len(vendor_results)} result(s)"
print(f"SUCCESS: Vendor '{vendor}' succeeded - {result_summary}")
# Stopping logic: Stop after first successful vendor for single-vendor configs
# Multiple vendor configs (comma-separated) may want to collect from multiple sources
# Stopping logic for single vendor: Stop after first success
if len(primary_vendors) == 1:
print(f"DEBUG: Stopping after successful vendor '{vendor}' (single-vendor config)")
break
else:
print(f"FAILED: Vendor '{vendor}' produced no results")
# Stopping logic for multi-vendor: Stop after all primaries attempted (regardless of success)
if len(primary_vendors) > 1 and set(primary_vendors_attempted) == set(primary_vendors):
print(f"DEBUG: All primary vendors attempted ({', '.join(primary_vendors_attempted)}), stopping")
break
# Final result summary
if not results: