Python v4 SDKs now stream server-sent events. An operation whose success response is text/event-stream returns an EventStream[T] typed over the event's data schema, and the asynchronous client returns an AsyncEventStream[T] you read with async for.
Details
The runtime now has one request seam: a call returns as soon as the response head arrives, and the decoder owns the body from there. That is what lets a stream start yielding events before the connection closes, and the same seam serves JSON, text, binary and empty responses.
Each iteration yields one event's data, parsed into the schema the specification declares, and a discriminated oneOf event arrives already routed to its variant. The stream ends cleanly on the sentinel named by the x-sse-sentinel extension, which is consumed and never yielded, or when the server closes the connection. Draining the loop releases the connection, and a with block releases it when you stop early.
Usage Example
Iterate the stream directly, and open it in a with block whenever you might stop before it ends:
from payments import PaymentsClient
client = PaymentsClient(bearer_auth="YOUR_ACCESS_TOKEN")
# Each event's data arrives as a typed model; the loop ends with the stream.
for update in client.payments.stream_payment_updates("pay_123"):
print(update.status)
# The with block releases the connection when you break out early.
with client.payments.stream_payment_updates("pay_123") as stream:
for update in stream:
if update.settled_at is not None:
break
On the async client, await the call and iterate the result:
from payments import AsyncPaymentsClient
async def main() -> None:
async with AsyncPaymentsClient(bearer_auth="YOUR_ACCESS_TOKEN") as client:
async for update in await client.payments.stream_payment_updates("pay_123"):
print(update.status)
This feature is available in the Python v4 SDK (beta). Generate it from the APIMatic CLI:
apimatic sdk generate --language=python --codegen-version=v4 --stability=beta
For the full set of flags and options, see the APIMatic CLI documentation.