TypeBuilder
TypeBuilder
is used to create or modify output schemas at runtime. It’s particularly useful when you have dynamic output structures that can’t be determined at compile time - like categories from a database or user-provided schemas.
Here’s a simple example of using TypeBuilder to add new enum values before calling a BAML function:
BAML Code
Runtime Usage
Dynamic Types
There are two ways to use TypeBuilder:
- Modifying existing BAML types marked with
@@dynamic
- Creating entirely new types at runtime
Modifying Existing Types
To modify an existing BAML type, mark it with @@dynamic
:
Runtime Usage
Runtime Usage
Creating New Types
You can also create entirely new types at runtime:
Type Builders
TypeBuilder provides methods for building different kinds of types:
Adding Descriptions
You can add descriptions to properties and enum values to help guide the LLM:
Common Patterns
Here are some common patterns when using TypeBuilder:
- Dynamic Categories: When categories come from a database or external source
- Form Fields: When extracting dynamic form fields
- Optional Properties: When some fields might not be present
All types added through TypeBuilder must be connected to the return type of your BAML function. Standalone types that aren’t referenced won’t affect the output schema.
Future Features
We’re working on additional features for TypeBuilder:
- JSON Schema support (awaiting use cases)
- OpenAPI schema integration
- Pydantic model support
If you’re interested in these features, please join the discussion in our GitHub issues.