Error Handling and Debugging

The Mathpix Python SDK is designed to surface informative errors when issues occur, helping you debug problems effectively.

Common Error Types

  • Authentication Errors:
    These occur if your app_id or app_key are missing, invalid, or expired.
    Check your credentials and environment setup.
     
  • Network Errors:
    If the Mathpix API server is unreachable, a network error will be raised.
    Ensure your internet connection is active and that firewall rules are not blocking access to api.mathpix.com.
     
  • Invalid Input Errors:
    Errors related to invalid file formats, unsupported media types, or incorrect parameters passed to the API methods.
     
  • Rate Limit Errors:
    If you exceed your Mathpix usage limits (requests per second or per month), you will receive a rate limiting error.
    Contact Mathpix support if you need a higher quota.

Debugging Tips

  • Enable logging in your application to capture error messages and full responses.
  • Carefully inspect any error messages returned by API calls — they usually include helpful explanations.
  • Check your authentication method and token expiration if requests suddenly start failing.
  • Validate file paths and URLs when uploading documents or images.
  • Ensure you are passing the correct parameters expected by each SDK method.

Example Error Handling

from mpxpy.mathpix_client import MathpixClient
from mpxpy.exceptions import MathpixClientError

client = MathpixClient()

try:
    image = client.image_new(file_path="nonexistent.jpg")
    mmd = image.mmd()
except MathpixClientError as e:
    print(f"MathpixClientError: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")
If your error handling needs to distinguish between types of problems (e.g., authentication issues vs. file errors), you can catch and process exceptions accordingly.