Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

29.0 KB
Raw Blame
"""Verify the single-text ``embed`` public API on the shipped embedders.

The batching decorator's own correctness (batching, memoization, GPU runner) is
covered by ``python/tests/core/test_function_batching.py``. These tests verify
the thin wrapper added on top β€” that ``await embedder.embed("text")`` returns a
single ``NDArray[np.float32]`` rather than a ``list[NDArray]``.
"""

from __future__ import annotations

import asyncio
import base64
from collections import ChainMap
from datetime import timedelta
from types import SimpleNamespace
from typing import Any
from unittest.mock import AsyncMock, call, patch

import numpy as np
import pytest

pytest.importorskip("litellm", reason="litellm not installed")
# Arrives with litellm; imported through importorskip so this module skips
# rather than failing collection where neither is installed.
httpx = pytest.importorskip("httpx", reason="httpx not installed")

from litellm.exceptions import (  # noqa: E402
    APIConnectionError,
    AuthenticationError,
    Timeout,
)

import cocoindex as coco  # noqa: E402
from cocoindex.ops.litellm import LiteLLMEmbedder, _aligned_embeddings  # noqa: E402

# Note on the sleep patch target below: retry sleeps now happen inside
# cocoindex._internal.deadline via a late `asyncio.sleep` lookup. Patching
# `cocoindex.ops.litellm._asyncio.sleep` still intercepts them because
# `_asyncio` is an alias of the stdlib module object, so the patch mutates
# the same `asyncio.sleep` attribute the deadline helper reads.
from cocoindex.resources.embedder import Embedder  # noqa: E402


class _FakeHTTPError(Exception):
    def __init__(self, status_code: int, message: str | None = None) -> None:
        self.status_code = status_code
        super().__init__(message or f"HTTP {status_code}")


@pytest.mark.asyncio
async def test_litellm_embedder_single_text_api() -> None:
    # Patch litellm.aembedding to return a deterministic 4-d vector.
    fake_response = type(
        "R",
        (),
        {"data": [{"embedding": [0.1, 0.2, 0.3, 0.4]}]},
    )()
    embedder = LiteLLMEmbedder("fake-model")

    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(return_value=fake_response),
    ) as mocked:
        vec = await embedder.embed("hello")

    # Single NDArray, not a list
    assert isinstance(vec, np.ndarray)
    assert vec.dtype == np.float32
    assert vec.shape == (4,)
    # Exactly one underlying call with our single text in the batch
    mocked.assert_called_once()
    call_kwargs = mocked.call_args.kwargs
    assert call_kwargs["input"] == ["hello"]


def test_litellm_embedder_satisfies_embedder_protocol() -> None:
    embedder = LiteLLMEmbedder("fake-model")
    assert isinstance(embedder, Embedder)


@pytest.mark.parametrize(
    "model, expects_float_hint",
    [
        ("text-embedding-3-small", True),
        ("openai/text-embedding-3-small", True),
        ("voyage/voyage-code-3", False),
        ("voyage/voyage-3-large", False),
        ("bedrock/amazon.titan-embed-text-v2:0", False),
    ],
)
@pytest.mark.asyncio
async def test_litellm_encoding_format_gated_by_provider(
    model: str, expects_float_hint: bool
) -> None:
    fake_response = type(
        "R",
        (),
        {"data": [{"embedding": [0.1, 0.2, 0.3, 0.4]}]},
    )()
    embedder = LiteLLMEmbedder(model)

    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(return_value=fake_response),
    ) as mocked:
        await embedder.embed("hello")

    call_kwargs = mocked.call_args.kwargs
    if expects_float_hint:
        assert call_kwargs.get("encoding_format") == "float"
        assert call_kwargs.get("drop_params") is True
    else:
        assert "encoding_format" not in call_kwargs
        assert "drop_params" not in call_kwargs


@pytest.mark.asyncio
async def test_litellm_embedder_retries_transient_embedding_errors() -> None:
    fake_response = type(
        "R",
        (),
        {"data": [{"embedding": [0.1, 0.2, 0.3, 0.4]}]},
    )()
    embedder = LiteLLMEmbedder("fake-model")
    mocked_embedding = AsyncMock(
        side_effect=[
            _FakeHTTPError(429),
            _FakeHTTPError(503),
            fake_response,
        ]
    )

    with (
        patch("cocoindex.ops.litellm.litellm.aembedding", new=mocked_embedding),
        patch("cocoindex.ops.litellm._asyncio.sleep", new=AsyncMock()) as sleep,
    ):
        vec = await embedder.embed("hello")

    assert vec.tolist() == pytest.approx([0.1, 0.2, 0.3, 0.4])
    assert mocked_embedding.call_count == 3
    sleep.assert_has_awaits([call(1.0), call(2.0)])


@pytest.mark.asyncio
async def test_litellm_embedder_does_not_retry_non_transient_embedding_errors() -> None:
    embedder = LiteLLMEmbedder("fake-model")
    mocked_embedding = AsyncMock(side_effect=_FakeHTTPError(400))

    with (
        patch("cocoindex.ops.litellm.litellm.aembedding", new=mocked_embedding),
        patch("cocoindex.ops.litellm._asyncio.sleep", new=AsyncMock()) as sleep,
    ):
        with pytest.raises(_FakeHTTPError):
            await embedder.embed("hello")

    mocked_embedding.assert_awaited_once()
    sleep.assert_not_called()


# ============================================================================
# RetryWithSmallerBatch: over-limit batches are split, global errors are not
# ============================================================================


def _fake_embedding_response(texts: list[str]) -> Any:
    # Embedding derived from the text so tests can verify item alignment.
    return SimpleNamespace(data=[{"embedding": [float(len(t))]} for t in texts])


@pytest.mark.asyncio
async def test_litellm_embedder_splits_oversized_batch() -> None:
    """A provider batch-size rejection splits the batch; every text succeeds
    with its own embedding (results stay aligned through the split)."""
    started = asyncio.Event()
    release = asyncio.Event()
    call_inputs: list[list[str]] = []

    async def fake_aembedding(*, model: str, input: list[str], **kwargs: Any) -> Any:
        call_inputs.append(list(input))
        if len(call_inputs) == 1:
            started.set()
            await release.wait()
        if len(input) > 2:
            raise _FakeHTTPError(400, "TOO_MANY_TOKENS_IN_BATCH")
        return _fake_embedding_response(input)

    embedder = LiteLLMEmbedder("fake-model")
    with patch("cocoindex.ops.litellm.litellm.aembedding", new=fake_aembedding):
        # First call runs inline and blocks, so the next four coalesce into
        # one batch of 4 β€” which the fake provider rejects.
        task0 = asyncio.create_task(embedder.embed("a"))
        await started.wait()
        texts = ["bb", "ccc", "dddd", "eeeee"]
        tasks = [asyncio.create_task(embedder.embed(t)) for t in texts]
        await asyncio.sleep(0.05)  # let them enqueue behind the inline call
        release.set()
        results = await asyncio.gather(task0, *tasks)

    for text, vec in zip(["a", *texts], results):
        assert vec.tolist() == [float(len(text))]
    # Inline [1], rejected [4], then the two halves of 2.
    assert [len(c) for c in call_inputs[:2]] == [1, 4]
    assert sorted(len(c) for c in call_inputs[2:]) == [2, 2]


@pytest.mark.asyncio
async def test_litellm_embedder_raises_retry_with_smaller_batch_on_400() -> None:
    """A non-retryable, non-global error on a multi-text batch becomes the
    RetryWithSmallerBatch signal (with the original error as its cause)."""
    embedder = LiteLLMEmbedder("fake-model")
    provider_error = _FakeHTTPError(400, "batch exceeds maximum context length")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(side_effect=provider_error),
    ):
        with pytest.raises(coco.RetryWithSmallerBatch) as exc_info:
            await embedder._embed._execute_orig_async_fn(["a", "b"])
    assert exc_info.value.__cause__ is provider_error


@pytest.mark.asyncio
async def test_litellm_embedder_single_text_error_surfaces_original() -> None:
    """With one text there is nothing to split β€” the caller sees the original
    provider error (the engine unwraps the size-1 signal)."""
    embedder = LiteLLMEmbedder("fake-model")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(side_effect=_FakeHTTPError(400, "input too large")),
    ):
        with pytest.raises(_FakeHTTPError):
            await embedder.embed("only")


def _raised_while_handling(error: Exception, context: Exception) -> Exception:
    """``error`` as litellm raises it from inside an ``except`` block handling
    ``context``: the implicit ``__context__`` is set, ``__cause__`` is not."""
    error.__context__ = context
    return error


@pytest.mark.parametrize(
    ("error", "expected_calls"),
    [
        # litellm.Timeout is terminal even though it reports HTTP 408 and
        # descends from openai's APIConnectionError β€” both of which the
        # classification would otherwise treat as retryable.
        pytest.param(
            Timeout(message="too slow", model="fake-model", llm_provider="openai"),
            1,
            id="litellm-timeout",
        ),
        # A refused/reset connection is a fast failure: still retried.
        pytest.param(
            APIConnectionError(
                message="connection refused", model="fake-model", llm_provider="openai"
            ),
            3,
            id="litellm-connection-error",
        ),
        # The same wrapper around a real transport failure keeps retrying:
        # the local-failure rule matches only a wrapped ValueError/TypeError.
        pytest.param(
            _raised_while_handling(
                APIConnectionError(
                    message="connection refused",
                    model="fake-model",
                    llm_provider="openai",
                ),
                httpx.ConnectError("refused"),
            ),
            3,
            id="litellm-connection-error-wrapping-transport-error",
        ),
        # httpx timeouts subclass neither TimeoutError nor litellm.Timeout;
        # they are matched by name.
        pytest.param(httpx.ReadTimeout("stalled"), 1, id="httpx-read-timeout"),
        pytest.param(httpx.PoolTimeout("no free slot"), 1, id="httpx-pool-timeout"),
        pytest.param(httpx.ConnectError("refused"), 3, id="httpx-connect-error"),
    ],
)
@pytest.mark.asyncio
async def test_litellm_embedder_retries_fast_failures_but_not_timeouts(
    error: Exception, expected_calls: int
) -> None:
    fake_response = type("R", (), {"data": [{"embedding": [0.1]}]})()
    embedder = LiteLLMEmbedder("fake-model")
    mocked_embedding = AsyncMock(side_effect=[error, error, fake_response])

    with (
        patch("cocoindex.ops.litellm.litellm.aembedding", new=mocked_embedding),
        patch("cocoindex.ops.litellm._asyncio.sleep", new=AsyncMock()),
    ):
        if expected_calls == 1:
            with pytest.raises(type(error)):
                await embedder.embed("hello")
        else:
            await embedder.embed("hello")

    assert mocked_embedding.await_count == expected_calls


@pytest.mark.asyncio
async def test_litellm_embedder_does_not_split_timeouts() -> None:
    """A multi-text batch that times out propagates the timeout; splitting
    would only re-spend a budget the backend already failed to meet."""
    embedder = LiteLLMEmbedder("fake-model")
    timeout_error = Timeout(
        message="too slow", model="fake-model", llm_provider="openai"
    )
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(side_effect=timeout_error),
    ):
        with pytest.raises(Timeout):
            await embedder._embed._execute_orig_async_fn(["a", "b"])


@pytest.mark.asyncio
async def test_litellm_embedder_does_not_split_global_errors() -> None:
    """Credential / auth errors can't be fixed by splitting β€” they propagate
    as-is even for multi-text batches."""
    embedder = LiteLLMEmbedder("fake-model")
    auth_error = AuthenticationError(
        message="access denied", llm_provider="openai", model="fake-model"
    )
    for error in (
        _FakeHTTPError(401, "Invalid API key provided"),
        auth_error,
    ):
        with patch(
            "cocoindex.ops.litellm.litellm.aembedding",
            new=AsyncMock(side_effect=error),
        ):
            with pytest.raises(type(error)):
                await embedder._embed._execute_orig_async_fn(["a", "b"])


@pytest.mark.asyncio
async def test_litellm_embedder_does_not_retry_missing_credentials_server_error() -> (
    None
):
    embedder = LiteLLMEmbedder("fake-model")
    missing_credentials_error = _FakeHTTPError(
        500,
        "litellm.InternalServerError: OpenAIException - Missing credentials. "
        "Please pass an `api_key`, `workload_identity`, `admin_api_key`, or set "
        "the `OPENAI_API_KEY` or `OPENAI_ADMIN_KEY` environment variable.",
    )
    mocked_embedding = AsyncMock(side_effect=missing_credentials_error)

    with (
        patch("cocoindex.ops.litellm.litellm.aembedding", new=mocked_embedding),
        patch("cocoindex.ops.litellm._asyncio.sleep", new=AsyncMock()) as sleep,
    ):
        with pytest.raises(_FakeHTTPError):
            await embedder.embed("hello")

    mocked_embedding.assert_awaited_once()
    sleep.assert_not_called()


# ============================================================================
# A request litellm refuses to build fails at once, and only its own input
# ============================================================================


def _raise_local_request_failure(local_error: Exception) -> None:
    """Fail the way litellm does when it cannot build a request: the local
    error is re-raised inside an ``except`` block as ``APIConnectionError``
    β€” implicit ``__context__``, and a ``status_code`` of 500 although no
    request was sent."""
    try:
        raise local_error
    except Exception:
        raise APIConnectionError(
            message=str(local_error), model="fake-model", llm_provider="gemini"
        )


@pytest.mark.parametrize(
    "local_error",
    [
        # litellm's own request transformation rejecting an input.
        pytest.param(
            ValueError("Unsupported MIME type for embedding: application/octet-stream"),
            id="value-error",
        ),
        # A dependency litellm imports at call time and the install lacks.
        pytest.param(ModuleNotFoundError("No module named 'mcp'"), id="import-error"),
    ],
)
@pytest.mark.asyncio
async def test_litellm_embedder_does_not_retry_local_request_failures(
    local_error: Exception,
) -> None:
    embedder = LiteLLMEmbedder("fake-model")
    mocked_embedding = AsyncMock(
        side_effect=lambda **kwargs: _raise_local_request_failure(local_error)
    )

    with (
        patch("cocoindex.ops.litellm.litellm.aembedding", new=mocked_embedding),
        patch("cocoindex.ops.litellm._asyncio.sleep", new=AsyncMock()) as sleep,
    ):
        with pytest.raises(APIConnectionError, match=str(local_error)) as exc_info:
            await embedder.embed("hello")

    # The wrapper's status code is a constant, not an HTTP result: the rule
    # that stops the retry has to see past it to the wrapped local error.
    assert exc_info.value.status_code == 500
    mocked_embedding.assert_awaited_once()
    sleep.assert_not_called()


@pytest.mark.asyncio
async def test_litellm_embedder_fails_only_the_input_litellm_rejects() -> None:
    """One input litellm refuses to send fails alone: the batch splits around
    it, and every other text keeps its embedding."""
    started = asyncio.Event()
    release = asyncio.Event()
    call_inputs: list[list[str]] = []

    async def fake_aembedding(*, model: str, input: list[str], **kwargs: Any) -> Any:
        call_inputs.append(list(input))
        if len(call_inputs) == 1:
            started.set()
            await release.wait()
        for text in input:
            if text.startswith("bad"):
                _raise_local_request_failure(ValueError(f"rejected {text}"))
        return _fake_embedding_response(input)

    # A short bound so that, were the failure retried, the test ends with a
    # deadline error instead of hanging for the default ten minutes.
    embedder = LiteLLMEmbedder("fake-model", timeout=timedelta(seconds=2))
    with patch("cocoindex.ops.litellm.litellm.aembedding", new=fake_aembedding):
        # First call runs inline and blocks, so the next four coalesce into
        # one batch of 4, one of which litellm rejects.
        task0 = asyncio.create_task(embedder.embed("a"))
        await started.wait()
        texts = ["bb", "bad", "dddd", "eeeee"]
        tasks = [asyncio.create_task(embedder.embed(t)) for t in texts]
        await asyncio.sleep(0.05)  # let them enqueue behind the inline call
        release.set()
        results = await asyncio.gather(task0, *tasks, return_exceptions=True)

    first, *rest = results
    assert isinstance(first, np.ndarray) and first.tolist() == [1.0]
    by_text = dict(zip(texts, rest))
    assert isinstance(by_text["bad"], APIConnectionError)
    for text in ("bb", "dddd", "eeeee"):
        vec = by_text[text]
        assert isinstance(vec, np.ndarray) and vec.tolist() == [float(len(text))]
    # The rejected input's last attempt was on its own.
    assert [c for c in call_inputs if "bad" in c][-1] == ["bad"]


# ============================================================================
# A text litellm would read as a media reference is sent as text
# ============================================================================


@pytest.mark.parametrize(
    ("text", "sent"),
    [
        pytest.param(
            "data:application/octet-stream;base64,AACAvw==",
            " data:application/octet-stream;base64,AACAvw==",
            id="inline-data-url",
        ),
        pytest.param(
            "data:image/png;base64,iVBORw0KGgo=",
            " data:image/png;base64,iVBORw0KGgo=",
            id="inline-image",
        ),
        pytest.param(
            "gs://bucket/build/output.txt",
            " gs://bucket/build/output.txt",
            id="gcs-url",
        ),
        pytest.param(
            "files/README.md\nfiles/setup.py",
            " files/README.md\nfiles/setup.py",
            id="file-reference",
        ),
        pytest.param(
            "data:text/plain,hello", "data:text/plain,hello", id="data-url-no-base64"
        ),
        pytest.param("see gs://bucket/x", "see gs://bucket/x", id="prefix-mid-text"),
        pytest.param("hello", "hello", id="plain"),
    ],
)
@pytest.mark.asyncio
async def test_litellm_embedder_escapes_media_references(text: str, sent: str) -> None:
    """litellm reads a text that starts like a data URL, a GCS object, or a
    file reference as media to embed; a leading space keeps it text."""
    embedder = LiteLLMEmbedder("fake-model")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding",
        new=AsyncMock(return_value=_fake_embedding_response([sent])),
    ) as mocked:
        await embedder._embed._execute_orig_async_fn([text])
    assert mocked.call_args.kwargs["input"] == [sent]


# ============================================================================
# A base64 response payload is decoded to float32 before numpy sees it
# ============================================================================


def _base64_embedding(vec: list[float]) -> str:
    """A vector as a provider returns it for ``encoding_format="base64"``: the
    raw ``float32`` bytes, base64-encoded. ``Embedding.embedding`` is typed
    ``list[float] | str``, so this is the shape that arrives as a ``str``.

    When ``encoding_format`` is given, the OpenAI SDK skips its own base64
    decode, so this payload reaches ``_aligned_embeddings`` undecoded."""
    return base64.b64encode(np.array(vec, dtype=np.float32).tobytes()).decode()


def test_aligned_embeddings_decodes_base64() -> None:
    """A base64 payload decodes to a ``float32`` vector instead of raising in
    ``np.array``. The ``list[float]`` form is untouched."""
    vec = [1.0, 2.0, 3.0]
    data = [{"index": 0, "embedding": _base64_embedding(vec)}]
    (out,) = _aligned_embeddings(data, 1)
    assert out.dtype == np.float32
    assert out.tolist() == vec


@pytest.mark.asyncio
async def test_litellm_embedder_decodes_base64_batch() -> None:
    """A batch of base64 payloads decodes to ``float32`` vectors, not ``str``s."""
    vecs = [[0.5, 1.5], [2.5, 3.5]]
    response = SimpleNamespace(
        data=[
            {"index": i, "embedding": _base64_embedding(v)} for i, v in enumerate(vecs)
        ]
    )
    embedder = LiteLLMEmbedder("fake-model", encoding_format="base64")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding", new=AsyncMock(return_value=response)
    ):
        out = await embedder._embed._execute_orig_async_fn(["a", "b"])
    assert [v.dtype for v in out] == [np.float32, np.float32]
    assert [v.tolist() for v in out] == vecs


# ============================================================================
# The schema probe sizes the vector from the decoded payload
# ============================================================================


@pytest.mark.asyncio
async def test_litellm_embedder_schema_size_from_base64_probe() -> None:
    """Sizing the column off the base64 string would advertise a width as long
    as the encoded payload β€” 16 for a 3-d vector β€” and every insert of a real
    vector would then fail."""
    vec = [1.0, 2.0, 3.0]
    response = SimpleNamespace(data=[{"index": 0, "embedding": _base64_embedding(vec)}])
    embedder = LiteLLMEmbedder("fake-model", encoding_format="base64")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding", new=AsyncMock(return_value=response)
    ) as mocked:
        schema = await embedder.__coco_vector_schema__()
    assert schema.size == len(vec)
    assert mocked.call_args.kwargs["encoding_format"] == "base64"


# ============================================================================
# `encoding_format="base64"` refuses payload options the decode cannot honor
# ============================================================================


@pytest.mark.parametrize(
    "kwargs",
    [
        pytest.param(
            {"encoding_format": "base64", "embed_dtype": "float16"},
            id="embed-dtype",
        ),
        pytest.param(
            {"encoding_format": "base64", "extra_body": {"embed_dtype": "float16"}},
            id="embed-dtype-extra-body",
        ),
        pytest.param(
            {"encoding_format": "base64", "output_dtype": "int8"}, id="output-dtype"
        ),
        pytest.param(
            {"extra_body": {"encoding_format": "base64", "embed_dtype": "float16"}},
            id="encoding-format-extra-body",
        ),
        # Both spellings reach the provider, and which one wins differs per
        # key, so a pair that disagrees is refused rather than resolved.
        pytest.param(
            {
                "encoding_format": "base64",
                "embed_dtype": "float32",
                "extra_body": {"embed_dtype": "float16"},
            },
            id="conflicting-sources",
        ),
        # litellm accepts any mapping for `extra_body`, not just a dict.
        pytest.param(
            {
                "encoding_format": "base64",
                "extra_body": ChainMap({"embed_dtype": "float16"}),
            },
            id="embed-dtype-mapping",
        ),
        # vLLM's `endianness` applies to base64 payloads: big-endian bytes read
        # in native order have the right length and the wrong values.
        pytest.param(
            {"encoding_format": "base64", "endianness": "big"},
            id="endianness",
        ),
        pytest.param(
            {"encoding_format": "base64", "extra_body": {"endianness": "big"}},
            id="endianness-extra-body",
        ),
    ],
)
def test_litellm_embedder_rejects_base64_payload_options_it_cannot_decode(
    kwargs: dict[str, Any],
) -> None:
    """litellm forwards the options that alter the payload β€” at the top level
    or through `extra_body` β€” and the base64 opt-in itself can arrive either
    way. Those bytes decode as float32 to a shorter vector of meaningless
    values, or, for a big-endian payload, to the right length of wrong values.
    Neither raises on its own."""
    with pytest.raises(ValueError, match="float32"):
        LiteLLMEmbedder("fake-model", **kwargs)


@pytest.mark.parametrize(
    "kwargs",
    [
        pytest.param(
            {"encoding_format": "base64", "embed_dtype": "float32"},
            id="embed-dtype",
        ),
        pytest.param(
            {"encoding_format": "base64", "extra_body": {"embed_dtype": "float32"}},
            id="embed-dtype-extra-body",
        ),
        pytest.param(
            {"encoding_format": "base64", "output_dtype": "float"}, id="output-dtype"
        ),
        pytest.param(
            {"extra_body": {"encoding_format": "base64", "embed_dtype": "float32"}},
            id="encoding-format-extra-body",
        ),
        pytest.param(
            {"encoding_format": "base64", "endianness": "native"},
            id="endianness-native",
        ),
        pytest.param(
            {"encoding_format": "base64", "endianness": "little"},
            id="endianness-little",
        ),
    ],
)
def test_litellm_embedder_accepts_base64_payload_options_it_can_decode(
    kwargs: dict[str, Any],
) -> None:
    """The options the decode can honor β€” float payloads, native/little
    endianness β€” stay valid."""
    LiteLLMEmbedder("fake-model", **kwargs)


@pytest.mark.parametrize("output_dtype", ["int8", "uint8", "binary", "ubinary"])
def test_litellm_embedder_rejects_quantized_voyage_output(
    output_dtype: str,
) -> None:
    """With base64, Voyage's quantized dtypes decode as float32 to a shorter
    vector of meaningless values. Without base64 they arrive as plain lists,
    which the guard deliberately leaves alone."""
    with pytest.raises(ValueError, match="output_dtype"):
        LiteLLMEmbedder(
            "voyage/voyage-3", encoding_format="base64", output_dtype=output_dtype
        )


def test_litellm_embedder_accepts_float_voyage_output() -> None:
    LiteLLMEmbedder("voyage/voyage-3", output_dtype="float")


@pytest.mark.parametrize("model", ["fake-model", "voyage/voyage-3"])
def test_litellm_embedder_leaves_non_base64_output_dtype_alone(model: str) -> None:
    """Without base64 the payload arrives as a list, and Voyage's `int8` values
    come back as full-length integers that float32 holds exactly, so the guard
    must not refuse them."""
    LiteLLMEmbedder(model, output_dtype="int8")


# ============================================================================
# Response items are aligned to inputs by `index` when the provider sends one
# ============================================================================


def test_aligned_embeddings_positional_when_no_index() -> None:
    # A missing key and an explicit None are both "no index".
    data = [{"embedding": [1.0]}, {"index": None, "embedding": [2.0]}]
    assert [v.tolist() for v in _aligned_embeddings(data, 2)] == [[1.0], [2.0]]


def test_aligned_embeddings_reorders_by_index() -> None:
    data = [
        {"index": 2, "embedding": [2.0]},
        {"index": 0, "embedding": [0.0]},
        {"index": 1, "embedding": [1.0]},
    ]
    assert [v.tolist() for v in _aligned_embeddings(data, 3)] == [[0.0], [1.0], [2.0]]


@pytest.mark.asyncio
async def test_litellm_embedder_aligns_batch_by_index() -> None:
    """The batch body maps a provider response that arrives out of order
    back onto the input texts by ``index``."""
    texts = ["a", "bb", "ccc"]
    response = SimpleNamespace(
        data=[{"index": i, "embedding": [float(len(texts[i]))]} for i in (2, 0, 1)]
    )
    embedder = LiteLLMEmbedder("fake-model")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding", new=AsyncMock(return_value=response)
    ):
        vecs = await embedder._embed._execute_orig_async_fn(texts)
    assert [v.tolist() for v in vecs] == [[1.0], [2.0], [3.0]]


@pytest.mark.parametrize(
    ("indices", "n", "match"),
    [
        pytest.param([0, None], 2, "with and without", id="partial"),
        pytest.param([0, 0], 2, "not a permutation", id="duplicate"),
        pytest.param([0, 1], 3, "2 items for 3 inputs", id="missing"),
        pytest.param([-1, 0], 2, "not a permutation", id="negative"),
        pytest.param([0, 2], 2, "not a permutation", id="out-of-range"),
        pytest.param([0, "1"], 2, "not a permutation", id="non-int"),
    ],
)
def test_aligned_embeddings_rejects_bad_indices(
    indices: list[Any], n: int, match: str
) -> None:
    data = [{"index": i, "embedding": [0.0]} for i in indices]
    with pytest.raises(RuntimeError, match=match):
        _aligned_embeddings(data, n)


@pytest.mark.asyncio
async def test_litellm_embedder_bad_index_fails_whole_batch() -> None:
    """A malformed index set is a provider bug: it surfaces as-is rather than
    as RetryWithSmallerBatch, since splitting would only hide the misorder."""
    response = SimpleNamespace(data=[{"index": 0, "embedding": [0.0]}] * 2)
    embedder = LiteLLMEmbedder("fake-model")
    with patch(
        "cocoindex.ops.litellm.litellm.aembedding", new=AsyncMock(return_value=response)
    ):
        with pytest.raises(RuntimeError, match="not a permutation"):
            await embedder._embed._execute_orig_async_fn(["a", "b"])