Streaming
fold.run supports SSE (Server-Sent Events) streaming for function responses, A2A task execution, and pipeline output. When a deployed function returns a streaming response, fold.run passes it through to the caller without buffering.
Function streaming
If your deployed function returns a text/event-stream or application/x-ndjson response, fold.run detects it automatically and streams the response through to the caller.
Use fold.stream() from @fold-run/runtime to return an SSE response from an async generator:
import { defineHandler, type FoldContext } from '@fold-run/runtime';
export default defineHandler(async (fold: FoldContext) => {
return fold.stream(async function* () {
yield 'data: hello\n\n';
yield 'data: world\n\n';
});
});For AI inference, stream tokens directly from fold.ai:
import { defineHandler, type FoldContext } from '@fold-run/runtime';
export default defineHandler(async (fold: FoldContext) => {
const { prompt } = await fold.request.json() as { prompt: string };
return fold.stream(async function* () {
const stream = await fold.ai.run('@fold/llama-3', { prompt, stream: true });
for await (const chunk of stream as AsyncIterable<{ response?: string }>) {
if (chunk.response) yield `data: ${JSON.stringify({ token: chunk.response })}\n\n`;
}
});
});Activation tracking
Streaming activations go through two states:
| State | Description |
|---|---|
streaming |
Response headers received, body still streaming |
success / error |
Stream completed or errored |
The activation record updates to its final status when the stream closes.
A2A streaming
The A2A endpoint supports tasks/sendSubscribe for streaming task execution via SSE.
Agent Card
Streaming-capable organizations advertise "streaming": true in their Agent Card:
{
"capabilities": {
"streaming": true,
"pushNotifications": false
}
}tasks/sendSubscribe
curl -X POST https://my-tenant.fold.run/_a2a/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": "1",
"method": "tasks/sendSubscribe",
"params": {
"skill": "weather-lookup",
"message": { "city": "San Francisco" }
}
}'Response is an SSE stream:
event: status
data: {"id":"task_abc123","status":{"state":"working"}}
event: artifact
data: {"id":"task_abc123","artifact":{"name":"weather-lookup-result","parts":[{"type":"text","text":"{\"temp\":62}"}]}}
event: status
data: {"id":"task_abc123","status":{"state":"completed"}}Timeout handling
Functions have a ~30-second wall-clock limit. If a streaming task hasn't completed within 28 seconds, the stream closes with a working status and meta.timeout: true. Use tasks/get to poll for the final result.
MCP streaming
The MCP endpoint at /.well-known/mcp uses the MCP SDK's Streamable HTTP transport. The transport currently responds with JSON (not SSE) for all tool calls. SSE streaming for MCP tool results will be enabled when the MCP SDK adds Accept-header content negotiation.