Skip to content

[ENH]: Improve compatibility with array-like objects implementing __array__ (e.g. MLX arrays) #31149

Closed

Description

@lzqlzzq

Problem

Minimal Example

import mlx.core as mx
import matplotlib.pyplot as plt

x = mx.linspace(0, 10, 100)
y = mx.sin(x)

plt.plot(x, y)
plt.show()

Expected: Plot renders.
Actual: RuntimeError: Item size 2 for PEP 3118 buffer format string B does not match the dtype B item size 1.

Proposed solution

We may add normalization step before actually forward into gca().xxx

def _asarray(obj):
    if isinstance(obj, str):
        return obj
    try:
        arr = np.asarray(obj)
    except BaseException:
        return obj  # Forward original obj, maybe right?
    return arr

Activity

  1. timhoffm commented on Feb 13, 2026

    @timhoffm
    Member

    Unfortunately, we cannot unconditionally call np.asarray() (__array__) on objects as that can break existing units handling. The workaround so far is to whitelist array types that we support. The relevant discussion is in #25887.

    Relevant code:

    def _unpack_to_numpy(x):
    """Internal helper to extract data from e.g. pandas and xarray objects."""
    if isinstance(x, np.ndarray):
    # If numpy, return directly
    return x
    if hasattr(x, 'to_numpy'):
    # Assume that any to_numpy() method actually returns a numpy array
    return x.to_numpy()
    if hasattr(x, 'values'):
    xtmp = x.values
    # For example a dict has a 'values' attribute, but it is not a property
    # so in this case we do not want to return a function
    if isinstance(xtmp, np.ndarray):
    return xtmp
    if _is_torch_array(x) or _is_jax_array(x) or _is_tensorflow_array(x):
    # using np.asarray() instead of explicitly __array__(), as the latter is
    # only _one_ of many methods, and it's the last resort, see also
    # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
    # therefore, let arrays do better if they can
    xtmp = np.asarray(x)
    # In case np.asarray method does not return a numpy array in future
    if isinstance(xtmp, np.ndarray):
    return xtmp
    return x

    I think the straight way forward is to explicitly add mlx support.

    Alternative: Could we detect units and skip their conversion?

  2. jklymak commented on Feb 13, 2026

    @jklymak
    Member

    Is there really not a general solution? If we don't recognize the type we run as array and then check the type of the first element?

  3. lzqlzzq commented on Feb 13, 2026

    @lzqlzzq
    ContributorAuthor

    Unfortunately, we cannot unconditionally call np.asarray() (__array__) on objects as that can break existing units handling. The workaround so far is to whitelist array types that we support. The relevant discussion is in #25887.

    Relevant code:

    matplotlib/lib/matplotlib/cbook.py

    Lines 2443 to 2467 in c4b9963

    def _unpack_to_numpy(x):
    """Internal helper to extract data from e.g. pandas and xarray objects."""
    if isinstance(x, np.ndarray):
    # If numpy, return directly
    return x
    if hasattr(x, 'to_numpy'):
    # Assume that any to_numpy() method actually returns a numpy array
    return x.to_numpy()
    if hasattr(x, 'values'):
    xtmp = x.values
    # For example a dict has a 'values' attribute, but it is not a property
    # so in this case we do not want to return a function
    if isinstance(xtmp, np.ndarray):
    return xtmp
    if _is_torch_array(x) or _is_jax_array(x) or _is_tensorflow_array(x):
    # using np.asarray() instead of explicitly array(), as the latter is
    # only one of many methods, and it's the last resort, see also
    # https://numpy.org/devdocs/user/basics.interoperability.html#using-arbitrary-objects-in-numpy
    # therefore, let arrays do better if they can
    xtmp = np.asarray(x)

         # In case np.asarray method does not return a numpy array in future 
         if isinstance(xtmp, np.ndarray): 
             return xtmp 
     return x 
    

    I think the straight way forward is to explicitly add mlx support.

    Alternative: Could we detect units and skip their conversion?

    That makes sense. I made a PR as the workaround in #25887. It plotted mlx.core.array successfully.

  4. lzqlzzq commented on Feb 13, 2026

    @lzqlzzq
    ContributorAuthor

    Is there really not a general solution? If we don't recognize the type we run as array and then check the type of the first element?

    I think let it crash if not recognized is right. Array-like is not first class civilian in python for now. There is no PEP or RFC on this. We cannot rely on these unstable implementation, especially for Apple. Apple likes get fancy. But some helper would be convenient.

  5. QuLogic commented on Feb 13, 2026

    @QuLogic
    Member

    It seems like this is very specifically about the 16-bit float arrays; see the first Note here: https://ml-explore.github.io/mlx/build/html/usage/numpy.html

  6. lzqlzzq commented on Feb 13, 2026

    @lzqlzzq
    ContributorAuthor

    It seems like this is very specifically about the 16-bit float arrays; see the first Note here: https://ml-explore.github.io/mlx/build/html/usage/numpy.html

    Yes. I also comes up with these questions. How to deal with different devices(apple is unified memory so nothing with this, but cuda or ROCm?), and dtypes.

  7. jklymak commented on Feb 13, 2026

    @jklymak
    Member

    If np.asarray doesn't return a numpy array of numpy dtypes, then I think we need to leave the conversion to the user?

  8. lzqlzzq commented on Feb 14, 2026

    @lzqlzzq
    ContributorAuthor

    If np.asarray doesn't return a numpy array of numpy dtypes, then I think we need to leave the conversion to the user?

    It would return only if not bfloat16. I used this script for testing numpy compatibility:

    import mlx.core as mx
    
    dtypes = [name for name in dir(mx)
              if isinstance(getattr(mx, name), mx.Dtype)]
    
    print(dtypes)
    
    
    import numpy as np
    import traceback
    
    for dt in dtypes:
        arr = mx.ones([10], dtype=eval('mx.' + dt))
        try:
            nparr = np.asarray(arr)
        except Exception as e:
            print(dt, ':', e)
            print(traceback.format_exc())
    
  9. added this to the v3.11.0 milestone on Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions