From 2076e3d6b71e1c435d46acb2c4daf94c224f3a06 Mon Sep 17 00:00:00 2001 From: "ark-hand[bot]" Date: Wed, 4 Nov 2026 03:52:52 +0000 Subject: [PATCH] fix: tolerate null file and session resource lists Sync-Source-Commit: e5a7f32646b30a47a01bacaf86e4706dec5c650e Hand-Written-Reason: No Ark-APIs provenance marker; treated as a hand-written source commit. Release-Version: 0.19.0 --- src/arkruntime/_base_client.py | 2 + src/arkruntime/_list_response.py | 27 ++++++++ tests/test_list_null_responses.py | 106 ++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/arkruntime/_list_response.py create mode 100644 tests/test_list_null_responses.py diff --git a/src/arkruntime/_base_client.py b/src/arkruntime/_base_client.py index 8b950f2..634797e 100644 --- a/src/arkruntime/_base_client.py +++ b/src/arkruntime/_base_client.py @@ -55,6 +55,7 @@ ArkAPITimeoutError, ) from ._files import async_to_httpx_files, to_httpx_files +from ._list_response import normalize_list_response from ._models import GenericModel, construct_type from ._qs import Querystring from ._request_options import ExtraRequestOptions, RequestOptions @@ -474,6 +475,7 @@ def _process_response_data( response: httpx.Response, ) -> ResponseT: request_id = response.headers.get(CLIENT_REQUEST_HEADER, "") if response else None + data = normalize_list_response(data, response, self._base_url.path) if data is None: return cast(ResponseT, None) diff --git a/src/arkruntime/_list_response.py b/src/arkruntime/_list_response.py new file mode 100644 index 0000000..e074020 --- /dev/null +++ b/src/arkruntime/_list_response.py @@ -0,0 +1,27 @@ +# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 + +from __future__ import annotations + +import re + +import httpx + + +def normalize_list_response(data: object, response: httpx.Response, base_path: str) -> object: + """Adapt legacy null pages only for files and session resources GETs.""" + if response.request.method != "GET" or not response.is_success: + return data + path = response.request.url.path + prefix = base_path.rstrip("/") + is_files = path == prefix + "/files" + is_resources = re.fullmatch(re.escape(prefix) + r"/sessions/[^/]+/resources", path) is not None + if not (is_files or is_resources): + return data + if data is None: + if is_files: + return {"object": "file", "data": [], "first_id": "", "last_id": "", "has_more": False} + return {"data": []} + if isinstance(data, dict) and "data" in data and data["data"] is None: + return {**data, "data": []} + return data diff --git a/tests/test_list_null_responses.py b/tests/test_list_null_responses.py new file mode 100644 index 0000000..25f9dc9 --- /dev/null +++ b/tests/test_list_null_responses.py @@ -0,0 +1,106 @@ +# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 + +import asyncio + +import httpx +import pytest + +from arkruntime import Ark, AsyncArk +from arkruntime._list_response import normalize_list_response + + +@pytest.fixture(autouse=True) +def synthetic_credentials(monkeypatch): + # MockTransport handles every request; no real credential is needed. + monkeypatch.setenv("ARK_API_KEY", "placeholder") + + +@pytest.mark.parametrize("api", ["files", "resources"]) +@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}']) +def test_null_lists_sync(api, payload): + transport = httpx.MockTransport( + lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"}) + ) + with Ark(base_url="https://example.test/custom/v3", http_client=httpx.Client(transport=transport)) as client: + result = client.files.list() if api == "files" else client.sessions.resources.list("session-1") + assert result is not None + assert result.data == [] + if api == "files": + assert list(result) == [] + assert not result.has_next_page() + + +@pytest.mark.parametrize("api", ["files", "resources"]) +@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}']) +def test_null_lists_async(api, payload): + async def run(): + transport = httpx.MockTransport( + lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"}) + ) + http_client = httpx.AsyncClient(transport=transport) + async with AsyncArk(base_url="https://example.test/custom/v3", http_client=http_client) as client: + result = await client.files.list() if api == "files" else await client.sessions.resources.list("session-1") + assert result is not None + assert result.data == [] + if api == "files": + assert [item async for item in result] == [] + assert not result.has_next_page() + + asyncio.run(run()) + + +def test_pagination_and_nonempty_items_are_preserved(): + payload = { + "object": "file", + "data": [{"id": "file-1"}], + "first_id": "file-1", + "last_id": "file-1", + "has_more": True, + } + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) + with Ark(http_client=httpx.Client(transport=transport)) as client: + result = client.files.list() + assert result.data[0].id == "file-1" + assert result.first_id == result.last_id == "file-1" + assert result.has_next_page() + + +@pytest.mark.parametrize( + "path,method,status", + [ + ("/files/file-1", "GET", 200), + ("/agents", "GET", 200), + ("/files", "POST", 200), + ("/files", "GET", 400), + ("/sessions/session-1/resources", "POST", 200), + ], +) +def test_other_operations_and_errors_are_not_normalized(path, method, status): + response = httpx.Response(status, request=httpx.Request(method, "https://example.test" + path)) + assert normalize_list_response(None, response, "") is None + + +@pytest.mark.parametrize("data", [{}, {"data": "invalid"}, [], "invalid", 123]) +def test_invalid_shapes_are_not_normalized(data): + response = httpx.Response(200, request=httpx.Request("GET", "https://example.test/files")) + assert normalize_list_response(data, response, "") is data + + +def test_resources_keep_existing_items(): + payload = {"data": [{"id": "resource-1", "type": "file", "file_id": "file-1"}]} + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) + with Ark(http_client=httpx.Client(transport=transport)) as client: + result = client.sessions.resources.list("session-1") + assert result.data[0].id == "resource-1" + assert result.data[0].file_id == "file-1" + + +def test_null_data_keeps_pagination_fields(): + payload = {"data": None, "first_id": "cursor", "last_id": "cursor", "has_more": True} + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) + with Ark(http_client=httpx.Client(transport=transport)) as client: + result = client.files.list() + assert result.data == [] + assert result.first_id == result.last_id == "cursor" + assert result.has_more is True