Configuration

This document provides comprehensive documentation for configuration classes in the ViewAI Python SDK.

Overview

The ViewAI SDK provides flexible configuration management through:

  • ClientConfiguration: Main configuration class for client setup

  • RetryStrategy: Configurable retry logic with exponential backoff

  • Environment Variables: Load configuration from environment


ClientConfiguration

Enterprise client configuration with validation.

Overview

The ClientConfiguration dataclass holds all configuration parameters for the ViewAI client, providing validation, defaults, and environment variable support.

Initialization

@dataclass
class ClientConfiguration:
    """Enterprise client configuration with validation."""

Parameters:

  • Required:

    • api_key (str): API authentication key

  • API Configuration:

    • base_url (str): Base URL for API requests (default: "https://qi1gp3ed2m.execute-api.us-west-1.amazonaws.com")

  • Timeout Configuration:

    • connection_timeout (int): Connection timeout in seconds (default: 10)

    • read_timeout (int): Read timeout in seconds (default: 30)

  • Retry Configuration:

    • max_retries (int): Maximum number of retry attempts (default: 3)

    • retry_backoff_factor (float): Exponential backoff multiplier for retries (default: 2.0)

  • Resource Limits:

    • max_upload_size_mb (int): Maximum file upload size in megabytes (default: 100)

    • max_batch_size (int): Maximum number of items in batch operations (default: 10000)

  • Logging Configuration:

    • log_level (int): Logging level (default: logging.INFO)

    • log_file (str, optional): Optional log file path (default: None)

  • Performance Configuration:

    • enable_caching (bool): Enable response caching (default: True)

    • cache_ttl_seconds (int): Cache time-to-live in seconds (default: 300)

    • connection_pool_size (int): HTTP connection pool size (default: 10)

  • Feature Flags:

    • enable_telemetry (bool): Enable telemetry and metrics collection (default: False)

    • enable_compression (bool): Enable request/response compression (default: True)

  • Context Defaults:

    • default_workspace (str, optional): Default workspace name or ID (default: None)

    • default_project (str, optional): Default project name or ID (default: None)

    • default_model_id (str, optional): Default model ID for predictions (default: None)


Basic Usage


Advanced Configuration


Methods

validate()

Validate configuration values.

Raises:

  • ValueError: If any configuration value is invalid

Example:

Validation Rules:

  • API key must not be empty

  • Base URL must start with http:// or https://

  • Timeouts must be positive

  • Connection timeout cannot exceed read timeout

  • Max retries must be between 0 and 10

  • Retry backoff factor must be positive

  • Resource limits must be positive

  • Cache TTL cannot be negative

  • Connection pool size must be positive


from_environment(api_key_env_var="VIEWAI_API_KEY", base_url_env_var="VIEWAI_API_BASE_URL")

Create configuration from environment variables.

Parameters:

  • api_key_env_var (str): Environment variable name for API key (default: "VIEWAI_API_KEY")

  • base_url_env_var (str): Environment variable name for base URL (default: "VIEWAI_API_BASE_URL")

Returns: ClientConfiguration instance populated from environment

Raises:

  • ValueError: If required environment variables are missing

Environment Variables:

  • VIEWAI_API_KEY: API authentication key (required)

  • VIEWAI_API_BASE_URL: Base API URL (optional)

  • VIEWAI_TIMEOUT: Request timeout in seconds (optional)

  • VIEWAI_MAX_RETRIES: Max retry attempts (optional)

  • VIEWAI_LOG_LEVEL: Logging level (optional)

  • VIEWAI_DEFAULT_WORKSPACE: Default workspace (optional)

  • VIEWAI_DEFAULT_PROJECT: Default project (optional)

  • VIEWAI_DEFAULT_MODEL_ID: Default model ID (optional)

Example:

Custom Environment Variable Names:


to_dict()

Convert configuration to dictionary.

Returns: Dictionary representation of configuration

Example:

Note: API key is masked in the output for security.


Complete Configuration Example


RetryStrategy

Configurable retry strategy with exponential backoff and jitter.

Overview

The RetryStrategy class implements retry logic with exponential backoff and jitter for resilient API communication.

Initialization

Parameters:

  • max_attempts (int): Maximum number of retry attempts (default: 3)

  • initial_delay (float): Initial delay between retries in seconds (default: 1.0)

  • max_delay (float): Maximum delay between retries in seconds (default: 60.0)

  • exponential_base (float): Base for exponential backoff calculation (default: 2.0)

  • jitter (bool): Whether to add random jitter to delays (default: True)


Basic Usage


Methods

calculate_delay(attempt)

Calculate delay for a given retry attempt with exponential backoff.

Parameters:

  • attempt (int): The retry attempt number (1-indexed)

Returns: Delay in seconds before next retry (float)

Formula:

If jitter enabled, delay is randomized between 50-100% of calculated value.

Example:

Without Jitter:


should_retry(attempt, exception)

Determine if operation should be retried.

Parameters:

  • attempt (int): Current attempt number

  • exception (Exception): Exception that occurred

Returns: True if should retry, False otherwise

Example:

Note: Override this method in subclasses to implement custom retry logic based on exception type.


validate()

Validate retry strategy configuration.

Raises:

  • ValueError: If configuration is invalid

Example:

Validation Rules:

  • Max attempts must be positive

  • Initial delay must be positive

  • Max delay must be positive

  • Initial delay cannot exceed max delay

  • Exponential base must be greater than 1.0


Complete Retry Example


Custom Retry Strategy

Create a custom retry strategy by subclassing:


Configuration Best Practices

Development Configuration


Production Configuration


Testing Configuration


Environment-Based Configuration

Use environment variables for different deployment environments:


See Also

  • ViewAIClient - Main client class

  • Exceptions - Exception handling

  • Service Classes - Service documentation

Was this helpful?