Update app.py
Browse files
app.py
CHANGED
|
@@ -329,6 +329,14 @@ def get_recommendation_with_agent(user_id, merchant, category, amount):
|
|
| 329 |
return
|
| 330 |
|
| 331 |
result = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
# Stage 4
|
| 334 |
yield """
|
|
@@ -360,12 +368,35 @@ def get_recommendation_with_agent(user_id, merchant, category, amount):
|
|
| 360 |
|
| 361 |
print(f"π KEYS: {list(result.keys())}")
|
| 362 |
|
| 363 |
-
recommendation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
|
| 365 |
if not recommendation:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
yield f"β Invalid response: No recommendation found", None
|
| 367 |
return
|
| 368 |
|
|
|
|
| 369 |
# β
Extract values correctly
|
| 370 |
card_id = recommendation.get('recommended_card', 'Unknown')
|
| 371 |
card_name = recommendation.get('card_name', card_id)
|
|
|
|
| 329 |
return
|
| 330 |
|
| 331 |
result = response.json()
|
| 332 |
+
# β
CRITICAL DEBUG: Check response structure
|
| 333 |
+
print("=" * 80)
|
| 334 |
+
print("π DEBUG: Response Structure Analysis")
|
| 335 |
+
print(f"Response type: {type(result)}")
|
| 336 |
+
print(f"Top-level keys: {list(result.keys()) if isinstance(result, dict) else 'Not a dict'}")
|
| 337 |
+
|
| 338 |
+
# β
FLEXIBLE EXTRACTION: Handle multiple response formats
|
| 339 |
+
recommendation = None
|
| 340 |
|
| 341 |
# Stage 4
|
| 342 |
yield """
|
|
|
|
| 368 |
|
| 369 |
print(f"π KEYS: {list(result.keys())}")
|
| 370 |
|
| 371 |
+
# Format 1: Nested recommendation (expected from your agent_core.py)
|
| 372 |
+
if isinstance(result, dict) and 'recommendation' in result:
|
| 373 |
+
recommendation = result['recommendation']
|
| 374 |
+
print("β
Found 'recommendation' key (Format 1: Nested)")
|
| 375 |
+
|
| 376 |
+
# Format 2: Direct recommendation (flat structure)
|
| 377 |
+
elif isinstance(result, dict) and 'recommended_card' in result:
|
| 378 |
+
recommendation = result
|
| 379 |
+
print("β
Using direct response (Format 2: Flat)")
|
| 380 |
+
|
| 381 |
+
# Format 3: Check if it's wrapped in 'data' key
|
| 382 |
+
elif isinstance(result, dict) and 'data' in result:
|
| 383 |
+
data = result['data']
|
| 384 |
+
if isinstance(data, dict) and 'recommendation' in data:
|
| 385 |
+
recommendation = data['recommendation']
|
| 386 |
+
print("β
Found in 'data.recommendation' (Format 3)")
|
| 387 |
+
elif isinstance(data, dict) and 'recommended_card' in data:
|
| 388 |
+
recommendation = data
|
| 389 |
+
print("β
Found in 'data' directly (Format 3b)")
|
| 390 |
|
| 391 |
if not recommendation:
|
| 392 |
+
print(f"β ERROR: Could not find recommendation in response")
|
| 393 |
+
print(f"Available keys: {list(result.keys())}")
|
| 394 |
+
print(f"Full response (first 1000 chars): {str(result)[:1000]}")
|
| 395 |
+
|
| 396 |
yield f"β Invalid response: No recommendation found", None
|
| 397 |
return
|
| 398 |
|
| 399 |
+
|
| 400 |
# β
Extract values correctly
|
| 401 |
card_id = recommendation.get('recommended_card', 'Unknown')
|
| 402 |
card_name = recommendation.get('card_name', card_id)
|