Skip to content

Pagination and streams

SyncCursorPage

SyncCursorPage(
    *,
    client: SyncAPIClient,
    data: list[_T],
    next_cursor: str | None,
    previous_cursor: str | None,
    options: RequestOptions,
    response_type: Any,
)

Bases: _PageBase[_T]

One page of results. Iterating it walks this page and every page after it.

fetch classmethod

fetch(
    client: SyncAPIClient,
    options: RequestOptions,
    response_type: Any,
) -> SyncCursorPage[Any]

build classmethod

build(
    client: SyncAPIClient,
    options: RequestOptions,
    response_type: Any,
    payload: Any,
) -> SyncCursorPage[Any]

Builds a page from an already parsed response.

next_page

next_page() -> SyncCursorPage[_T]

Fetches the following page. Raises StopIteration on the last page.

previous_page

previous_page() -> SyncCursorPage[_T]

Fetches the preceding page. Raises StopIteration on the first page.

iter_pages

iter_pages() -> Generator[SyncCursorPage[_T], None, None]

Yields this page and then each following page.

AsyncCursorPage

AsyncCursorPage(
    *,
    client: AsyncAPIClient,
    data: list[_T],
    next_cursor: str | None,
    previous_cursor: str | None,
    options: RequestOptions,
    response_type: Any,
)

Bases: _PageBase[_T]

One page of results. async for walks this page and every page after it.

fetch async classmethod

fetch(
    client: AsyncAPIClient,
    options: RequestOptions,
    response_type: Any,
) -> AsyncCursorPage[Any]

build classmethod

build(
    client: AsyncAPIClient,
    options: RequestOptions,
    response_type: Any,
    payload: Any,
) -> AsyncCursorPage[Any]

Builds a page from an already parsed response.

next_page async

next_page() -> AsyncCursorPage[_T]

Fetches the following page. Raises StopAsyncIteration on the last page.

previous_page async

previous_page() -> AsyncCursorPage[_T]

Fetches the preceding page. Raises StopAsyncIteration on the first page.

iter_pages async

iter_pages() -> AsyncIterator[AsyncCursorPage[_T]]

Yields this page and then each following page.

AsyncPaginator

AsyncPaginator(
    client: AsyncAPIClient,
    options: RequestOptions,
    response_type: Any,
)

Bases: Generic[_T]

Returned by async list methods.

await it for the first page, or async for over it to walk every item across pages.

Stream

Stream(response: Response, adapter: TypeAdapter[_T])

Bases: _StreamBase[_T]

A synchronous stream of decoded events.

Iterate it to receive events. Close it, or use it as a context manager, to release the connection::

with client.event.subscribe() as events:
    for event in events:
        ...

close

close() -> None

Closes the underlying connection.

interrupt

interrupt() -> None

Unblocks a read in progress on another thread, which then ends with :class:APIConnectionError.

close must run on the iterating thread. Call this from any other thread first to wake it.

AsyncStream

AsyncStream(response: Response, adapter: TypeAdapter[_T])

Bases: _StreamBase[_T]

An asynchronous stream of decoded events.

Iterate it with async for. Close it, or use it as an async context manager, to release the connection::

async with await client.event.subscribe() as events:
    async for event in events:
        ...

close async

close() -> None

Closes the underlying connection.

Raw responses

Returned by with_raw_response and with_streaming_response on every resource. See Accessing raw response data.

APIResponse

APIResponse(
    response: Response,
    parser: Callable[[Response], Any],
    *,
    event_stream: bool,
)

Bases: _BaseAPIResponse[_R]

An HTTP response from a sync with_raw_response / with_streaming_response call.

content property

content: bytes

The whole body, read on first access.

text property

text: str

The body decoded as text.

parse

parse() -> _R

Returns the value the plain method call would have returned, reading the body first if needed.

For event-stream endpoints this is the :class:~opencode.Stream, which consumes the body as you iterate.

read

read() -> bytes

Reads and returns the whole body.

json

json() -> Any

The body decoded as JSON.

iter_bytes

iter_bytes(
    chunk_size: int | None = None,
) -> Iterator[bytes]

Iterates over the body in byte chunks, decoding any content encoding.

iter_text

iter_text(chunk_size: int | None = None) -> Iterator[str]

Iterates over the body as decoded text chunks.

iter_lines

iter_lines() -> Iterator[str]

Iterates over the body line by line.

close

close() -> None

Releases the connection without reading the rest of the body.

AsyncAPIResponse

AsyncAPIResponse(
    response: Response,
    parser: Callable[[Response], Any],
    *,
    event_stream: bool,
)

Bases: _BaseAPIResponse[_R]

An HTTP response from an async with_raw_response / with_streaming_response call.

parse async

parse() -> _R

Returns the value the plain method call would have returned, reading the body first if needed.

For event-stream endpoints this is the :class:~opencode.AsyncStream, which consumes the body as you iterate.

read async

read() -> bytes

Reads and returns the whole body.

text async

text() -> str

The body decoded as text.

json async

json() -> Any

The body decoded as JSON.

iter_bytes async

iter_bytes(
    chunk_size: int | None = None,
) -> AsyncIterator[bytes]

Iterates over the body in byte chunks, decoding any content encoding.

iter_text async

iter_text(
    chunk_size: int | None = None,
) -> AsyncIterator[str]

Iterates over the body as decoded text chunks.

iter_lines async

iter_lines() -> AsyncIterator[str]

Iterates over the body line by line.

close async

close() -> None

Releases the connection without reading the rest of the body.