Skip to content

Commit ff0c26c

Browse files
committed
Refactor file data format determination
1 parent 9af61ca commit ff0c26c

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/lmstudio/history.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
Sequence,
1919
Tuple,
2020
TypeAlias,
21-
Union,
2221
cast,
2322
get_args as get_typeform_args,
2423
runtime_checkable,
@@ -540,39 +539,33 @@ def add_tool_result(self, result: ToolCallResultInput) -> ToolResultMessage:
540539

541540
def _get_file_details(src: LocalFileInput) -> Tuple[str, Buffer]:
542541
"""Read file contents as binary data and generate a suitable default name."""
543-
# Try to handle buffer protocol objects first (unless it's a path)
544-
if not isinstance(src, (str, os.PathLike)) and not hasattr(src, "read"):
545-
try:
542+
data: Buffer
543+
if isinstance(src, Buffer):
544+
if isinstance(src, memoryview):
546545
# If already a memoryview, just use it directly
547-
if isinstance(src, memoryview):
548-
data: Buffer = src
549-
else:
550-
# Try to create a memoryview - this will work for any buffer protocol object
551-
# including bytes, bytearray, array.array, numpy arrays, etc.
552-
data = memoryview(src)
553-
name = str(uuid.uuid4())
554-
return name, data
555-
except TypeError:
556-
# Not a buffer protocol object, fall through to other checks
557-
pass
558-
559-
if hasattr(src, "read"):
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
552+
name = str(uuid.uuid4())
553+
elif hasattr(src, "read"):
560554
try:
561555
data = src.read()
562556
except OSError as exc:
563557
# Note: OSError details remain available via raised_exc.__context__
564558
err_msg = f"Error while reading {src!r} ({exc!r})"
565559
raise LMStudioOSError(err_msg) from None
566560
name = getattr(src, "name", str(uuid.uuid4()))
567-
# data is bytes here, which is a Buffer type
568-
return name, data
569561
else:
570562
# At this point, src must be a path-like object
571-
src_path_input = cast(Union[str, os.PathLike[str]], src)
572563
try:
573-
src_path = Path(src_path_input)
564+
src_path = Path(src)
574565
except Exception as exc:
575-
err_msg = f"Expected file-like object, filesystem path, or buffer ({exc!r})"
566+
err_msg = (
567+
f"Expected file-like object, filesystem path, or data buffer ({exc!r})"
568+
)
576569
raise LMStudioValueError(err_msg) from None
577570
try:
578571
data = src_path.read_bytes()
@@ -581,8 +574,7 @@ def _get_file_details(src: LocalFileInput) -> Tuple[str, Buffer]:
581574
err_msg = f"Error while reading {str(src_path)!r} ({exc!r})"
582575
raise LMStudioOSError(err_msg) from None
583576
name = str(src_path.name)
584-
# data is bytes here, which is a Buffer type
585-
return name, data
577+
return name, data
586578

587579

588580
_ContentHash: TypeAlias = bytes

0 commit comments

Comments
 (0)