Exceptions#

Exception classes for librosa

Librosa provides a structured exception hierarchy rooted in LibrosaError. Exceptions raised by Librosa inherit from both LibrosaError and relevant Python standard library exception classes where appropriate, allowing callers to catch errors using either Librosa-specific classes or familiar built-in exception types.

Exception Hierarchy#

        treeView-beta
LibrosaError
├── ParameterError
│   ├── ParameterValueError ## (also inherits ValueError)
│   │   └── ParameterShapeError
│   ├── ParameterTypeError ## (also inherits TypeError)
│   └── ParameterKeyError ## (also inherits KeyError)
├── StreamError
│   ├── StreamClosedError ## (also inherits RuntimeError)
│   ├── StreamBufferError ## (also inherits BufferError)
│   └── StreamTimeoutError ## (also inherits TimeoutError, queue.Empty)
└── LibrosaNotImplementedError ## (also inherits NotImplementedError)
    

Usage Guidance#

  • Catching specific validation failures: Use specific parameter exception classes to respond to targeted errors:

    try:
        D = librosa.stft(y, n_fft=n_fft, hop_length=hop_length)
    except librosa.ParameterShapeError:
        # Handle array shape, dimensionality, or channel mismatch
        ...
    except librosa.ParameterValueError:
        # Handle out-of-range numerical parameters or invalid options
        ...
    except librosa.ParameterTypeError:
        # Handle incorrect argument types
        ...
    
  • Interoperability with standard library exceptions: Because specific parameter exceptions inherit from standard built-ins (ValueError, TypeError, KeyError), existing exception handlers in user code will catch them naturally:

    try:
        chroma = librosa.feature.chroma_stft(y=y, sr=sr)
    except ValueError:
        # Catches both ParameterValueError and ParameterShapeError
        ...
    
  • Catching all parameter validation errors: Catch ParameterError to intercept any parameter or input validation error regardless of subtype. Existing code using except librosa.ParameterError: continues to work unchanged.

  • Catching any Librosa error: Catch LibrosaError as a top-level catch-all for any exception originating within Librosa.

  • Upstream and backend errors: Exceptions originating in underlying backend libraries (such as soundfile, pooch, or network errors from requests) are allowed to propagate directly without interception.

LibrosaError

The root librosa exception class

ParameterError

Base exception class for mal-formed inputs or parameter validation errors

ParameterValueError

Exception class for invalid parameter values

ParameterTypeError

Exception class for parameter type mismatches

ParameterShapeError

Exception class for array shape, dimension, or channel mismatches

ParameterKeyError

Exception class for invalid lookup keys, catalog entries, or note/interval names

StreamError

Base exception class for streaming runtime errors

StreamClosedError

Exception raised when an operation is attempted on a closed stream bridge

StreamBufferError

Exception raised when a stream buffer overflows under the error policy

StreamTimeoutError

Exception raised when waiting for a stream buffer chunk times out

LibrosaNotImplementedError

Exception class for abstract or unimplemented interface methods