@@dynamic
The @@dynamic attribute allows runtime modification of fields or values.
Dynamic Classes
class MyClass {
property1 string
property2 int?
@@dynamic // allows adding fields at runtime
}
Dynamic Enums
enum MyEnum {
Value1
Value2
@@dynamic // allows modifying values at runtime
}
Using TypeBuilder
Modify dynamic types at runtime using TypeBuilder.
- Python
- TypeScript
- Ruby
from baml_client.type_builder import TypeBuilder
from baml_client import b
async def run():
tb = TypeBuilder()
tb.MyClass.add_property('email', tb.string())
tb.MyClass.add_property('address', tb.string()).description("User's address")
res = await b.DynamicUserCreator("some user info", { "tb": tb })
# res can now have email and address fields
print(res)
import TypeBuilder from '../baml_client/type_builder'
import { b } from '../baml_client'
async function run() {
const tb = new TypeBuilder()
tb.MyClass.addProperty('email', tb.string())
tb.MyClass.addProperty('address', tb.string()).description("User's address")
const res = await b.DynamicUserCreator("some user info", { tb: tb })
// res can now have email and address fields
console.log(res)
}
require_relative 'baml_client/client'
def run
tb = Baml::TypeBuilder.new
tb.MyClass.add_property('email', tb.string)
tb.MyClass.add_property('address', tb.string).description("User's address")
res = Baml::Client.dynamic_user_creator(
input: "some user info",
baml_options: { tb: tb }
)
# res can now have email and address fields
puts res
end
Testing Dynamic Types
Use type_builder and dynamic blocks in tests:
test DynamicTest {
functions [ProcessDynamic]
type_builder {
class NewType {
field1 string
}
dynamic class ExistingDynamicType {
new_prop NewType
}
}
args {
input "test data"
}
}
Use Cases
- Add fields based on runtime configuration
- Support user-defined schemas
- A/B test different output structures
- Customize enums based on context