|
| 1 | +Data Stream Protocol |
| 2 | +A data stream follows a special protocol that the AI SDK provides to send information to the frontend. |
| 3 | + |
| 4 | +The data stream protocol uses Server-Sent Events (SSE) format for improved standardization, keep-alive through ping, reconnect capabilities, and better cache handling. |
| 5 | + |
| 6 | +When you provide data streams from a custom backend, you need to set the x-vercel-ai-ui-message-stream header to v1. |
| 7 | + |
| 8 | +The following stream parts are currently supported: |
| 9 | + |
| 10 | +Message Start Part |
| 11 | +Indicates the beginning of a new message with metadata. |
| 12 | + |
| 13 | +Format: Server-Sent Event with JSON object |
| 14 | + |
| 15 | +Example: |
| 16 | + |
| 17 | +data: {"type":"start","messageId":"..."} |
| 18 | +Text Parts |
| 19 | +Text content is streamed using a start/delta/end pattern with unique IDs for each text block. |
| 20 | + |
| 21 | +Text Start Part |
| 22 | +Indicates the beginning of a text block. |
| 23 | + |
| 24 | +Format: Server-Sent Event with JSON object |
| 25 | + |
| 26 | +Example: |
| 27 | + |
| 28 | +data: {"type":"text-start","id":"msg_68679a454370819ca74c8eb3d04379630dd1afb72306ca5d"} |
| 29 | +Text Delta Part |
| 30 | +Contains incremental text content for the text block. |
| 31 | + |
| 32 | +Format: Server-Sent Event with JSON object |
| 33 | + |
| 34 | +Example: |
| 35 | + |
| 36 | +data: {"type":"text-delta","id":"msg_68679a454370819ca74c8eb3d04379630dd1afb72306ca5d","delta":"Hello"} |
| 37 | +Text End Part |
| 38 | +Indicates the completion of a text block. |
| 39 | + |
| 40 | +Format: Server-Sent Event with JSON object |
| 41 | + |
| 42 | +Example: |
| 43 | + |
| 44 | +data: {"type":"text-end","id":"msg_68679a454370819ca74c8eb3d04379630dd1afb72306ca5d"} |
| 45 | +Reasoning Parts |
| 46 | +Reasoning content is streamed using a start/delta/end pattern with unique IDs for each reasoning block. |
| 47 | + |
| 48 | +Reasoning Start Part |
| 49 | +Indicates the beginning of a reasoning block. |
| 50 | + |
| 51 | +Format: Server-Sent Event with JSON object |
| 52 | + |
| 53 | +Example: |
| 54 | + |
| 55 | +data: {"type":"reasoning-start","id":"reasoning_123"} |
| 56 | +Reasoning Delta Part |
| 57 | +Contains incremental reasoning content for the reasoning block. |
| 58 | + |
| 59 | +Format: Server-Sent Event with JSON object |
| 60 | + |
| 61 | +Example: |
| 62 | + |
| 63 | +data: {"type":"reasoning-delta","id":"reasoning_123","delta":"This is some reasoning"} |
| 64 | +Reasoning End Part |
| 65 | +Indicates the completion of a reasoning block. |
| 66 | + |
| 67 | +Format: Server-Sent Event with JSON object |
| 68 | + |
| 69 | +Example: |
| 70 | + |
| 71 | +data: {"type":"reasoning-end","id":"reasoning_123"} |
| 72 | +Source Parts |
| 73 | +Source parts provide references to external content sources. |
| 74 | + |
| 75 | +Source URL Part |
| 76 | +References to external URLs. |
| 77 | + |
| 78 | +Format: Server-Sent Event with JSON object |
| 79 | + |
| 80 | +Example: |
| 81 | + |
| 82 | +data: {"type":"source-url","sourceId":"https://example.com","url":"https://example.com"} |
| 83 | +Source Document Part |
| 84 | +References to documents or files. |
| 85 | + |
| 86 | +Format: Server-Sent Event with JSON object |
| 87 | + |
| 88 | +Example: |
| 89 | + |
| 90 | +data: {"type":"source-document","sourceId":"https://example.com","mediaType":"file","title":"Title"} |
| 91 | +File Part |
| 92 | +The file parts contain references to files with their media type. |
| 93 | + |
| 94 | +Format: Server-Sent Event with JSON object |
| 95 | + |
| 96 | +Example: |
| 97 | + |
| 98 | +data: {"type":"file","url":"https://example.com/file.png","mediaType":"image/png"} |
| 99 | +Data Parts |
| 100 | +Custom data parts allow streaming of arbitrary structured data with type-specific handling. |
| 101 | + |
| 102 | +Format: Server-Sent Event with JSON object where the type includes a custom suffix |
| 103 | + |
| 104 | +Example: |
| 105 | + |
| 106 | +data: {"type":"data-weather","data":{"location":"SF","temperature":100}} |
| 107 | +The data-\* type pattern allows you to define custom data types that your frontend can handle specifically. |
| 108 | + |
| 109 | +Error Part |
| 110 | +The error parts are appended to the message as they are received. |
| 111 | + |
| 112 | +Format: Server-Sent Event with JSON object |
| 113 | + |
| 114 | +Example: |
| 115 | + |
| 116 | +data: {"type":"error","errorText":"error message"} |
| 117 | +Tool Input Start Part |
| 118 | +Indicates the beginning of tool input streaming. |
| 119 | + |
| 120 | +Format: Server-Sent Event with JSON object |
| 121 | + |
| 122 | +Example: |
| 123 | + |
| 124 | +data: {"type":"tool-input-start","toolCallId":"call_fJdQDqnXeGxTmr4E3YPSR7Ar","toolName":"getWeatherInformation"} |
| 125 | +Tool Input Delta Part |
| 126 | +Incremental chunks of tool input as it's being generated. |
| 127 | + |
| 128 | +Format: Server-Sent Event with JSON object |
| 129 | + |
| 130 | +Example: |
| 131 | + |
| 132 | +data: {"type":"tool-input-delta","toolCallId":"call_fJdQDqnXeGxTmr4E3YPSR7Ar","inputTextDelta":"San Francisco"} |
| 133 | +Tool Input Available Part |
| 134 | +Indicates that tool input is complete and ready for execution. |
| 135 | + |
| 136 | +Format: Server-Sent Event with JSON object |
| 137 | + |
| 138 | +Example: |
| 139 | + |
| 140 | +data: {"type":"tool-input-available","toolCallId":"call_fJdQDqnXeGxTmr4E3YPSR7Ar","toolName":"getWeatherInformation","input":{"city":"San Francisco"}} |
| 141 | +Tool Output Available Part |
| 142 | +Contains the result of tool execution. |
| 143 | + |
| 144 | +Format: Server-Sent Event with JSON object |
| 145 | + |
| 146 | +Example: |
| 147 | + |
| 148 | +data: {"type":"tool-output-available","toolCallId":"call_fJdQDqnXeGxTmr4E3YPSR7Ar","output":{"city":"San Francisco","weather":"sunny"}} |
| 149 | +Start Step Part |
| 150 | +A part indicating the start of a step. |
| 151 | + |
| 152 | +Format: Server-Sent Event with JSON object |
| 153 | + |
| 154 | +Example: |
| 155 | + |
| 156 | +data: {"type":"start-step"} |
| 157 | +Finish Step Part |
| 158 | +A part indicating that a step (i.e., one LLM API call in the backend) has been completed. |
| 159 | + |
| 160 | +This part is necessary to correctly process multiple stitched assistant calls, e.g. when calling tools in the backend, and using steps in useChat at the same time. |
| 161 | + |
| 162 | +Format: Server-Sent Event with JSON object |
| 163 | + |
| 164 | +Example: |
| 165 | + |
| 166 | +data: {"type":"finish-step"} |
| 167 | +Finish Message Part |
| 168 | +A part indicating the completion of a message. |
| 169 | + |
| 170 | +Format: Server-Sent Event with JSON object |
| 171 | + |
| 172 | +Example: |
| 173 | + |
| 174 | +data: {"type":"finish"} |
| 175 | +Stream Termination |
| 176 | +The stream ends with a special [DONE] marker. |
| 177 | + |
| 178 | +Format: Server-Sent Event with literal [DONE] |
| 179 | + |
| 180 | +Example: |
| 181 | + |
| 182 | +data: [DONE] |
| 183 | +The data stream protocol is supported by useChat and useCompletion on the frontend and used by default. useCompletion only supports the text and data stream parts. |
| 184 | + |
| 185 | +On the backend, you can use toUIMessageStreamResponse() from the streamText result object to return a streaming HTTP response. |
0 commit comments