portal-sse-streaming

Implement SSE streaming chat endpoints with typed events, cancellation, and error recovery.

Updated Jul 27, 2026
One-click install
npx skills add https://github.com/ArthurZizumbo/karisma-data --skill portal-sse-streaming-arthurzizumbo
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: portal-sse-streaming
Source: https://github.com/ArthurZizumbo/karisma-data/tree/main/.claude/skills/portal-sse-streaming
Command: npx skills add https://github.com/ArthurZizumbo/karisma-data --skill portal-sse-streaming-arthurzizumbo

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires fastapi, structlog, @microsoft/fetch-event-source.

What problem does it solve? Building a real-time chat streaming endpoint requires coordinating typed SSE events, real client-disconnect cancellation, mid-stream error recovery, and latency instrumentation, which is easy to get wrong without a defined contract. ## Core Features & Use Cases - Typed SSE Event Contract: Streams exactly four event types (tool_call, token, error, done) from a FastAPI async generator via StreamingResponse, with tool_call cards announced before tool execution. - Real Stop via Disconnect Detection: Detects client disconnects with request.is_disconnected(), cancels the in-flight LLM call to save tokens, and verifies generator cleanup with an asyncio task test. - Mid-Stream Error Recovery: Emits contextual error events with a retryable flag so the UI shows a Reintentar button without clearing conversation history, plus TTFT instrumentation as an OTel span attribute. - Use Case: When adding a chat endpoint to a FastAPI portal with a Nuxt frontend, use this Skill to scaffold the backend event generator, the useChatStream composable with AbortController, and the cancellation cleanup test. ## Quick Start Implement the /api/chat SSE streaming endpoint with typed events, Stop-button cancellation, and the useChatStream client composable.

Frequently Asked Questions about portal-sse-streaming

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I stream LLM responses with SSE in FastAPI?▼

Use an async generator yielding formatted SSE lines and return it via StreamingResponse with media_type text/event-stream. Emit typed events like tool_call, token, error, and done so the client can render progress incrementally.

How to cancel an LLM call when the client disconnects?▼

Check request.is_disconnected() inside the streaming loop on each event and break out when true, then call aclose() on the agent generator in a finally block. This propagates GeneratorExit and cancels the underlying LLM call, saving tokens.

How do I handle errors mid-stream in SSE without losing chat history?▼

Catch exceptions inside the generator and yield an error event with a contextual message and retryable flag instead of closing abruptly. The frontend shows a retry button that resends the last user query while keeping the existing conversation intact.

Does fetch-event-source support aborting an SSE request?▼

Yes, @microsoft/fetch-event-source accepts an AbortController signal in its options. Calling controller.abort() closes the socket, which the backend detects as a disconnect and uses to cancel the LLM run.

How do I test that cancelled SSE streams leave no hanging tasks?▼

Record asyncio.all_tasks() count before the request, open the stream, consume one event, then exit the context to simulate a disconnect. After a short sleep, assert the task count has returned to the baseline, proving generators were cleaned up.