From ac0ca626763dddc2e6116f3b20b340b1b53094b0 Mon Sep 17 00:00:00 2001 From: William Schmitt Date: Mon, 16 Nov 2026 13:22:14 -0800 Subject: [PATCH] fix(files): read PathLike content inside upload tuples `is_file_content()` returned True for any tuple, so `_transform_file()` and `_async_transform_file()` took the "already file content" branch and handed `("custom.md", Path(...), "text/markdown")` to httpx unchanged. httpx then called `.read()` on the Path, raising AttributeError, which the client wraps as `APIConnectionError: Connection error.` Reproduced against api.dev.browserbase.com with `extensions.create`: a bare Path uploads, while `(name, Path)` and `(name, Path, ctype)` (sync and async) fail with APIConnectionError caused by `AttributeError: 'PosixPath' object has no attribute 'read'`. With this change all four upload and keep the custom filename (`fileName=custom.zip`). Fix: drop `tuple` from `is_file_content()` so tuples reach the existing tuple branch that reads PathLike via `read_file_content()`, and let `assert_is_file_content()` (used by `extract_files`) keep accepting tuples. This is the same change Stainless shipped upstream in openai-python 90ac74ccf "fix(files): normalize PathLike upload tuples", so the sealed edit should match the template when this repo next picks up core updates. Co-Authored-By: Claude Opus 5.5 --- src/browserbase/_files.py | 6 ++---- tests/test_files.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/browserbase/_files.py b/src/browserbase/_files.py index 8042111f..e9e272b8 100644 --- a/src/browserbase/_files.py +++ b/src/browserbase/_files.py @@ -27,13 +27,11 @@ def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]: def is_file_content(obj: object) -> TypeGuard[FileContent]: - return ( - isinstance(obj, bytes) or isinstance(obj, tuple) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike) - ) + return isinstance(obj, bytes) or isinstance(obj, io.IOBase) or isinstance(obj, os.PathLike) def assert_is_file_content(obj: object, *, key: str | None = None) -> None: - if not is_file_content(obj): + if not is_file_content(obj) and not is_tuple_t(obj): prefix = f"Expected entry at `{key}`" if key is not None else f"Expected file input `{obj!r}`" raise RuntimeError( f"{prefix} to be bytes, an io.IOBase instance, PathLike or a tuple but received {type(obj)} instead. See https://github.com/browserbase/sdk-python/tree/main#file-uploads" diff --git a/tests/test_files.py b/tests/test_files.py index 713c5994..28d4e724 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -1,3 +1,4 @@ +from typing import Any from pathlib import Path import anyio @@ -22,6 +23,23 @@ def test_tuple_input() -> None: assert result == IsList(IsTuple("file", IsTuple("README.md", IsBytes()))) +tuple_extras = pytest.mark.parametrize("extras", [(), ("text/markdown",), ("text/markdown", {"X-Test": "1"})]) + + +@tuple_extras +def test_pathlike_in_file_tuple(extras: Any) -> None: + file: Any = ("custom.md", readme_path, *extras) + assert extract_files({"file": file}, paths=[["file"]]) == [("file", file)] + assert to_httpx_files({"file": file}) == {"file": ("custom.md", readme_path.read_bytes(), *extras)} + + +@tuple_extras +@pytest.mark.asyncio +async def test_async_pathlike_in_file_tuple(extras: Any) -> None: + file: Any = ("custom.md", readme_path, *extras) + assert await async_to_httpx_files({"file": file}) == {"file": ("custom.md", readme_path.read_bytes(), *extras)} + + @pytest.mark.asyncio async def test_async_pathlib_includes_file_name() -> None: result = await async_to_httpx_files({"file": readme_path})