Stream text chunks
result = await storyteller.stream(polos, "Tell me a story about a robot")
# Iterate over text chunks as they arrive
async for chunk in result.text_chunks:
print(chunk, end="", flush=True)
const result = await storyteller.stream(polos, {
input: 'Tell me a story about a robot',
});
// Iterate over text chunks as they arrive
for await (const chunk of result.textChunks) {
process.stdout.write(chunk);
}
Stream all events
result = await storyteller.stream(polos, "Write a haiku")
async for event in result.events:
if event.event_type == "text_delta":
print(event.data.get("content", ""), end="", flush=True)
elif event.event_type == "tool_call":
tool_name = event.data.get("tool_call", {}).get("function", {}).get("name")
print(f"\n[Tool: {tool_name}]")
elif event.event_type == "agent_finish":
print("\n[Done]")
const result = await storyteller.stream(polos, {
input: 'Write a haiku',
});
for await (const event of result.events) {
if (event.eventType === 'text_delta') {
const content = event.data['content'];
if (typeof content === 'string') {
process.stdout.write(content);
}
} else if (event.eventType === 'tool_call') {
const toolCall = event.data['tool_call'] as Record<string, unknown>;
const fn = toolCall?.['function'] as Record<string, unknown>;
console.log(`\n[Tool: ${fn?.['name']}]`);
} else if (event.eventType === 'agent_finish') {
console.log('\n[Done]');
}
}
Get final text
result = await storyteller.stream(polos, "What are benefits of reading?")
# Wait for completion and get accumulated text
final_text = await result.text()
print(final_text)
const result = await storyteller.stream(polos, {
input: 'What are benefits of reading?',
});
// Wait for completion and get accumulated text
const finalText = await result.text();
console.log(finalText);
Run it
git clone https://github.com/polos-dev/polos.git
cd polos/python-examples/03-agent-streaming
cp .env.example .env # Add your POLOS_PROJECT_ID and API key
uv sync
python main.py
git clone https://github.com/polos-dev/polos.git
cd polos/typescript-examples/03-agent-streaming
cp .env.example .env # Add your POLOS_PROJECT_ID and API key
npm install
npx tsx main.ts