BAML with Python/TS/Ruby

Streaming BAML Functions

Now that we know how to call BAML functions, let’s learn how to stream BAML function calls.

You can check out more examples in the BAML Examples repo.

This time, we’ll use function ExtractReceiptInfo(email: string) -> ReceiptInfo for our example:

BAML will generate b.stream.ExtractReceiptInfo() for you, which you can use like so:

main.py
1import asyncio
2from baml_client import b, partial_types, types
3
4# Using both async iteration and get_final_response() from a stream
5async def example1(receipt: str):
6 stream = b.stream.ExtractReceiptInfo(receipt)
7
8 async for partial in stream:
9 print(f"partial: parsed {len(partial.items)} items (object: {partial})")
10
11 final = await stream.get_final_response()
12 print(f"final: {len(final.items)} items (object: {final})")
13
14# Using only async iteration of a stream
15async def example2(receipt: str):
16 async for partial in b.stream.ExtractReceiptInfo(receipt):
17 print(f"partial: parsed {len(partial.items)} items (object: {partial})")
18
19# Using only get_final_response() of a stream
20#
21# In this case, you should just use b.ExtractReceiptInfo(receipt) instead,
22# which is faster and more efficient.
23async def example3(receipt: str):
24 final = await b.stream.ExtractReceiptInfo(receipt).get_final_response()
25 print(f"final: {len(final.items)} items (object: {final})")
26
27receipt = """
2804/14/2024 1:05 pm
29
30Ticket: 220000082489
31Register: Shop Counter
32Employee: Connor
33Customer: Sam
34Item # Price
35Guide leash (1 Pair) uni UNI
361 $34.95
37The Index Town Walls
381 $35.00
39Boot Punch
403 $60.00
41Subtotal $129.95
42Tax ($129.95 @ 9%) $11.70
43Total Tax $11.70
44Total $141.65
45"""
46
47if __name__ == '__main__':
48 asyncio.run(example1(receipt))
49 asyncio.run(example2(receipt))
50 asyncio.run(example3(receipt))