Streaming
BAML lets you stream in structured JSON output from LLMs as it comes in.
If you tried streaming in a JSON output from an LLM you’d see something like:
BAML automatically fixes this partial JSON, and transforms all your types into Partial
types with all Optional
fields only during the stream.
You can check out more examples (including streaming in FastAPI and NextJS) in the BAML Examples repo.
Lets stream the output of this function function ExtractReceiptInfo(email: string) -> ReceiptInfo
for our example:
Python
TypeScript
Ruby (beta)
OpenAPI
BAML will generate b.stream.ExtractReceiptInfo()
for you, which you can use like so:
main.py
Number fields are always streamed in only when the LLM completes them. E.g. if the final number is 129.95, you’ll only see null or 129.95 instead of partial numbers like 1, 12, 129.9, etc.