Dynamic Types
Sometimes you have a output schemas that change at runtime — for example if you have a list of Categories that you need to classify that come from a database, or your schema is user-provided.
Dynamic types are types that can be modified at runtime, which means you can change the output schema of a function at runtime.
Here are the steps to make this work:
- Add
@@dynamic
to the class or enum definition to mark it as dynamic
baml
- Create a TypeBuilder and modify the existing type. All dynamic types you
define in BAML will be available as properties of
TypeBuilder
. Think of the typebuilder as a registry of modified runtime types that the baml function will read from when building the output schema in the prompt.
Dynamic BAML Classes
Existing BAML classes marked with @@dynamic will be available as properties of TypeBuilder
.
BAML
Modify the User
schema at runtime:
Creating new dynamic classes or enums not in BAML
Here we create a new Hobbies
enum, and a new class called Address
.
Adding descriptions to dynamic types
Building dynamic types from JSON schema
We have a working implementation of this, but are waiting for a concrete use case to merge it. Please chime in on the GitHub issue if this is something you’d like to use.