https://www.boundaryml.com logo
🟡 Setting a field type to dynamic in BAML
# questions
b
can I set the type of a field in baml to be dynamic instead of the entire class? Something like
Copy code
class Picker{
  fruit @dynamic
  vegetables @dynamic
}
such that the attributes are set (and type safe) just the types of them are dynamic (trying to use string_literals here)
b
We don't have this, unfortunately
b
actually i'm making it work with enums!
Copy code
enum Fruit {
@@dynamic
}
enum Vegetable {
@@dynamic
}
class Picker{
  fruit Fruit
  vegetable Vegetable
}
Actually @baml-sam i'm noticing something odd. Based on this setup, I'd expect
Picker.fruit
to be of type
Fruit | None
but it's actually
Fruit | str | None
-- and calling a function that returns
Picker
does indeed have
fruit
as a string. Why isn't it the enum? https://cdn.discordapp.com/attachments/1426282842048172052/1426294633163522108/image.png?ex=68eab3e3&is=68e96263&hm=6bda3febd1bd64f4deb32d62054375229b03d20b48c05100b5bfc6bf7186219e& https://cdn.discordapp.com/attachments/1426282842048172052/1426294633574568068/image.png?ex=68eab3e3&is=68e96263&hm=7c02608a118e8a9cced9e6934681ee8f2ac153ef1ce2e4efccdf112bfde477ce&
b
Ah, that’s one of the weird things about dynamic enums- if we return a dynamically created enum value it gets returned as a string
eg if you add “banana” to Fruit at runtime, bc python doesnt know that “banana” is a Fruit, we can’t /only/ type the return as a Fruit
b
i actually prefer that in my case (because some of my strings arent enum compatible), but maybe you can update the compiled code to fix the type hint?
b
I’m saying the type hint is correct! strings are what dynamic enums values get returned as
b
i think the type hint should be
str | None
-- not
str | Fruit | None
since it cannot ever be a
Fruit
type
b
Ah, because you’re treating it as a placeholder, I see
b
oh I see, so if I had 1 value in it like
Copy code
enum Fruit{
  APPLE
  @@dynamic
}
and the model picks apple, it WILL be a Fruit type
b
Yep
b
that is indeed confusing
b
creating types at runtime is 🤯🫠
b
personally i would challenge that and say "if you add dynamics to enums, for the sake of consistency, we will always return strings"
but i understand the nuance, and why its not obviously one way or the other
b
That’s very fair! Glad that we could make it make sense, will keep it in mind when we next revisit this