diff --git a/.release-please-manifest.json b/.release-please-manifest.json index caf1487..271a68c 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "1.11.0" + ".": "1.11.1" } \ No newline at end of file diff --git a/.stats.yml b/.stats.yml index e977e9c..d6c1b85 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 7 openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/landingai%2Fade-7247e8d6fb71965ebfc666fb0c1379394b2d7744be2bb304e30e04acfa8855f8.yml openapi_spec_hash: 5511c4c03ee213ceaf68c9f9d889f861 -config_hash: 2b7c8ce95f04ab9eea27533da72f6234 +config_hash: 56c9f6570b7996559a642d8de67b400f diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e1f7ab..44da4fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.11.1 (2026-04-18) + +Full Changelog: [v1.11.0...v1.11.1](https://github.com/landing-ai/ade-python/compare/v1.11.0...v1.11.1) + +### Performance Improvements + +* **client:** optimize file structure copying in multipart requests ([e78f8c0](https://github.com/landing-ai/ade-python/commit/e78f8c0604a7d59671998f02b7bb1832d8c09aa3)) + ## 1.11.0 (2026-04-13) Full Changelog: [v1.10.0...v1.11.0](https://github.com/landing-ai/ade-python/compare/v1.10.0...v1.11.0) diff --git a/pyproject.toml b/pyproject.toml index 501304b..0f12a41 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "landingai-ade" -version = "1.11.0" +version = "1.11.1" description = "The official Python library for the landingai-ade API" dynamic = ["readme"] license = "Apache-2.0" diff --git a/src/landingai_ade/_client.py b/src/landingai_ade/_client.py index 15a1310..326807b 100644 --- a/src/landingai_ade/_client.py +++ b/src/landingai_ade/_client.py @@ -14,6 +14,7 @@ from . import _exceptions from ._qs import Querystring from .types import client_parse_params, client_split_params, client_extract_params, client_extract_build_schema_params +from ._files import deepcopy_with_paths from ._types import ( Body, Omit, @@ -33,7 +34,6 @@ is_given, extract_files, maybe_transform, - deepcopy_minimal, get_async_library, async_maybe_transform, ) @@ -348,14 +348,15 @@ def extract( # Convert local file paths to file parameters markdown, markdown_url = convert_url_to_file_if_local(markdown, markdown_url) - body = deepcopy_minimal( + body = deepcopy_with_paths( { "schema": schema, "markdown": markdown, "markdown_url": markdown_url, "model": model, "strict": strict, - } + }, + [["markdown"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdown"]]) # It should be noted that the actual Content-Type header that will be @@ -430,14 +431,15 @@ def extract_build_schema( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "markdown_urls": markdown_urls, "markdowns": markdowns, "model": model, "prompt": prompt, "schema": schema, - } + }, + [["markdowns", ""]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdowns", ""]]) # It should be noted that the actual Content-Type header that will be @@ -520,7 +522,7 @@ def parse( # Convert local file paths to file parameters document, document_url = convert_url_to_file_if_local(document, document_url) - body = deepcopy_minimal( + body = deepcopy_with_paths( { "custom_prompts": custom_prompts, "document": document, @@ -528,7 +530,8 @@ def parse( "model": model, "password": password, "split": split, - } + }, + [["document"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["document"]]) # It should be noted that the actual Content-Type header that will be @@ -606,13 +609,14 @@ def split( # Store original inputs for filename extraction original_markdown = markdown original_markdown_url = markdown_url - body = deepcopy_minimal( + body = deepcopy_with_paths( { "split_class": split_class, "markdown": markdown, "markdown_url": markdown_url, "model": model, - } + }, + [["markdown"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdown"]]) # It should be noted that the actual Content-Type header that will be @@ -885,14 +889,15 @@ async def extract( # Convert local file paths to file parameters markdown, markdown_url = convert_url_to_file_if_local(markdown, markdown_url) - body = deepcopy_minimal( + body = deepcopy_with_paths( { "schema": schema, "markdown": markdown, "markdown_url": markdown_url, "model": model, "strict": strict, - } + }, + [["markdown"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdown"]]) # It should be noted that the actual Content-Type header that will be @@ -963,14 +968,15 @@ async def extract_build_schema( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "markdown_urls": markdown_urls, "markdowns": markdowns, "model": model, "prompt": prompt, "schema": schema, - } + }, + [["markdowns", ""]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdowns", ""]]) # It should be noted that the actual Content-Type header that will be @@ -1045,7 +1051,7 @@ async def parse( # Convert local file paths to file parameters document, document_url = convert_url_to_file_if_local(document, document_url) - body = deepcopy_minimal( + body = deepcopy_with_paths( { "custom_prompts": custom_prompts, "document": document, @@ -1053,7 +1059,8 @@ async def parse( "model": model, "password": password, "split": split, - } + }, + [["document"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["document"]]) # It should be noted that the actual Content-Type header that will be @@ -1119,13 +1126,14 @@ async def split( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "split_class": split_class, "markdown": markdown, "markdown_url": markdown_url, "model": model, - } + }, + [["markdown"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["markdown"]]) # It should be noted that the actual Content-Type header that will be diff --git a/src/landingai_ade/_files.py b/src/landingai_ade/_files.py index a43caad..1e26af6 100644 --- a/src/landingai_ade/_files.py +++ b/src/landingai_ade/_files.py @@ -3,8 +3,8 @@ import io import os import pathlib -from typing import overload -from typing_extensions import TypeGuard +from typing import Sequence, cast, overload +from typing_extensions import TypeVar, TypeGuard import anyio @@ -17,7 +17,9 @@ HttpxFileContent, HttpxRequestFiles, ) -from ._utils import is_tuple_t, is_mapping_t, is_sequence_t +from ._utils import is_list, is_mapping, is_tuple_t, is_mapping_t, is_sequence_t + +_T = TypeVar("_T") def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]: @@ -132,3 +134,51 @@ async def async_read_file_content(file: FileContent) -> HttpxFileContent: return file.encode('utf-8') return file + + +def deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]]) -> _T: + """Copy only the containers along the given paths. + + Used to guard against mutation by extract_files without copying the entire structure. + Only dicts and lists that lie on a path are copied; everything else + is returned by reference. + + For example, given paths=[["foo", "files", "file"]] and the structure: + { + "foo": { + "bar": {"baz": {}}, + "files": {"file": } + } + } + The root dict, "foo", and "files" are copied (they lie on the path). + "bar" and "baz" are returned by reference (off the path). + """ + return _deepcopy_with_paths(item, paths, 0) + + +def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T: + if not paths: + return item + if is_mapping(item): + key_to_paths: dict[str, list[Sequence[str]]] = {} + for path in paths: + if index < len(path): + key_to_paths.setdefault(path[index], []).append(path) + + # if no path continues through this mapping, it won't be mutated and copying it is redundant + if not key_to_paths: + return item + + result = dict(item) + for key, subpaths in key_to_paths.items(): + if key in result: + result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1) + return cast(_T, result) + if is_list(item): + array_paths = [path for path in paths if index < len(path) and path[index] == ""] + + # if no path expects a list here, nothing will be mutated inside it - return by reference + if not array_paths: + return cast(_T, item) + return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item]) + return item diff --git a/src/landingai_ade/_utils/__init__.py b/src/landingai_ade/_utils/__init__.py index 10cb66d..1c090e5 100644 --- a/src/landingai_ade/_utils/__init__.py +++ b/src/landingai_ade/_utils/__init__.py @@ -24,7 +24,6 @@ coerce_integer as coerce_integer, file_from_path as file_from_path, strip_not_given as strip_not_given, - deepcopy_minimal as deepcopy_minimal, get_async_library as get_async_library, maybe_coerce_float as maybe_coerce_float, get_required_header as get_required_header, diff --git a/src/landingai_ade/_utils/_utils.py b/src/landingai_ade/_utils/_utils.py index 63b8cd6..771859f 100644 --- a/src/landingai_ade/_utils/_utils.py +++ b/src/landingai_ade/_utils/_utils.py @@ -177,21 +177,6 @@ def is_iterable(obj: object) -> TypeGuard[Iterable[object]]: return isinstance(obj, Iterable) -def deepcopy_minimal(item: _T) -> _T: - """Minimal reimplementation of copy.deepcopy() that will only copy certain object types: - - - mappings, e.g. `dict` - - list - - This is done for performance reasons. - """ - if is_mapping(item): - return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()}) - if is_list(item): - return cast(_T, [deepcopy_minimal(entry) for entry in item]) - return item - - # copied from https://github.com/Rapptz/RoboDanny def human_join(seq: Sequence[str], *, delim: str = ", ", final: str = "or") -> str: size = len(seq) diff --git a/src/landingai_ade/_version.py b/src/landingai_ade/_version.py index ea8e696..bf3c85f 100644 --- a/src/landingai_ade/_version.py +++ b/src/landingai_ade/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "landingai_ade" -__version__ = "1.11.0" # x-release-please-version +__version__ = "1.11.1" # x-release-please-version diff --git a/src/landingai_ade/resources/parse_jobs.py b/src/landingai_ade/resources/parse_jobs.py index d8810d2..d25cb9c 100644 --- a/src/landingai_ade/resources/parse_jobs.py +++ b/src/landingai_ade/resources/parse_jobs.py @@ -8,8 +8,9 @@ import httpx from ..types import parse_job_list_params, parse_job_create_params +from .._files import deepcopy_with_paths from .._types import Body, Omit, Query, Headers, NotGiven, FileTypes, omit, not_given -from .._utils import extract_files, path_template, maybe_transform, deepcopy_minimal, async_maybe_transform +from .._utils import extract_files, path_template, maybe_transform, async_maybe_transform from .._compat import cached_property from .._resource import SyncAPIResource, AsyncAPIResource from .._response import ( @@ -106,7 +107,7 @@ def create( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "custom_prompts": custom_prompts, "document": document, @@ -115,7 +116,8 @@ def create( "output_save_url": output_save_url, "password": password, "split": split, - } + }, + [["document"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["document"]]) # It should be noted that the actual Content-Type header that will be @@ -304,7 +306,7 @@ async def create( timeout: Override the client-level default timeout for this request, in seconds """ - body = deepcopy_minimal( + body = deepcopy_with_paths( { "custom_prompts": custom_prompts, "document": document, @@ -313,7 +315,8 @@ async def create( "output_save_url": output_save_url, "password": password, "split": split, - } + }, + [["document"]], ) files = extract_files(cast(Mapping[str, object], body), paths=[["document"]]) # It should be noted that the actual Content-Type header that will be diff --git a/tests/test_deepcopy.py b/tests/test_deepcopy.py deleted file mode 100644 index 00ba666..0000000 --- a/tests/test_deepcopy.py +++ /dev/null @@ -1,58 +0,0 @@ -from landingai_ade._utils import deepcopy_minimal - - -def assert_different_identities(obj1: object, obj2: object) -> None: - assert obj1 == obj2 - assert id(obj1) != id(obj2) - - -def test_simple_dict() -> None: - obj1 = {"foo": "bar"} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_dict() -> None: - obj1 = {"foo": {"bar": True}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - - -def test_complex_nested_dict() -> None: - obj1 = {"foo": {"bar": [{"hello": "world"}]}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"]) - assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0]) - - -def test_simple_list() -> None: - obj1 = ["a", "b", "c"] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_list() -> None: - obj1 = ["a", [1, 2, 3]] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1[1], obj2[1]) - - -class MyObject: ... - - -def test_ignores_other_types() -> None: - # custom classes - my_obj = MyObject() - obj1 = {"foo": my_obj} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert obj1["foo"] is my_obj - - # tuples - obj3 = ("a", "b") - obj4 = deepcopy_minimal(obj3) - assert obj3 is obj4 diff --git a/tests/test_files.py b/tests/test_files.py index 6d4006b..8527bac 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -4,7 +4,8 @@ import pytest from dirty_equals import IsDict, IsList, IsBytes, IsTuple -from landingai_ade._files import to_httpx_files, async_to_httpx_files +from landingai_ade._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files +from landingai_ade._utils import extract_files readme_path = Path(__file__).parent.parent.joinpath("README.md") @@ -50,3 +51,99 @@ def test_string_allowed() -> None: } ) assert result == {"file": b"foo"} + + +def assert_different_identities(obj1: object, obj2: object) -> None: + assert obj1 == obj2 + assert obj1 is not obj2 + + +class TestDeepcopyWithPaths: + def test_copies_top_level_dict(self) -> None: + original = {"file": b"data", "other": "value"} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + + def test_file_value_is_same_reference(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + assert result["file"] is file_bytes + + def test_list_popped_wholesale(self) -> None: + files = [b"f1", b"f2"] + original = {"files": files, "title": "t"} + result = deepcopy_with_paths(original, [["files", ""]]) + assert_different_identities(result, original) + result_files = result["files"] + assert isinstance(result_files, list) + assert_different_identities(result_files, files) + + def test_nested_array_path_copies_list_and_elements(self) -> None: + elem1 = {"file": b"f1", "extra": 1} + elem2 = {"file": b"f2", "extra": 2} + original = {"items": [elem1, elem2]} + result = deepcopy_with_paths(original, [["items", "", "file"]]) + assert_different_identities(result, original) + result_items = result["items"] + assert isinstance(result_items, list) + assert_different_identities(result_items, original["items"]) + assert_different_identities(result_items[0], elem1) + assert_different_identities(result_items[1], elem2) + + def test_empty_paths_returns_same_object(self) -> None: + original = {"foo": "bar"} + result = deepcopy_with_paths(original, []) + assert result is original + + def test_multiple_paths(self) -> None: + f1 = b"file1" + f2 = b"file2" + original = {"a": f1, "b": f2, "c": "unchanged"} + result = deepcopy_with_paths(original, [["a"], ["b"]]) + assert_different_identities(result, original) + assert result["a"] is f1 + assert result["b"] is f2 + assert result["c"] is original["c"] + + def test_extract_files_does_not_mutate_original_top_level(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes, "other": "value"} + + copied = deepcopy_with_paths(original, [["file"]]) + extracted = extract_files(copied, paths=[["file"]]) + + assert extracted == [("file", file_bytes)] + assert original == {"file": file_bytes, "other": "value"} + assert copied == {"other": "value"} + + def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None: + file1 = b"f1" + file2 = b"f2" + original = { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + + copied = deepcopy_with_paths(original, [["items", "", "file"]]) + extracted = extract_files(copied, paths=[["items", "", "file"]]) + + assert extracted == [("items[][file]", file1), ("items[][file]", file2)] + assert original == { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + assert copied == { + "items": [ + {"extra": 1}, + {"extra": 2}, + ], + "title": "example", + }