-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enums & Literals as map keys (#1178)
Issue #1050 Added support for enums and literal strings in map keys: ```baml enum MapKey { A B C } class Fields { // Enum as key e map<MapKey, string> // Single literal as key l1 map<"literal", string> // Union of literals as keys l2 map<"one" | "two" | ("three" | "four"), string> } ``` Literal integers are more complicated since they require maps to support int keys. See #1180. <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Add support for enums and literal strings as map keys in BAML, with updated validation, coercion logic, and tests. > > - **Behavior**: > - Support for enums and literal strings as map keys added in `mod.rs` and `types.rs`. > - Validation logic updated to allow enums and literal strings as map keys. > - Coercion logic updated to handle enums and literal strings as map keys. > - **Tests**: > - Added tests in `map_enums_and_literals.baml` and `map_types.baml` to verify new map key functionality. > - Updated `test_functions.py` and `integ-tests.test.ts` to include cases for enum and literal string map keys. > - **Misc**: > - Updated error messages in `error.rs` to reflect new map key types. > - Minor updates in `async_client.py`, `sync_client.py`, and `client.rb` to support new map key types. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup> for c7742fd. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
- Loading branch information
1 parent
fcdbdfb
commit 39e0271
Showing
35 changed files
with
1,210 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
engine/baml-lib/baml/tests/validation_files/class/map_enums_and_literals.baml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
enum MapKey { | ||
A | ||
B | ||
C | ||
} | ||
|
||
class Fields { | ||
e map<MapKey, string> | ||
l1 map<"literal", string> | ||
l2 map<"one" | "two" | ("three" | "four"), string> | ||
} | ||
|
||
function InOutEnumKey(i1: map<MapKey, string>, i2: map<MapKey, string>) -> map<MapKey, string> { | ||
client "openai/gpt-4o" | ||
prompt #" | ||
Merge these: {{i1}} {{i2}} | ||
|
||
{{ ctx.output_format }} | ||
"# | ||
} | ||
|
||
function InOutLiteralStringUnionMapKey( | ||
i1: map<"one" | "two" | ("three" | "four"), string>, | ||
i2: map<"one" | "two" | ("three" | "four"), string> | ||
) -> map<"one" | "two" | ("three" | "four"), string> { | ||
client "openai/gpt-4o" | ||
prompt #" | ||
Merge these: | ||
|
||
{{i1}} | ||
|
||
{{i2}} | ||
|
||
{{ ctx.output_format }} | ||
"# | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.