Loading...
Loading...
Implement and debug SSE (Server-Sent Events) streaming for the Perplexity AI API, including parsing, reconnection, and retry logic.
npx skill4agent add pv-udpv/pplx-sdk sse-streamingevent: query_progress
data: {"status": "searching", "progress": 0.5}
event: answer_chunk
data: {"text": "partial token", "backend_uuid": "uuid-here"}
event: final_response
data: {"text": "complete answer", "cursor": "cursor-value", "backend_uuid": "uuid-here"}
: [end]:[end]event: <type>data: <json>json.loads()[end]| Event | Purpose | Key Fields |
|---|---|---|
| Search progress | |
| Source citations | |
| Partial token | |
| Complete answer | |
| Follow-up suggestions | |
| Server error | |
for chunk in transport.stream(query="...", context_uuid="..."):
if chunk.type == "answer_chunk":
print(chunk.text, end="", flush=True) # Incremental display
elif chunk.type == "final_response":
entry = Entry(...) # Build complete entry
breakfinal_responsecursor = last_chunk.data.get("cursor")
backend_uuid = last_chunk.backend_uuid
payload["cursor"] = cursor
payload["resume_entry_uuids"] = [backend_uuid]from pplx_sdk.shared.retry import RetryConfig
config = RetryConfig(
max_retries=3,
initial_backoff_ms=1000, # 1s, 2s, 4s
max_backoff_ms=30000,
backoff_multiplier=2.0,
jitter=True, # ±25% randomization
)json.loads()StreamingErrordata:[end][end]