From 55e596419055c8da52b841b9ecbf16e328bc1033 Mon Sep 17 00:00:00 2001 From: Antonio Sarosi Date: Tue, 15 Oct 2024 17:34:26 +0100 Subject: [PATCH] Add basic docs for literal types (#1030) Mention **Literal Types** in `supported-types` and add a couple examples. ---- > [!IMPORTANT] > Add documentation for Literal Types in `supported-types.mdx`, including examples and references. > > - **Documentation**: > - Add section for **Literal Types** in `supported-types.mdx`. > - Include example of using literal values as return types in a function. > - Reference to Union type for more details. > - **Examples**: > - Add Example 5 demonstrating literal types in BAML, Python, and TypeScript equivalents. > > This description was created by [Ellipsis](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral) for 109b715f09b0a1515b81542d88b598100265ee4e. It will automatically update as commits are pushed. --- docs/docs/snippets/supported-types.mdx | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/docs/snippets/supported-types.mdx b/docs/docs/snippets/supported-types.mdx index cf715dedc..18b2a2527 100644 --- a/docs/docs/snippets/supported-types.mdx +++ b/docs/docs/snippets/supported-types.mdx @@ -13,6 +13,29 @@ Here's a list of all the types that can be represented in BAML: * `string` * `null` +## Literal Types + +The primitive types `string`, `int` and `bool` can be constrained to a specific value. +For example, you can use literal values as return types: + +```rust +function ClassifyIssue(issue_description: string) -> "bug" | "enhancement" { + client GPT4Turbo + prompt #" + {{ _.role("user")}} + Classify the issue based on the following description: + {{ issue_description }} + {{ ctx.output_format }} + "# +} +``` + +See [Union(|)](#union-) for more details. + + + This feature was added in: v0.61.0. + + ## Multimodal Types See [calling a function with multimodal types](/docs/snippets/calling-baml/multi-modal) and [testing image inputs](/docs/snippets/test-cases#images) @@ -335,6 +358,23 @@ Optional[List[Union[Optional[int], List[str], MyClass]]] +### Example 5 + + +```baml BAML +"str" | 1 | false +``` + +```python Python Equivalent +Union[Literal["str"], Literal[1], Literal[False]] +``` + +```typescript TypeScript Equivalent +"str" | 1 | false +``` + + + ## ⚠️ Unsupported - `any/json` - Not supported. We don't want to encourage its use as it defeats the purpose of having a type system. if you really need it, for now use `string` and call `json.parse` yourself or use [dynamic types](../calling-baml/dynamic-types.mdx) - `datetime` - Not yet supported. Use a `string` instead.