Skip to main content

Streaming

client.chat.stream() returns an Iterator[str] that yields text deltas 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

Collecting the full text

full_text = "".join(
client.chat.stream(
model="auto",
messages=[{"role": "user", "content": "Tell me a story."}],
)
)
print(full_text)

Async streaming

The async client exposes the same stream() method as an AsyncIterator[str]:

from aicp import AsyncAICPClient

async def main():
async with AsyncAICPClient(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.