From ac63c614c26b5d7509ceb67ec4d14de21812e926 Mon Sep 17 00:00:00 2001 From: "Abdulazeez A. Noaman" <42905848+HostX0@users.noreply.github.com> Date: Fri, 21 Aug 2026 16:01:24 +0300 Subject: [PATCH] fix: decode base64 embeddings as little-endian floats --- src/openai/lib/_parsing/_embeddings.py | 9 +++++-- tests/lib/test_embeddings.py | 37 +++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/openai/lib/_parsing/_embeddings.py b/src/openai/lib/_parsing/_embeddings.py index 5b03068bcd..313d0756ba 100644 --- a/src/openai/lib/_parsing/_embeddings.py +++ b/src/openai/lib/_parsing/_embeddings.py @@ -2,6 +2,7 @@ import array import base64 +import sys from typing import cast from ..._types import Omit, NotGiven @@ -28,12 +29,16 @@ def parse_embedding_response( data = cast(object, embedding.embedding) if not isinstance(data, str): continue + decoded = base64.b64decode(data) if not use_numpy: # use array for base64 optimisation - embedding.embedding = array.array("f", base64.b64decode(data)).tolist() + floats = array.array("f", decoded) + if sys.byteorder == "big": + floats.byteswap() + embedding.embedding = floats.tolist() else: embedding.embedding = np.frombuffer( # type: ignore[no-untyped-call] - base64.b64decode(data), dtype="float32" + decoded, dtype=" bool: assert embeddings_parser.parse_embedding_response(response, encoding_format=omit) is response +def test_stdlib_decoder_swaps_big_endian_values(monkeypatch: pytest.MonkeyPatch) -> None: + response = make_response(ENCODED) + decoded = base64.b64decode(ENCODED) + + class Floats: + swapped = False + + def byteswap(self) -> None: + self.swapped = True + + def tolist(self) -> list[float]: + assert self.swapped + return VALUES + + floats = Floats() + + def make_array(typecode: str, initializer: bytes) -> Floats: + assert typecode == "f" + assert initializer == decoded + return floats + + monkeypatch.setattr(embeddings_parser, "has_numpy", lambda: False) + monkeypatch.setattr(embeddings_parser, "sys", SimpleNamespace(byteorder="big")) + monkeypatch.setattr(embeddings_parser.array, "array", make_array) + + parsed = embeddings_parser.parse_embedding_response(response, encoding_format=omit) + + assert parsed.data[0].embedding == VALUES + + @pytest.mark.parametrize("encoding_format", ["float", "base64", None]) @pytest.mark.parametrize("vectors", [(ENCODED,), ("abc",), ()], ids=["encoded", "invalid", "empty"]) def test_explicit_format_is_untouched(