-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow optional lists and maps (#1251)
# Allow optional lists and maps Resolves #948 ## Description This PR adds support for optional lists and maps in BAML, allowing users to declare properties like `string[]?` and `map<string, int>?`. Previously these types were not supported, and the parser would coerce unset parameters into empty lists/maps. ## Changes 1. Parser (`parse_types.rs`): - Added support for optional arrays with `string[]?` syntax - Added optional list and map support using syntax of `map<string, int>?` - Plenty of test cases covered, both optional list and optional map 2. Json Schema Generation(`json_schema.rs`) - Supporting the optional array and maps in the generation - Correct JSON schema is generated in, with - appropriate type definitions(`[\"array\", \"null\"]`, or `[\"object\",\"null\"]`) - Null default values - Documentation 3. Grammar (`datamodel.pest`): - Changed array notation to allow optional token - Changed map syntax to allow optional token ## Example Usage ```baml class Foo { optional_array string[]? optional_map map<string, int>? } ``` ## Testing - Added parser tests for optional arrays and maps - Verified JSON schema generation - Tested with various type combinations <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Adds support for optional lists and maps in BAML, updating parser, JSON schema generation, and grammar to handle `string[]?` and `map<string, int>?` syntax. > > - **Behavior**: > - Support for optional lists and maps added in `parse_types.rs` using `string[]?` and `map<string, int>?` syntax. > - Parser now distinguishes between required and optional types, updating `FieldArity` accordingly. > - JSON schema generation in `json_schema.rs` updated to handle optional arrays and maps, allowing `null` values. > - **Grammar**: > - Updated `datamodel.pest` to allow optional token for arrays and maps. > - **Testing**: > - Added tests for optional arrays and maps in `parse_types.rs` to verify correct parsing and schema generation. > > <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 8a31ab9. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN --> --------- Co-authored-by: Antonio Sarosi <[email protected]>
- Loading branch information
1 parent
14342cd
commit 9170b89
Showing
28 changed files
with
546 additions
and
98 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
4 changes: 4 additions & 0 deletions
4
engine/baml-lib/baml/tests/validation_files/class/allowed_optionals.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,4 @@ | ||
class OptionalListAndMap { | ||
p string[]? | ||
q map<string, string>? | ||
} |
17 changes: 0 additions & 17 deletions
17
engine/baml-lib/baml/tests/validation_files/class/disallowed_types.baml
This file was deleted.
Oops, something went wrong.
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
15 changes: 15 additions & 0 deletions
15
integ-tests/baml_src/test-files/functions/output/allowed_optionals.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,15 @@ | ||
class OptionalListAndMap { | ||
p string[]? | ||
q map<string, string>? | ||
} | ||
|
||
function AllowedOptionals(optionals: OptionalListAndMap) -> OptionalListAndMap { | ||
client GPT4 | ||
prompt #" | ||
Return the given input: | ||
|
||
{{optionals}} | ||
|
||
{{ctx.output_format}} | ||
"# | ||
} |
Oops, something went wrong.