Skip to content

[feat] Enable Dynamic Instantiation of BAML Enums + Bug Report #2480

@dlmgary

Description

@dlmgary

Product

BAML

Problem Statement / Use Case

Hi folks!

First, I want to say that I'm a huge fan of BAML and think you're on the right track to find the correct level of abstraction for AI/code invocation.

Feature Request

The feature I'm requesting (which there might already be a way to do, but I haven't been able to find) is the ability to instantiate dynamically generated BAML enums on the caller side.

Problem

I haven't found a way to dynamically instantiate the BAML enums that I create via the TypeBuilder. The enums are available to the BAML client callee when passed via baml_options={"tb": tb}, but they're not available to the caller.

Current Behavior

Given the following enum definition:

# enums.baml 
enum MyBamlEnum {
    VariableOne
    @@dynamic
}

I can add new variables dynamically:

from baml_client import b as baml_client
from baml_client.type_builder import TypeBuilder

tb = TypeBuilder()
tb.MyBamlEnum.add_value(name="VariableTwo")
baml_client.MyFunction(..., baml_options={"tb": tb})

As expected, MyFunction sees MyBamlEnum with both variables: VariableOne and VariableTwo. However, the dynamically added values are not accessible to the function caller:

tb = TypeBuilder()
tb.MyBamlEnum.add_value(name="VariableTwo")
tb.MyBamlEnum.list_values()
# Returns: [('VariableOne', <baml_py.baml_py.EnumValueBuilder at 0x147069e90>)]
# Notice "VariableTwo" is missing
Desired Behavior

Ideally, tb.MyBamlEnum would allow users to instantiate their newly created dynamic enum with something like:

tb = TypeBuilder()
tb.MyBamlEnum.add_value(name="VariableTwo")

# Creates a new class with the added variable
updated_enum = tb.MyBamlEnum.get_class()
print(updated_enum)
# Expected: <enum 'MyBamlEnum'>

print(updated_enum("VariableOne"))
# Expected: MyBamlEnum.VariableOne

print(updated_enum("VariableTwo"))
# Expected: MyBamlEnum.VariableTwo

This would be extremely helpful for downstream applications (e.g., creating mocks, validation, etc.).

Bug Report: Inconsistent Enum Return Types

Additionally, I believe there's a bug in how enums are handled by BAML functions. The enum returns different types depending on how instances were created:

  • Static enum values return objects: <MyBamlEnum.VariableOne: 'VariableOne'>
  • Dynamic enum values return strings: 'VariableTwo'

Reproduction Steps

Using this sample BAML function:

function MyFunction(
    name_of_variable_to_return: string  
) -> MyBamlEnum {
    client "openai/gpt-4.1-nano"
    prompt #"
    return the enum called: {{name_of_variable_to_return }}
    {{ ctx.output_format }}
    "#
}
from baml_client import b as baml_client
from baml_client.type_builder import TypeBuilder
from baml_client.types import MyBamlEnum

tb = TypeBuilder()
tb.MyBamlEnum.add_value(name="VariableTwo")

resp_1 = baml_client.MyFunction(name_of_variable_to_return="variable_one", baml_options={"tb": tb})
print(resp_1)
# Actual: <MyBamlEnum.VariableOne: 'VariableOne'>

resp_2 = baml_client.MyFunction(name_of_variable_to_return="variable_two", baml_options={"tb": tb})
print(resp_2)
# Actual: 'VariableTwo'
# Expected: <MyBamlEnum.VariableTwo: 'VariableTwo'>

Expected Behavior

Both static and dynamic enum values should return the same type of object for consistency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions