result_type
, you can ensure that tasks return exactly the data structure you need.
Scalar Types
The simplest way to specify a result type is with Python’s built-in scalar types:Classification
For classification tasks, Marvin provides several ways to specify options. Under the hood, Marvin optimizes classification by having agents choose indices rather than writing out full labels:Multi-label Classification
For tasks where multiple labels can apply, use eitherlist[Literal]
, list[Enum]
, or Marvin’s shorthand double-list syntax:
Collections
Use Python’s type hints to specify collections. Marvin supports several collection types:Lists
Lists are the most common collection type and work well with all LLM providers:Sets
Sets work similarly to lists but ensure unique values:Dictionaries
Dictionaries are useful for key-value data:Tuples
While tuples are not directly supported by most LLM providers, Marvin will attempt to coerce the result into a tuple for you:Structured Types
Marvin supports several options for complex data structures, each with their own benefits:TypedDict
TypedDicts provide a way to specify dictionary types with fixed keys:Dataclasses
Dataclasses offer a more object-oriented approach with attribute access:Pydantic Models
Pydantic models (recommended) provide rich validation and nested structures:Validation
Result Validators
You can provide a validation function to enforce additional constraints. The function should either return the validated result or raise an exception:Pydantic Validation
When using Pydantic models, you can use field or model validators for more complex validation:using field_validator
using model_validator
Annotated Types
UseAnnotated
to provide additional context about the expected result:
field_validator
) to validate a type so that the validation is baked into the type itself, you can use pydantic
’s functional validators: