Enable streaming
Setstream: true when calling prompts or functions:
Stream format
Braintrust uses Server-Sent Events (SSE) format for streaming responses. The Braintrust SDK and UI automatically parse the SSE stream, and we have adapters for common libraries like the Vercel AI SDK.Why this format
Streaming is powerful for consuming LLM outputs, but the predominant “chat” data structure produced by modern LLMs is more complex than most applications need. The most common use cases are to (a) convert the text of the first message into a string or (b) parse the arguments of the first tool call into a JSON object. The Braintrust SSE format is optimized to make these use cases easy to parse, while also supporting more advanced scenarios like parallel tool calls.Event types
SSE events consist of three fields:id (optional), event (optional), and data. The Braintrust SSE format always sets event and data, and never sets id.
text_delta: Incremental text chunk (JSON-encoded string)json_delta: Incremental JSON snippet (concatenate all deltas and parse as JSON at the end)error: Error during execution (JSON-encoded string)progress: Intermediate events from function execution (JSON-encoded object)done: Stream completion signal (empty data)
Text deltas
Atext_delta is a snippet of text, which is JSON-encoded:
text_delta, JSON-decode the string and display it directly.
JSON deltas
Ajson_delta is a snippet of JSON-encoded data, which cannot necessarily be parsed on its own:
json_delta events, concatenate the strings together and parse them as JSON at the end of the stream.
Progress events
Aprogress event is a JSON-encoded object containing intermediate events produced by functions while executing:
event field is the type of event produced by the intermediate function call, and the data field follows the same format as text_delta and json_delta events.
A start event is a progress event with event: "start" and an empty string for data. Start is not guaranteed to be sent and is for display purposes only.
Stream with AI Proxy
The AI Proxy automatically streams responses from all providers:Stream with wrapped clients
AI provider wrappers automatically handle streaming:Stream via REST API
Call the Data API with streaming enabled:Handle streaming errors
Catch errors during streaming:Next steps
- Use the AI Proxy for unified streaming across providers
- Deploy prompts that support streaming
- Wrap providers to automatically log streams