Skip to content

Commit 6b65295

Browse files
fix: resolve critical issues in memory provider error handling
- Fix factory return value being discarded in memory.py - Remove silent exception swallowing that broke backward compatibility - Fix datetime import bug in MongoDB adapter (datetime.timezone.utc) - Preserve original error propagation for misconfigured providers - Ensure factory-created adapters are properly used when successful Fixes issues identified by Greptile and CodeRabbit reviewers. Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent e0b8fd7 commit 6b65295

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/praisonai-agents/praisonaiagents/memory/adapters/factories.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,15 @@ def _create_indexes(self):
364364

365365
def store_short_term(self, text: str, metadata: Optional[Dict[str, Any]] = None, **kwargs) -> str:
366366
"""Store in MongoDB short-term collection."""
367-
from datetime import datetime
367+
from datetime import datetime, timezone
368368
import time
369369

370370
doc_id = str(time.time_ns())
371371
doc = {
372372
"_id": doc_id,
373373
"content": text,
374374
"metadata": metadata or {},
375-
"created_at": datetime.now(datetime.timezone.utc),
375+
"created_at": datetime.now(timezone.utc),
376376
"memory_type": "short_term"
377377
}
378378

@@ -396,15 +396,15 @@ def search_short_term(self, query: str, limit: int = 5, **kwargs) -> List[Dict[s
396396

397397
def store_long_term(self, text: str, metadata: Optional[Dict[str, Any]] = None, **kwargs) -> str:
398398
"""Store in MongoDB long-term collection."""
399-
from datetime import datetime
399+
from datetime import datetime, timezone
400400
import time
401401

402402
doc_id = str(time.time_ns())
403403
doc = {
404404
"_id": doc_id,
405405
"content": text,
406406
"metadata": metadata or {},
407-
"created_at": datetime.now(datetime.timezone.utc),
407+
"created_at": datetime.now(timezone.utc),
408408
"memory_type": "long_term"
409409
}
410410

src/praisonai-agents/praisonaiagents/memory/memory.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,22 @@ def _init_protocol_driven_memory(self):
172172
"mongodb": create_mongodb_memory_adapter,
173173
}
174174
if adapter_name in factory_map:
175-
# Call the factory to trigger the ImportError with installation hint
175+
# Call the factory to get ImportError with installation hint
176+
# or capture successful adapter creation
176177
adapter_config = self._get_adapter_config_for_provider(adapter_name)
177-
factory_map[adapter_name](**adapter_config)
178+
adapter = factory_map[adapter_name](**adapter_config)
178179
except ImportError:
179-
# Re-raise the ImportError with installation instructions
180+
# Re-raise ImportError with installation instructions
180181
raise
181-
except Exception:
182-
# Fall through to fallback behavior for other errors
183-
pass
182+
# All other exceptions (e.g., ValueError for missing API key)
183+
# propagate naturally, preserving backward compatibility
184184

185+
# If factory succeeded, use that adapter
186+
if adapter is not None:
187+
self.memory_adapter = adapter
188+
self.provider = adapter_name
189+
return
190+
185191
# Fallback to first available adapter
186192
self._log_verbose(f"Provider '{adapter_name}' not available, trying fallbacks")
187193
# Try each fallback preference individually

0 commit comments

Comments
 (0)