@@dynamic
The @@dynamic
attribute in BAML allows for the dynamic modification of fields or values at runtime. This is particularly useful when you need to adapt the structure of your data models based on runtime conditions or external inputs.
Usage
Dynamic Classes
The @@dynamic
attribute can be applied to classes, enabling the addition of fields dynamically during runtime.
Dynamic Enums
Similarly, the @@dynamic
attribute can be applied to enums, allowing for the modification of enum values at runtime.
Using @@dynamic
with TypeBuilder
To modify dynamic types at runtime, you can use the TypeBuilder
from the baml_client
. Below are examples for Python, TypeScript, and Ruby.
Python Example
TypeScript Example
Ruby Example
Testing Dynamic Types
Dynamic classes and enums can be modified in tests using the type_builder
and
dynamic
blocks. All properties added in the dynamic
block will be available
during the test execution.