Skip to content

feat: implement structured exception classes with error codes and sug…#3279

Merged
parshvadaftari merged 6 commits intomem0ai:mainfrom
bkidd1:feature/structured-exceptions
Sep 18, 2025
Merged

feat: implement structured exception classes with error codes and sug…#3279
parshvadaftari merged 6 commits intomem0ai:mainfrom
bkidd1:feature/structured-exceptions

Conversation

@bkidd1
Copy link
Copy Markdown
Contributor

@bkidd1 bkidd1 commented Aug 5, 2025

Summary

Replace generic APIError with specific exception types (AuthenticationError, RateLimitError, ValidationError, NetworkError, etc.)

  • Add comprehensive error context with error codes, suggestions, and debug info
  • Implement HTTP status code to exception mapping for automatic error classification
  • Update api_error_handler decorator to use new structured exceptions
  • Add 57 comprehensive tests covering all exception scenarios
  • Update client method docstrings to reference specific exception types
  • Maintain backward compatibility with deprecated APIError class
  • Enable better error recovery patterns and user feedback

Resolves: #3270

Description

This PR implements structured exception classes to replace the generic APIError with specific, actionable exceptions that provide better context for debugging and user feedback. The implementation enables developers to implement proper error recovery patterns, show meaningful errors to users, and improves monitoring capabilities.

Key improvements:

  • 10 specific exception types with clear inheritance from base MemoryError
  • Automatic HTTP status code mapping (400→ValidationError, 429→RateLimitError, etc.)
  • Rich error context including error codes, suggestions, debug info, and retry timing
  • Real-world usage patterns matching the exact examples from the GitHub issue

Example usage:

try:
    memory.add(content, user_id=user_id)
except RateLimitError as e:
    time.sleep(e.debug_info.get('retry_after', 60))
except MemoryQuotaExceededError as e:
    logger.error(f"Quota exceeded: {e.error_code}")
except ValidationError as e:
    raise HTTPException(400, detail=e.suggestion)

Fixes #3270

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Unit Test
  • 57 comprehensive tests covering all exception scenarios:
    • Individual exception class testing (initialization, attributes, inheritance)
    • HTTP status code to exception mapping validation
    • api_error_handler decorator integration testing
    • Real-world usage examples from the GitHub issue
    • Backward compatibility testing for existing APIError usage
    • Network error classification (timeout, connection, generic)
    • Rate limit header extraction and retry logic

Test execution:

hatch run dev_py_3_11:pytest tests/test_exceptions.py tests/test_client_utils.py -v
# Result: 57 passed, 0 failed

Multi-Python version testing:

  • Python 3.10: ✅ 57 passed
  • Python 3.11: ✅ 57 passed

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Aug 5, 2025

CLA assistant check
All committers have signed the CLA.

@bkidd1
Copy link
Copy Markdown
Contributor Author

bkidd1 commented Aug 7, 2025

@deshraj just following up here :) happy to make improvements/edits

@parshvadaftari
Copy link
Copy Markdown
Contributor

@bkidd1 Can you resolve merge conflicts?

@parshvadaftari parshvadaftari self-requested a review September 15, 2025 21:41
Copy link
Copy Markdown
Contributor

@parshvadaftari parshvadaftari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are specifically for client. Can you add for the OSS as well? I see multiple exception handling class which can be used for OSS

…gestions

- Replace generic APIError with specific exception types (AuthenticationError,
  RateLimitError, ValidationError, NetworkError, etc.)
- Add comprehensive error context with error codes, suggestions, and debug info
- Implement HTTP status code to exception mapping for automatic error classification
- Update api_error_handler decorator to use new structured exceptions
- Add 57 comprehensive tests covering all exception scenarios
- Update client method docstrings to reference specific exception types
- Maintain backward compatibility with deprecated APIError class
- Enable better error recovery patterns and user feedback

Resolves: mem0ai#3270
@bkidd1 bkidd1 force-pushed the feature/structured-exceptions branch from 579d5cd to a572b48 Compare September 16, 2025 02:27
@parshvadaftari
Copy link
Copy Markdown
Contributor

parshvadaftari commented Sep 16, 2025

@bkidd1 Can you see for the failing tests? Thanks

- Remove unused imports that were only referenced in docstrings
- Add back NetworkError import to utils.py (required for actual code usage)
- Maintain clean, production-ready code with comprehensive test coverage
- All tests passing: 40/40 exception tests, 17/17 client utils tests
- Ready for CI validation and merge

Resolves linting issues while preserving all structured exception functionality
- Add 6 new OSS-specific exception classes: VectorStoreError, GraphStoreError,
  EmbeddingError, LLMError, DatabaseError, DependencyError
- Update OSS memory operations to use structured exceptions instead of generic ValueError
- Replace ValueError with Mem0ValidationError in main.py with rich context
- Add comprehensive docstrings to OSS methods referencing structured exceptions
- Add 11 comprehensive tests for OSS exception handling
- All tests passing: 68/68 total tests (40 + 11 + 17)

This extends the structured exception system to cover both client and OSS use cases,
providing consistent error handling across the entire Mem0 ecosystem.

Resolves: mem0ai#3270
@bkidd1
Copy link
Copy Markdown
Contributor Author

bkidd1 commented Sep 16, 2025

@parshvadaftari fixed errors and added OSS!

…gestions

- Add comprehensive exception hierarchy with MemoryError base class
- Implement specific exceptions: AuthenticationError, RateLimitError, ValidationError, etc.
- Add OSS-specific exceptions: VectorStoreError, GraphStoreError, EmbeddingError, etc.
- Update client docstrings to reference new exception types
- Update memory operations to use structured exceptions
- Remove unused imports to fix linting errors
- Remove extensive test files to keep PR focused on core functionality
@bkidd1
Copy link
Copy Markdown
Contributor Author

bkidd1 commented Sep 17, 2025

@parshvadaftari my bad, i didn't run ruff/isort correctly the previous commit (and i had to remove leftover test file) but it should be clean now

@parshvadaftari
Copy link
Copy Markdown
Contributor

@bkidd1 No worries, I hope the tests will pass.

@bkidd1
Copy link
Copy Markdown
Contributor Author

bkidd1 commented Sep 18, 2025

@parshvadaftari i see everything passed but build_embedchain was skipped. do i need to fix something for this to run? lmk :)

@parshvadaftari parshvadaftari merged commit d4e98db into mem0ai:main Sep 18, 2025
6 of 7 checks passed
@parshvadaftari
Copy link
Copy Markdown
Contributor

parshvadaftari commented Sep 18, 2025

@bkidd1 Thanks for the PR, no need to fix anything. Thank you for the contribution

This was referenced Mar 19, 2026
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhance error handling with structured exception classes

3 participants