API Streaming
Learn when you can stream back to your front end
How Streaming Works
Configuring Streaming
import json
import requests
def stream_prediction(chatflow_id, question):
url = f"https://your-tailwinds-instance.com/api/v1/predictions/{chatflow_id}"
payload = {
"question": question,
"streaming": True
}
headers = {
"Content-Type": "application/json"
}
with requests.post(url, json=payload, headers=headers, stream=True) as response:
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
if decoded_line.startswith('data: '):
data = json.loads(decoded_line[6:])
if isinstance(data, dict) and 'token' in data:
print(data['token'], end='', flush=True)
elif decoded_line.startswith('event: error'):
print(f"\nError: {decoded_line}")
break
elif decoded_line == 'event: end':
print("\nStream ended")
break
# Usage
stream_prediction("your-chatflow-id", "Hello world!")Understanding the Event Stream
Event
Description
Example of a Token Event
Best Practices
Last updated
Was this helpful?