Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lmstudio/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"FileHandleDict",
"FileHandleInput",
"FileType",
"LocalFileInput",
"SystemPrompt",
"SystemPromptContent",
"ToolCallRequest",
Expand Down
52 changes: 25 additions & 27 deletions tests/async/test_images_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from io import BytesIO

from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError
from lmstudio import AsyncClient, Chat, FileHandle, LMStudioServerError, LocalFileInput

from ..support import (
EXPECTED_VLM_ID,
Expand All @@ -17,59 +17,57 @@
check_sdk_error,
)

_IMAGE_DATA = IMAGE_FILEPATH.read_bytes()

_FILE_INPUT_CASES: list[tuple[str, LocalFileInput]] = [
("filesystem path", IMAGE_FILEPATH),
("bytes IO stream", BytesIO(_IMAGE_DATA)),
("raw bytes", _IMAGE_DATA),
("mutable buffer", bytearray(_IMAGE_DATA)),
("memoryview", memoryview(_IMAGE_DATA)),
]
_FILE_INPUT_CASE_IDS = [case[0] for case in _FILE_INPUT_CASES]


@pytest.mark.asyncio
@pytest.mark.lmstudio
async def test_upload_from_pathlike_async(caplog: LogCap) -> None:
@pytest.mark.parametrize(
"input_kind,file_input", _FILE_INPUT_CASES, ids=_FILE_INPUT_CASE_IDS
)
async def test_prepare_async(
caplog: LogCap, input_kind: str, file_input: LocalFileInput
) -> None:
caplog.set_level(logging.DEBUG)
async with AsyncClient() as client:
session = client.files
file = await session._prepare_file(IMAGE_FILEPATH)
file = await session._prepare_file(file_input)
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
image = await session.prepare_image(IMAGE_FILEPATH)
logging.info(f"Uploaded file from {input_kind}: {file}")
image = await session.prepare_image(file_input)
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
logging.info(f"Uploaded image from {input_kind}: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file


@pytest.mark.asyncio
@pytest.mark.lmstudio
async def test_upload_from_file_obj_async(caplog: LogCap) -> None:
async def test_prepare_from_file_obj_async(caplog: LogCap) -> None:
caplog.set_level(logging.DEBUG)
async with AsyncClient() as client:
session = client.files
with open(IMAGE_FILEPATH, "rb") as f:
file = await session._prepare_file(f)
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
logging.info(f"Uploaded file from file object: {file}")
with open(IMAGE_FILEPATH, "rb") as f:
image = await session.prepare_image(f)
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file


@pytest.mark.asyncio
@pytest.mark.lmstudio
async def test_upload_from_bytesio_async(caplog: LogCap) -> None:
caplog.set_level(logging.DEBUG)
async with AsyncClient() as client:
session = client.files
file = await session._prepare_file(BytesIO(IMAGE_FILEPATH.read_bytes()))
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
image = await session.prepare_image(BytesIO(IMAGE_FILEPATH.read_bytes()))
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
logging.info(f"Uploaded image from file object: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file

Expand Down
51 changes: 25 additions & 26 deletions tests/sync/test_images_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from io import BytesIO

from lmstudio import Client, Chat, FileHandle, LMStudioServerError
from lmstudio import Client, Chat, FileHandle, LMStudioServerError, LocalFileInput

from ..support import (
EXPECTED_VLM_ID,
Expand All @@ -24,56 +24,55 @@
check_sdk_error,
)

_IMAGE_DATA = IMAGE_FILEPATH.read_bytes()

_FILE_INPUT_CASES: list[tuple[str, LocalFileInput]] = [
("filesystem path", IMAGE_FILEPATH),
("bytes IO stream", BytesIO(_IMAGE_DATA)),
("raw bytes", _IMAGE_DATA),
("mutable buffer", bytearray(_IMAGE_DATA)),
("memoryview", memoryview(_IMAGE_DATA)),
]
_FILE_INPUT_CASE_IDS = [case[0] for case in _FILE_INPUT_CASES]


@pytest.mark.lmstudio
def test_upload_from_pathlike_sync(caplog: LogCap) -> None:
@pytest.mark.parametrize(
"input_kind,file_input", _FILE_INPUT_CASES, ids=_FILE_INPUT_CASE_IDS
)
def test_prepare_sync(
caplog: LogCap, input_kind: str, file_input: LocalFileInput
) -> None:
caplog.set_level(logging.DEBUG)
with Client() as client:
session = client.files
file = session._prepare_file(IMAGE_FILEPATH)
file = session._prepare_file(file_input)
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
image = session.prepare_image(IMAGE_FILEPATH)
logging.info(f"Uploaded file from {input_kind}: {file}")
image = session.prepare_image(file_input)
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
logging.info(f"Uploaded image from {input_kind}: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file


@pytest.mark.lmstudio
def test_upload_from_file_obj_sync(caplog: LogCap) -> None:
def test_prepare_from_file_obj_sync(caplog: LogCap) -> None:
caplog.set_level(logging.DEBUG)
with Client() as client:
session = client.files
with open(IMAGE_FILEPATH, "rb") as f:
file = session._prepare_file(f)
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
logging.info(f"Uploaded file from file object: {file}")
with open(IMAGE_FILEPATH, "rb") as f:
image = session.prepare_image(f)
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file


@pytest.mark.lmstudio
def test_upload_from_bytesio_sync(caplog: LogCap) -> None:
caplog.set_level(logging.DEBUG)
with Client() as client:
session = client.files
file = session._prepare_file(BytesIO(IMAGE_FILEPATH.read_bytes()))
assert file
assert isinstance(file, FileHandle)
logging.info(f"Uploaded file: {file}")
image = session.prepare_image(BytesIO(IMAGE_FILEPATH.read_bytes()))
assert image
assert isinstance(image, FileHandle)
logging.info(f"Uploaded image: {image}")
logging.info(f"Uploaded image from file object: {image}")
# Even with the same data uploaded, assigned identifiers should differ
assert image != file

Expand Down