Before: Vague instructions like "be careful not to make up ticker symbols"
After:
- Explicit rules with examples
- Clear VALID vs INVALID examples
- Specific instructions about what NOT to recommend
- Better formatting requirements
Key Additions:
- ✅ Valid examples showing correct format
- ❌ Invalid examples showing what NOT to do (Canada, sectors, etc.)
- Explicit list of non-tradeable entities to avoid
- Clear ticker format specification (1-5 letters, optional class shares like BRK.B)
New Function: validate_recommendation(rec)
Validates:
- ✅ Required fields present
- ✅ Ticker format (1-5 uppercase letters, optional .X for class shares)
- ✅ Recommendation type (BUY/SELL only)
- ✅ Confidence level (HIGH/MEDIUM/LOW only)
- ✅ Company name doesn't contain invalid keywords
- ✅ Company name is not a country name
- ✅ Company name is not a currency
- ✅ Company name is not a commodity
- ✅ Company name is not a sector/index
Returns: (is_valid: bool, error_message: str)
Before: temperature=0.7 (too creative for structured JSON)
After: temperature=0.2 (more deterministic, better for JSON output)
Before: All recommendations stored, even invalid ones
After:
- Each recommendation validated before storage
- Invalid recommendations logged with error messages
- Only valid recommendations stored in database
1. LLM generates recommendations
↓
2. Each recommendation validated
↓
3. Invalid ones logged and rejected
↓
4. Only valid recommendations stored
The system will now reject and log:
# Country instead of stock
{"company": "Canada", "ticker": "CAN", ...}
→ Error: "Invalid entity: Canada is a country, not a stock"
# Sector instead of stock
{"company": "Technology Sector", "ticker": "TECH", ...}
→ Error: "Invalid entity type: Technology Sector (contains invalid keyword)"
# Invalid ticker format
{"company": "Apple Inc", "ticker": "AAPL123", ...}
→ Error: "Invalid ticker format: AAPL123 (must be 1-5 uppercase letters)"
# Invalid recommendation type
{"company": "Apple Inc", "ticker": "AAPL", "recommendation": "HOLD", ...}
→ Error: "Invalid recommendation type: HOLD (must be BUY or SELL)"- Data Quality: Only valid stock recommendations stored
- Error Prevention: Catches common LLM mistakes (countries, sectors, etc.)
- Better Prompts: Clear examples help LLM understand requirements
- Debugging: Validation errors logged for analysis
- Format Consistency: Ensures all recommendations follow database schema
To test the improvements:
- Monitor logs for validation failures
- Check database - should only contain valid stock recommendations
- Review rejected recommendations to identify prompt improvements
- Adjust validation rules if needed based on real-world data
- Ticker Symbol Lookup: Validate against real stock exchange databases
- Company Name Normalization: Standardize company names (e.g., "Apple Inc" vs "Apple")
- Confidence Calibration: Track which confidence levels correlate with accuracy
- Feedback Loop: Allow users to flag incorrect recommendations to improve prompts