For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Usage
  • Dynamic Classes
  • Dynamic Enums
  • Using @@dynamic with TypeBuilder
  • Python Example
  • TypeScript Example
  • Ruby Example
  • Testing Dynamic Types
Attributes

@@dynamic

Was this page helpful?
Edit this page
Previous

LLM Clients (client<llm>)

Next
Built with

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.

BAML
1class MyClass {
2 property1 string
3 property2 int?
4
5 @@dynamic // allows adding fields dynamically at runtime
6}

Dynamic Enums

Similarly, the @@dynamic attribute can be applied to enums, allowing for the modification of enum values at runtime.

BAML
1enum MyEnum {
2 Value1
3 Value2
4
5 @@dynamic // allows modifying enum values dynamically at runtime
6}

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.

Read more about the TypeBuilder in the TypeBuilder section.

Python Example

1from baml_client.type_builder import TypeBuilder
2from baml_client import b
3
4async def run():
5 tb = TypeBuilder()
6 tb.MyClass.add_property('email', tb.string())
7 tb.MyClass.add_property('address', tb.string()).description("The user's address")
8 res = await b.DynamicUserCreator("some user info", { "tb": tb })
9 # Now res can have email and address fields
10 print(res)

TypeScript Example

1import TypeBuilder from '../baml_client/type_builder'
2import { b } from '../baml_client'
3
4async function run() {
5 const tb = new TypeBuilder()
6 tb.MyClass.addProperty('email', tb.string())
7 tb.MyClass.addProperty('address', tb.string()).description("The user's address")
8 const res = await b.DynamicUserCreator("some user info", { tb: tb })
9 // Now res can have email and address fields
10 console.log(res)
11}

Ruby Example

1require_relative 'baml_client/client'
2
3def run
4 tb = Baml::TypeBuilder.new
5 tb.MyClass.add_property('email', tb.string)
6 tb.MyClass.add_property('address', tb.string).description("The user's address")
7
8 res = Baml::Client.dynamic_user_creator(input: "some user info", baml_options: {tb: tb})
9 # Now res can have email and address fields
10 puts res
11end

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.

1class DynamicClass {
2 static_prop string
3 @@dynamic
4}
5
6function ReturnDynamicClass(input: string) -> DynamicClass {
7 // ...
8}
9
10test DynamicClassTest {
11 functions [ReturnDynamicClass]
12 type_builder {
13 dynamic class DynamicClass {
14 new_prop_here string
15 }
16 }
17 args {
18 input "test data"
19 }
20}