2.5 KiB
Bug Fix: ChromaDB Collection Name Collision
Issue Description
When running multiple analyses through the API (either concurrently or sequentially), the system would fail with:
chromadb.errors.InternalError: Collection [bull_memory] already exists
Root Cause
The TradingAgentsGraph class was creating ChromaDB memory collections with hardcoded names:
bull_memorybear_memorytrader_memoryinvest_judge_memoryrisk_manager_memory
When multiple analyses ran (even for different tickers), they all tried to create collections with the same names, causing ChromaDB to reject duplicate collection creation.
Location of the bug:
tradingagents/graph/trading_graph.pylines 90-94tradingagents/agents/utils/memory.pyline 14
Solution Implemented
Changes Made
-
Modified
TradingAgentsGraph.__init__(tradingagents/graph/trading_graph.py):- Added optional
analysis_idparameter - Collection names now include the analysis ID as a suffix:
bull_memory_{analysis_id} - When
analysis_idis None, collections use original names (backward compatibility)
- Added optional
-
Modified
state_manager.py(api/state_manager.py):- Pass the unique
analysis_idwhen creatingTradingAgentsGraph - Added cleanup in
finallyblock to delete collections after analysis completes
- Pass the unique
-
Added cleanup method (
tradingagents/graph/trading_graph.py):- New
cleanup_memories()method to delete ChromaDB collections - Called after each analysis (success or failure) to prevent memory leaks
- Prevents accumulation of old collections in the database
- New
Backward Compatibility
The fix is fully backward compatible:
- CLI usage (
cli/main.py) - continues to work withoutanalysis_id - Standalone usage (
main.py) - continues to work withoutanalysis_id - API usage - now provides unique
analysis_idfor isolation
Testing Recommendations
- Test concurrent analyses: Run multiple analyses simultaneously for the same or different tickers
- Test sequential analyses: Run multiple analyses one after another for the same ticker
- Test failure scenarios: Ensure collections are cleaned up even when analysis fails
- Test CLI: Verify CLI still works without regression
Benefits
✅ Multiple analyses can now run concurrently without conflicts
✅ Same ticker can be analyzed multiple times without errors
✅ Memory collections are properly cleaned up after each analysis
✅ No breaking changes to existing code
✅ Prevents ChromaDB from accumulating stale collections