-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make literals nullable in generated python (#1334)
Fixes an issue with streaming for classes containing literal types. During streaming there may not be enough tokens to satisfy a field's literal value. In that case, the field should be `null`, and this should be acceptable for the partial type. The old generated `partial_types.py` does not allow the field to be null however, so pydantic validation fails to create a `partial_types.ClassForNullLiteral`: ``` class ClassForNullLiteral(BaseModel): a: Literal["hi"] = None ``` The new generated `partial_types.py` after this PR allows literal fields to be null: ``` class ClassForNullLiteral(BaseModel): a: Optional[Literal["hi"]] = None ``` PR adds one parser test and one integ test. <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > This PR allows literal fields in generated Python code to be nullable, updating code generation and adding tests to ensure correct handling of null literals. > > - **Behavior**: > - Allows literal fields to be nullable in `partial_types.py` by changing `a: Literal["hi"] = None` to `a: Optional[Literal["hi"]] = None`. > - Updates `BamlSyncClient` and `BamlAsyncClient` to handle nullable literals in `sync_client.ts` and `async_client.ts`. > - **Code Generation**: > - Modifies `generate_types.rs` to wrap literals with `Optional[]`. > - Updates `mod.rs` to reflect changes in literal handling. > - **Testing**: > - Adds `test_partial_class_with_null_literal` in `test_literals.rs`. > - Introduces `literal-or-null.baml` for integration testing. > - Adds `test_null_literal_class_hello` in `test_functions.py`. > > <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 910ab4b. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
- Loading branch information
1 parent
98b7783
commit 68745d0
Showing
21 changed files
with
367 additions
and
37 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
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/literal-or-null.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 ClassForNullLiteral { | ||
a "hi" | ||
} | ||
|
||
function NullLiteralClassHello(s: string) -> ClassForNullLiteral { | ||
client GPT35 | ||
prompt #" | ||
Return the empty object: {}. | ||
"# | ||
} | ||
|
||
test NullLiteralClassHello { | ||
functions [NullLiteralClassHello] | ||
args { s "unused" } | ||
} |
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.