Skip to content

Commit 2076e3d

Browse files
committed
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
1 parent 986d408 commit 2076e3d

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

‎src/arkruntime/_base_client.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
ArkAPITimeoutError,
5656
)
5757
from ._files import async_to_httpx_files, to_httpx_files
58+
from ._list_response import normalize_list_response
5859
from ._models import GenericModel, construct_type
5960
from ._qs import Querystring
6061
from ._request_options import ExtraRequestOptions, RequestOptions
@@ -474,6 +475,7 @@ def _process_response_data(
474475
response: httpx.Response,
475476
) -> ResponseT:
476477
request_id = response.headers.get(CLIENT_REQUEST_HEADER, "") if response else None
478+
data = normalize_list_response(data, response, self._base_url.path)
477479
if data is None:
478480
return cast(ResponseT, None)
479481

‎src/arkruntime/_list_response.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
from __future__ import annotations
5+
6+
import re
7+
8+
import httpx
9+
10+
11+
def normalize_list_response(data: object, response: httpx.Response, base_path: str) -> object:
12+
"""Adapt legacy null pages only for files and session resources GETs."""
13+
if response.request.method != "GET" or not response.is_success:
14+
return data
15+
path = response.request.url.path
16+
prefix = base_path.rstrip("/")
17+
is_files = path == prefix + "/files"
18+
is_resources = re.fullmatch(re.escape(prefix) + r"/sessions/[^/]+/resources", path) is not None
19+
if not (is_files or is_resources):
20+
return data
21+
if data is None:
22+
if is_files:
23+
return {"object": "file", "data": [], "first_id": "", "last_id": "", "has_more": False}
24+
return {"data": []}
25+
if isinstance(data, dict) and "data" in data and data["data"] is None:
26+
return {**data, "data": []}
27+
return data

‎tests/test_list_null_responses.py‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import asyncio
5+
6+
import httpx
7+
import pytest
8+
9+
from arkruntime import Ark, AsyncArk
10+
from arkruntime._list_response import normalize_list_response
11+
12+
13+
@pytest.fixture(autouse=True)
14+
def synthetic_credentials(monkeypatch):
15+
# MockTransport handles every request; no real credential is needed.
16+
monkeypatch.setenv("ARK_API_KEY", "placeholder")
17+
18+
19+
@pytest.mark.parametrize("api", ["files", "resources"])
20+
@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}'])
21+
def test_null_lists_sync(api, payload):
22+
transport = httpx.MockTransport(
23+
lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"})
24+
)
25+
with Ark(base_url="https://example.test/custom/v3", http_client=httpx.Client(transport=transport)) as client:
26+
result = client.files.list() if api == "files" else client.sessions.resources.list("session-1")
27+
assert result is not None
28+
assert result.data == []
29+
if api == "files":
30+
assert list(result) == []
31+
assert not result.has_next_page()
32+
33+
34+
@pytest.mark.parametrize("api", ["files", "resources"])
35+
@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}'])
36+
def test_null_lists_async(api, payload):
37+
async def run():
38+
transport = httpx.MockTransport(
39+
lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"})
40+
)
41+
http_client = httpx.AsyncClient(transport=transport)
42+
async with AsyncArk(base_url="https://example.test/custom/v3", http_client=http_client) as client:
43+
result = await client.files.list() if api == "files" else await client.sessions.resources.list("session-1")
44+
assert result is not None
45+
assert result.data == []
46+
if api == "files":
47+
assert [item async for item in result] == []
48+
assert not result.has_next_page()
49+
50+
asyncio.run(run())
51+
52+
53+
def test_pagination_and_nonempty_items_are_preserved():
54+
payload = {
55+
"object": "file",
56+
"data": [{"id": "file-1"}],
57+
"first_id": "file-1",
58+
"last_id": "file-1",
59+
"has_more": True,
60+
}
61+
transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload))
62+
with Ark(http_client=httpx.Client(transport=transport)) as client:
63+
result = client.files.list()
64+
assert result.data[0].id == "file-1"
65+
assert result.first_id == result.last_id == "file-1"
66+
assert result.has_next_page()
67+
68+
69+
@pytest.mark.parametrize(
70+
"path,method,status",
71+
[
72+
("/files/file-1", "GET", 200),
73+
("/agents", "GET", 200),
74+
("/files", "POST", 200),
75+
("/files", "GET", 400),
76+
("/sessions/session-1/resources", "POST", 200),
77+
],
78+
)
79+
def test_other_operations_and_errors_are_not_normalized(path, method, status):
80+
response = httpx.Response(status, request=httpx.Request(method, "https://example.test" + path))
81+
assert normalize_list_response(None, response, "") is None
82+
83+
84+
@pytest.mark.parametrize("data", [{}, {"data": "invalid"}, [], "invalid", 123])
85+
def test_invalid_shapes_are_not_normalized(data):
86+
response = httpx.Response(200, request=httpx.Request("GET", "https://example.test/files"))
87+
assert normalize_list_response(data, response, "") is data
88+
89+
90+
def test_resources_keep_existing_items():
91+
payload = {"data": [{"id": "resource-1", "type": "file", "file_id": "file-1"}]}
92+
transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload))
93+
with Ark(http_client=httpx.Client(transport=transport)) as client:
94+
result = client.sessions.resources.list("session-1")
95+
assert result.data[0].id == "resource-1"
96+
assert result.data[0].file_id == "file-1"
97+
98+
99+
def test_null_data_keeps_pagination_fields():
100+
payload = {"data": None, "first_id": "cursor", "last_id": "cursor", "has_more": True}
101+
transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload))
102+
with Ark(http_client=httpx.Client(transport=transport)) as client:
103+
result = client.files.list()
104+
assert result.data == []
105+
assert result.first_id == result.last_id == "cursor"
106+
assert result.has_more is True

0 commit comments

Comments
 (0)