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 |
For a framework-specific case (mlflow’s ChatAgent), see the mlflow integration example.
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)
}
}Provide dump, load, or both, depending on your needs.
Next steps
- Review the mapping syntax that feeds these serializers.
- See the mlflow integration example for a Pydantic-based endpoint.