Advanced Mapping
Learn how to map complex objects like Pydantic models, dataclasses, and custom types as function parameters and return values. The SDK automatically handles serialization and deserialization.
How It Works
When using request_mapping, the SDK:
- Input (load): Converts mapped dictionaries to typed objects based on function parameter type hints
- Output (dump): Serializes return values to JSON-compatible dictionaries
This means you can use native type signatures without manual conversion:
Automatic Type Detection
The SDK automatically detects and handles common serialization patterns:
| Type | Output (dump) | Input (load) |
|---|---|---|
| Pydantic v2 | model_dump() | model_validate() |
| Pydantic v1 | dict() | parse_obj() |
| Dataclass | dataclasses.asdict() | Type(**dict) |
| NamedTuple | _asdict() | Type(**dict) |
to_dict/from_dict | to_dict() | from_dict() |
| Primitives | pass through | pass through |
Using with Pydantic Models
mlflow Agent Example
For mlflow’s ChatAgent framework:
Using with Dataclasses
Mixed Parameters
Functions can mix typed objects with primitives:
Custom Serializers
For types that don’t follow standard patterns, provide custom serializers:
Serializer Format
The serializers parameter accepts a dictionary mapping types to their handlers:
serializers={
MyType: {
"dump": lambda obj: {...}, # object → dict (for output)
"load": lambda d: MyType(...), # dict → object (for input)
}
}You can provide just dump, just load, or both depending on your needs.
Backward Compatibility
The serialization system is fully backward compatible. Functions with simple parameters work exactly as before:
How It Works Internally
All values flow through the same serialization path:
Input Flow:
Rhesis Request → request_mapping → TypeSerializer.load() → Function Parameter
Output Flow:
Function Return → TypeSerializer.dump() → response_mapping → Rhesis ResponseThe serializer automatically:
- Detects the appropriate method based on object type
- Recursively handles nested structures
- Falls back gracefully for unknown types