Streaming
Streaming means you get the answer piece by piece, as the AI writes it, instead of waiting for all of it. It is how chat screens show words appearing one after another.
client.chat.stream() gives you an Iterator[str]: a list of text pieces (called deltas) that you can loop over as they arrive.
Sync streaming
for chunk in client.chat.stream(
model="auto",
messages=[{"role": "user", "content": "Write a haiku about the sea."}],
):
print(chunk, end="", flush=True)
print() # newline after stream ends
The for loop waits for each piece, one at a time. end="" keeps the pieces on one line, and flush=True shows each one straight away.
Collecting the full text
Want the whole answer at the end as well? Join the pieces together:
full_text = "".join(
client.chat.stream(
model="auto",
messages=[{"role": "user", "content": "Tell me a story."}],
)
)
print(full_text)
Async streaming
The async client has the same stream() method, as an AsyncIterator[str]:
from nuroute import AsyncNuRouteClient
async def main():
async with AsyncNuRouteClient(api_key="aicp-...", base_url="https://your-gateway") as client:
async for chunk in client.chat.stream(
model="auto",
messages=[{"role": "user", "content": "Hello!"}],
):
print(chunk, end="", flush=True)
Parameters
stream() accepts the same parameters as complete(). See Chat completions.
The iterator gives you text only, so the SDK does not expose the gateway's X-Router-* response headers. To find out whether Artemis or the fallback router decided a streamed request, and why, look it up with client.requests.list() (see Which router decided).