Why does this exist
Streaming is a very powerful way to consume LLM outputs, but the predominant “chat” data structure produced by modern LLMs is more complex than most applications need. In fact, 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 really optimized to make these use cases easy to parse, while also supporting more advanced scenarios like parallel tools calls.Formal spec
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.
The SSE events in Braintrust follow this structure:
Text
Atext_delta is a snippet of text, which is JSON-encoded. For example, you might receive:
text_delta, you can JSON-decode the string and display it directly.
JSON
Ajson_delta is a snippet of JSON-encoded data, which cannot necessarily be parsed on its own.
For example:
json_delta events, concatenate the strings together and then parse them
as JSON at the end of the stream.
Error
Anerror event is a JSON-encoded string that contains the error message. For example:
Progress
Aprogress event is a JSON-encoded object that contains intermediate events produced by functions
while they are executing. Each json object contains the following fields:
event field is the type of event produced by the intermediate function call, and the
data field is the same as the data field in the text_delta and json_delta events.
Start
Astart 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.
Done
Adone event is a progress event with event: "done" and an empty string for data. Once a done event is received,
you can safely assume that the function has completed and will send no more events.