Cast
Convert data between types while preserving meaning
The cast
function is your bridge between natural language and structured data types. It transforms str → T
while preserving meaning, making it easy to convert:
- Natural numbers (“three point five” → 3.5)
- Casual responses (“sounds good” → True)
- Time expressions (“next Tuesday” → datetime)
- Rich descriptions (“John is 25” → Person(name=“John”, age=25))
For complex transformations, consider creating a custom task. The cast
function is a convenient wrapper around Marvin’s task system - see Tasks for more details.
Usage
Convert natural language descriptions of numbers into their numeric representation:
Parameters
data
: The input data to convert (any type)target
: The target type to convert to (defaults tostr
)instructions
: Optional instructions to guide the conversionagent
: Optional custom agent to usethread
: Optional thread for conversation contextcontext
: Optional additional context
Async Support
The function is also available in an async version:
Examples
Converting Numbers
Convert natural language descriptions of quantities into numeric values:
Boolean Conversion
Interpret natural language expressions as boolean values:
Structured Data
Extract structured information from natural language descriptions:
Custom Instructions
Use instructions to control how text is interpreted and formatted: