Skip to main content
The Mem[v] SDK provides comprehensive error handling with specific exception types for different error scenarios.

Exception types

import memvai

# Base exception
memvai.APIError

# Connection errors
memvai.APIConnectionError
memvai.APITimeoutError

# Status code errors
memvai.BadRequestError        # 400
memvai.AuthenticationError    # 401
memvai.PermissionDeniedError  # 403
memvai.NotFoundError          # 404
memvai.UnprocessableEntityError  # 422
memvai.RateLimitError         # 429
memvai.InternalServerError    # 500+

Basic error handling

import memvai
from memvai import Memv

client = Memv()

try:
    spaces = client.spaces.list()
except memvai.APIError as e:
    print(f"API Error: {e.message}")

Handle specific errors

Authentication errors

try:
    client = Memv(api_key="invalid_key")
    spaces = client.spaces.list()
except memvai.AuthenticationError:
    print("Invalid API key - check your credentials")

Not found errors

try:
    space = client.spaces.retrieve(space_id="non_existent")
except memvai.NotFoundError:
    print("Space not found")
    # Create the space or handle missing resource

Rate limiting

import time

try:
    for i in range(1000):
        client.memories.add(
            space_id="space_abc123",
            content=f"Memory {i}"
        )
except memvai.RateLimitError as e:
    print("Rate limit exceeded")
    retry_after = e.response.headers.get('Retry-After', 60)
    print(f"Waiting {retry_after} seconds...")
    time.sleep(int(retry_after))

Access error details

try:
    client.spaces.create(name="")
except memvai.APIStatusError as e:
    print(f"Status: {e.status_code}")
    print(f"Message: {e.message}")
    print(f"Headers: {e.response.headers}")

Retry logic

Custom retry with backoff

import time

def retry_with_backoff(func, max_attempts=3, base_delay=1):
    """Retry with exponential backoff."""
    for attempt in range(max_attempts):
        try:
            return func()
        except memvai.RateLimitError:
            if attempt == max_attempts - 1:
                raise
            delay = base_delay * (2 ** attempt)
            print(f"Rate limited. Retrying in {delay}s...")
            time.sleep(delay)
        except memvai.APIError as e:
            if attempt == max_attempts - 1:
                raise
            print(f"Error: {e}. Retrying...")
            time.sleep(base_delay)

# Usage
result = retry_with_backoff(lambda: client.spaces.list())

Best practices

Never let exceptions propagate unchecked.
Catch specific exceptions before general ones.
Always log errors with relevant context for debugging.
Retry operations that might fail temporarily.

Next steps

Advanced usage

Advanced SDK features

Installation

SDK installation and setup