AsyncClient / SyncClient
BAML generates both a sync client and an async client. They offer the exact same public API but methods are either synchronous or asynchronous.
BAML Functions
The generated client exposes all the functions that you’ve defined your BAML
files as methods. Suppose we have this file named baml_src/literature.baml
:
After running baml-cli generate
you can directly call these functions from
your code. Here’s an example using the async client:
The sync client is exactly the same but it doesn’t need an async runtime, instead it just blocks.
Call Patterns
The client object exposes some references to other objects that call your functions in a different manner.
.stream
The .stream
object is used to stream the response from a function.
.request
This feature was added in: v0.79.0
The .request
object returns the raw HTTP request but it does not send it.
However, the async client still returns an awaitable object because we might
need to resolve media types like images and convert them to base64 or the
required format in order to send them to the LLM.
.stream_request
This feature was added in: v0.79.0
Same as .request
but sets the streaming options to true
.
.parse
This feature was added in: v0.79.0
The .parse
object is used to parse the response returned by the LLM after
the function call. Can be used in combination with .request
.
.parse_stream
This feature was added in: v0.79.0
Same as .parse
but for streaming responses. Can be used in
combination with .stream_request
.