At a very high level, print_enum is a utility function in baml that takes all the type information about an enum and converts it to a string.

Syntax

#"
  {#print_enum(EnumName)}
"#
  • PrinterName is the name of the printer that will be used to print the enum. You can define your own printer or use one of the built-in printers. By default we use the Json printer.
  • EnumName is the name of the enum that you want to print.

print_enum can only be used within a block string!

Value Attributes

@alias

The alias decorator allows you to temporarily rename a value.

enum Sentiment {
  Happy @alias(positive)
  Sad @alias(negative)
}

print_enum(Sentiment) will now print:

Sentiment
---
positive
negative

Your application code remains unchanged! Its still Sentiment.Happy and Sentiment.Sad in your code. @alias is only used for serialization and deserialization.

Deserialization with @alias

The deserializer automatically maps @alias to the actual enum value!

In the above example, the deserializer will now accept positive and negative as valid values for the Sentiment enum as Sentiment.Happy and Sentiment.Sad respectively.

@description

The description decorator allows you to add a description to a property.

enum Sentiment {
  Happy @description(#"
    A happy feeling
  "#)
  Sad @description("A sad feeling")
}

print_enum(Sentiment) will now print:

Sentiment
---
Happy: A happy feeling
Sad: A sad feeling

@skip

The skip decorator allows you to skip a property when serializing and deserializing.

enum Sentiment {
  Happy @skip
  Sad
}

print_enum(Sentiment) will now print:

Sentiment
---
Sad

Block Attributes

You can add some attributes to the entire enum by using a double @@ symbol.

@@alias

This is a block attribute that allows you to alias the name of the enum itself.

enum Sentiment {
  Happy
  Sad

  @@alias(feelings)
}

print_enum(Sentiment) will now print:

feelings
---
Happy
Sad

Your application code remains unchanged! Its still Sentiment in your code. @@alias is only used for serialization.

@@reorder

Not currently implemented.

If you want to reorder how values are rendered, for now you will have to change the order in the enum definition itself.

override

By default, any attributes and block attributes you write will apply to every application of that enum. If you want to override the attributes for a specific impl, you can use the override keyword.

  • override can only be used within an impl block
  • print_enum first looks for attributes in the override block, then in the enum definition. So, things defined in the enum block will be the default value.
  • There is currently no easy way to “reset” an attribute to its default value in an override block
  • You cannot add new values to an enum in an override block

enum Sentiment {
  Happy @alias(excited)
  Sad @alias(negative)
}

impl<llm, MyAiFunction> foo {
  override Sentiment {
    Happy @alias(positive)
    @description(#"
      A happy feeling
    "#)

    Sad @description(#"
      A sad feeling
    "#)

    @@alias(feelings)
  }

  prompt #"
    {#print_enum(Sentiment)}

    Which of the above feelings do you feel?
  "#

  client SomeClient
}

This prompt will now print:

feelings
---
positive: A happy feeling
negative: A sad feeling

Which of the above feelings do you feel?

Custom Printers

This capability is currently hidden behind a feature flag. Reach out to us on discord if you want to enable it. We will be adding more documentation on this soon.

At a high level, it allows you to write python code that runs when you call print_enum with all properties of the enum.