Skip to main content

Best Practices & Error Handling

Learn how to use the Moorcheh Python SDK effectively and efficiently in your applications.

1. Use Context Managers

Always use the client as a context manager (with statement) to ensure proper cleanup of resources.
Context Manager Usage

2. Batch Processing

When uploading large amounts of data, use batching to optimize performance and handle errors gracefully.
Batch Processing Example

3. Environment Variables

Store sensitive information like API keys in environment variables instead of hardcoding them.
Environment Variables Example

4. Implement Retries

For production applications, implement retry logic for transient errors.
Retry Logic Example

5. Namespace Organization

Organize your namespaces logically and use descriptive names.
Namespace Organization Example

6. Document Structure

Use consistent and well-structured document formats.
Document Structure Example

7. Logging and Monitoring

Implement proper logging for debugging and monitoring.
Logging Example

8. Testing

Write tests for your SDK integration code.
Testing Example

Error Handling

The SDK uses custom exceptions to signal specific problems. It’s best to wrap your code in a try…except block to handle them gracefully.
Error Handling Example

Exception Hierarchy

The SDK provides a hierarchy of exceptions for different error conditions:

MoorchehError

Base exception for all SDK errors

APIError

General API errors (4xx, 5xx responses)

AuthenticationError

Invalid or missing API key

ConflictError

Resource already exists (409)

NamespaceNotFound

Namespace doesn’t exist (404)

ValidationError

Invalid input parameters

RateLimitError

Too many requests (429)

ServerError

Internal server error (5xx)

Production Checklist

1

Security

  • Store API keys in environment variables
  • Never commit API keys to version control
  • Use different API keys for different environments
  • Regularly rotate API keys
2

Error Handling

  • Implement comprehensive exception handling
  • Add retry logic for transient failures
  • Log errors with appropriate detail
  • Monitor error rates and patterns
3

Performance

  • Use context managers for resource management
  • Batch large uploads and operations
  • Set appropriate timeouts
  • Monitor API usage and quotas
4

Data Management

  • Use consistent document structures
  • Organize namespaces logically
  • Implement data validation
  • Plan for data lifecycle management
5

Testing

  • Write unit tests for SDK integration
  • Test error conditions and edge cases
  • Use test namespaces for development
  • Implement integration tests

Additional Best Practices

Async Processing Awareness

  • Text Documents: Remember that embedding is asynchronous. Add a short delay (time.sleep()) before searching to ensure your documents are indexed and available.
  • Vector Documents: Vector uploads are synchronous and immediately available for search.

Document Chunking

  • For best search and generative AI results, split large documents into smaller, meaningful chunks (e.g., paragraphs) before uploading.
  • Each chunk should have a unique ID and contain coherent, self-contained information.

Performance Optimization

  • Use appropriate top_k values - higher values provide more context but may increase response time.
  • Set reasonable thresholds to filter out low-relevance results.
  • Use kiosk mode for production applications where result quality is critical.

Memory Management

  • For large batch operations, process data in chunks to avoid memory issues.
  • Clean up large variables after processing batches.
  • Monitor memory usage during bulk operations.