Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/arkruntime/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions src/arkruntime/_list_response.py
Original file line number Diff line number Diff line change
@@ -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
106 changes: 106 additions & 0 deletions tests/test_list_null_responses.py
Original file line number Diff line number Diff line change
@@ -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
Loading