Sub topic 11
Streaming replies as they are generated
This chapter assumes you can already create a session and send a synchronous message, as shown in the earlier chapters. You also need a valid access token and an open session ID.
Why stream
The synchronous endpoint returns only after the agent has finished its whole answer. For long replies, the user sees nothing until then. The streaming endpoint sends the reply in pieces as the agent generates it. Your client can show text immediately, which makes the app feel faster.
The pieces arrive as server-sent events (SSE). SSE is a standard where the server keeps one HTTP response open and writes small text events to it over time.
Enabling streaming for the session
Streaming depends on what you declare when you create the session. The session request in the earlier chapter included this field:
"streamingCapabilities": {"chunkTypes": ["Text"]}This tells the service which chunk types your client can handle. Keep it in the create-session request if you plan to stream.
Calling the streaming endpoint
The streaming endpoint is /sessions/{sessionId}/messages/stream. It takes the same message body as the synchronous call. The difference is the Accept: text/event-stream header.
curl -N -X POST "https://api.salesforce.com/einstein/ai-agent/v1/sessions/$SESSION_ID/messages/stream" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"message": {"sequenceId": 2, "type": "Text", "text": "What is the status of my order?"}}'The -N flag turns off curl's output buffering. Without it, curl may hold the events and print them all at once, which hides the streaming.
Note the sequenceId of 2. Each message in a session needs a higher sequence ID than the one before it. The first message used 1.
How the flow works
Reading the events
An SSE stream is plain text. Each event has an event: line and a data: line holding JSON, and events are separated by a blank line. Your client reads the response line by line. For each data: line, it parses the JSON and acts on the event type.
A typical client does three things:
- Appends text from each text chunk event to the display.
- Ignores or shows a typing indicator for progress events.
- Treats the end-of-turn event as the signal that the reply is complete.
Check the exact event names and JSON fields against the current Agent API reference. The service can add event types, so your parser should skip types it does not recognize instead of failing.
Common mistakes
- Missing the Accept header. Without
text/event-stream, you may get an error or a normal single response. - Buffering the output. A client or proxy that buffers the response defeats streaming. Use
-Nwith curl, and in code, read the response as a stream. - Reusing a sequence ID. Increase it with every message, streamed or not.
- Setting a short read timeout. A total timeout of 30 seconds can cut off a long reply. Use a timeout that applies to the gap between events, not to the whole response.
- Forgetting to end the session. The stream finishing does not close the session. You still call the delete endpoint.
Exercise
Your streaming call prints the whole reply at once after a long pause, even though the endpoint is correct. Name two things to check.
Show answer
Check that the client is not buffering output (for example, curl needs -N, and code should read the response as a stream), and that the Accept header is text/event-stream. A proxy that buffers responses can also cause this.
Exercise
After the end-of-turn event arrives, do you need to create a new session to send the next message?
Show answer
No. The connection for that reply closes, but the session stays open. Send the next message with a higher sequenceId, and end the session when the conversation is finished.