Skip to main content

@@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.

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)

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

See Also​