Skip to content

Commit

Permalink
Add basic docs for literal types (#1030)
Browse files Browse the repository at this point in the history
Mention **Literal Types** in `supported-types` and add a couple
examples.
<!-- ELLIPSIS_HIDDEN -->


----

> [!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.
> 
> <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 109b715. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
antoniosarosi authored Oct 15, 2024
1 parent 10130b8 commit 55e5964
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/docs/snippets/supported-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<Info>
This feature was added in: v0.61.0.
</Info>

## Multimodal Types
See [calling a function with multimodal types](/docs/snippets/calling-baml/multi-modal)
and [testing image inputs](/docs/snippets/test-cases#images)
Expand Down Expand Up @@ -335,6 +358,23 @@ Optional[List[Union[Optional[int], List[str], MyClass]]]

</CodeBlocks>

### Example 5

<CodeBlocks>
```baml BAML
"str" | 1 | false
```

```python Python Equivalent
Union[Literal["str"], Literal[1], Literal[False]]
```

```typescript TypeScript Equivalent
"str" | 1 | false
```

</CodeBlocks>

## ⚠️ 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.
Expand Down

0 comments on commit 55e5964

Please sign in to comment.