Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d12e757

Browse files
ArEnScncoghlan
andauthored
Feature: accept any buffer instance when processing file data (#148)
- Change _get_file_details() to return Buffer instead of bytes - Add special handling for memoryview inputs to avoid unnecessary conversion - Support all buffer protocol objects (bytes, bytearray, memoryview, array.array, etc.) - Update type annotations throughout to use Buffer type from typing_extensions - Improve error messages to mention "data buffer" instead of just "bytes" This change reduces memory overhead when working with buffer protocol objects by preserving memoryview objects instead of converting them to bytes unnecessarily. Closes #46 Co-authored-by: Alyssa Coghlan <[email protected]>
1 parent 3a81ccb commit d12e757

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/lmstudio/history.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from typing_extensions import (
2626
# Native in 3.11+
2727
Self,
28+
# Native in Python 3.12+
29+
Buffer,
2830
# Native in 3.13+
2931
TypeIs,
3032
)
@@ -529,17 +531,24 @@ def add_tool_result(self, result: ToolCallResultInput) -> ToolResultMessage:
529531
return message
530532

531533

532-
LocalFileInput = BinaryIO | bytes | str | os.PathLike[str]
534+
LocalFileInput = BinaryIO | Buffer | str | os.PathLike[str]
533535

534536

535537
# Private until file handle caching support is part of the published SDK API
536538

537539

538-
def _get_file_details(src: LocalFileInput) -> Tuple[str, bytes]:
540+
def _get_file_details(src: LocalFileInput) -> Tuple[str, Buffer]:
539541
"""Read file contents as binary data and generate a suitable default name."""
540-
if isinstance(src, bytes):
541-
# We process bytes as raw data, not a bytes filesystem path
542-
data = src
542+
data: Buffer
543+
if isinstance(src, Buffer):
544+
if isinstance(src, memoryview):
545+
# If already a memoryview, just use it directly
546+
data = src
547+
else:
548+
# Try to create a memoryview - this will work for any buffer protocol object
549+
# including bytes, bytearray, array.array, numpy arrays, etc.
550+
data = memoryview(src)
551+
# Received raw file data without any name information
543552
name = str(uuid.uuid4())
544553
elif hasattr(src, "read"):
545554
try:
@@ -550,10 +559,13 @@ def _get_file_details(src: LocalFileInput) -> Tuple[str, bytes]:
550559
raise LMStudioOSError(err_msg) from None
551560
name = getattr(src, "name", str(uuid.uuid4()))
552561
else:
562+
# At this point, src must be a path-like object
553563
try:
554564
src_path = Path(src)
555565
except Exception as exc:
556-
err_msg = f"Expected file-like object, filesystem path, or bytes ({exc!r})"
566+
err_msg = (
567+
f"Expected file-like object, filesystem path, or data buffer ({exc!r})"
568+
)
557569
raise LMStudioValueError(err_msg) from None
558570
try:
559571
data = src_path.read_bytes()
@@ -573,7 +585,7 @@ class _LocalFileData:
573585
"""Local file data to be added to a chat history."""
574586

575587
name: str
576-
raw_data: bytes
588+
raw_data: Buffer
577589

578590
def __init__(self, src: LocalFileInput, name: str | None = None) -> None:
579591
default_name, raw_data = _get_file_details(src)

0 commit comments

Comments
 (0)