0
# Types
1
2
Custom type definitions for specialized content. ZenML provides string subclasses for structured content types that preserve semantic meaning and enable automatic visualization.
3
4
## Capabilities
5
6
### HTML String
7
8
```python { .api }
9
class HTMLString(str):
10
"""
11
String subclass for HTML content.
12
13
Used to mark strings as HTML for proper rendering and visualization
14
in the ZenML dashboard.
15
16
Example:
17
```python
18
from zenml import step
19
from zenml.types import HTMLString
20
21
@step
22
def generate_report() -> HTMLString:
23
html = '''
24
<html>
25
<body>
26
<h1>Model Performance Report</h1>
27
<p>Accuracy: 95%</p>
28
<table>
29
<tr><th>Metric</th><th>Value</th></tr>
30
<tr><td>Precision</td><td>0.93</td></tr>
31
<tr><td>Recall</td><td>0.97</td></tr>
32
</table>
33
</body>
34
</html>
35
'''
36
return HTMLString(html)
37
```
38
"""
39
```
40
41
Import from:
42
43
```python
44
from zenml.types import HTMLString
45
```
46
47
### Markdown String
48
49
```python { .api }
50
class MarkdownString(str):
51
"""
52
String subclass for Markdown content.
53
54
Used to mark strings as Markdown for proper rendering and visualization.
55
56
Example:
57
```python
58
from zenml import step
59
from zenml.types import MarkdownString
60
61
@step
62
def generate_documentation() -> MarkdownString:
63
md = '''
64
# Model Documentation
65
66
## Overview
67
This model performs sentiment analysis on text data.
68
69
## Metrics
70
- **Accuracy**: 95%
71
- **Precision**: 93%
72
- **Recall**: 97%
73
74
## Usage
75
```python
76
model.predict("This is a great product!")
77
```
78
'''
79
return MarkdownString(md)
80
```
81
"""
82
```
83
84
Import from:
85
86
```python
87
from zenml.types import MarkdownString
88
```
89
90
### CSV String
91
92
```python { .api }
93
class CSVString(str):
94
"""
95
String subclass for CSV content.
96
97
Used to mark strings as CSV for proper visualization and export.
98
99
Example:
100
```python
101
from zenml import step
102
from zenml.types import CSVString
103
104
@step
105
def export_results() -> CSVString:
106
csv = '''name,accuracy,precision,recall
107
model_v1,0.90,0.88,0.92
108
model_v2,0.93,0.91,0.95
109
model_v3,0.95,0.93,0.97
110
'''
111
return CSVString(csv)
112
```
113
"""
114
```
115
116
Import from:
117
118
```python
119
from zenml.types import CSVString
120
```
121
122
### JSON String
123
124
```python { .api }
125
class JSONString(str):
126
"""
127
String subclass for JSON content.
128
129
Used to mark strings as JSON for proper visualization and validation.
130
131
Example:
132
```python
133
from zenml import step
134
from zenml.types import JSONString
135
import json
136
137
@step
138
def export_config() -> JSONString:
139
config = {
140
"model": {
141
"type": "transformer",
142
"layers": 12,
143
"hidden_size": 768
144
},
145
"training": {
146
"learning_rate": 0.001,
147
"batch_size": 32,
148
"epochs": 10
149
}
150
}
151
return JSONString(json.dumps(config, indent=2))
152
```
153
"""
154
```
155
156
Import from:
157
158
```python
159
from zenml.types import JSONString
160
```
161
162
## Usage Examples
163
164
### HTML Report Generation
165
166
```python
167
from zenml import step, pipeline
168
from zenml.types import HTMLString
169
170
@step
171
def train_model(data: list) -> dict:
172
"""Train model."""
173
return {
174
"accuracy": 0.95,
175
"precision": 0.93,
176
"recall": 0.97,
177
"f1": 0.95
178
}
179
180
@step
181
def generate_html_report(metrics: dict) -> HTMLString:
182
"""Generate HTML performance report."""
183
html = f'''
184
<!DOCTYPE html>
185
<html>
186
<head>
187
<style>
188
body {{ font-family: Arial, sans-serif; margin: 20px; }}
189
h1 {{ color: #333; }}
190
table {{ border-collapse: collapse; width: 100%; }}
191
th, td {{ border: 1px solid #ddd; padding: 8px; text-align: left; }}
192
th {{ background-color: #4CAF50; color: white; }}
193
</style>
194
</head>
195
<body>
196
<h1>Model Performance Report</h1>
197
<table>
198
<tr>
199
<th>Metric</th>
200
<th>Value</th>
201
</tr>
202
<tr>
203
<td>Accuracy</td>
204
<td>{metrics["accuracy"]:.2%}</td>
205
</tr>
206
<tr>
207
<td>Precision</td>
208
<td>{metrics["precision"]:.2%}</td>
209
</tr>
210
<tr>
211
<td>Recall</td>
212
<td>{metrics["recall"]:.2%}</td>
213
</tr>
214
<tr>
215
<td>F1 Score</td>
216
<td>{metrics["f1"]:.2%}</td>
217
</tr>
218
</table>
219
</body>
220
</html>
221
'''
222
return HTMLString(html)
223
224
@pipeline
225
def reporting_pipeline():
226
"""Pipeline generating HTML report."""
227
metrics = train_model([1, 2, 3])
228
report = generate_html_report(metrics)
229
return report
230
```
231
232
### Markdown Documentation
233
234
```python
235
from zenml import step
236
from zenml.types import MarkdownString
237
238
@step
239
def generate_model_card(
240
model: dict,
241
metrics: dict,
242
training_info: dict
243
) -> MarkdownString:
244
"""Generate model card in Markdown."""
245
md = f'''
246
# Model Card: {model.get("name", "Untitled Model")}
247
248
## Model Details
249
- **Type**: {model.get("type", "Unknown")}
250
- **Version**: {model.get("version", "1.0")}
251
- **Framework**: {model.get("framework", "N/A")}
252
253
## Performance Metrics
254
| Metric | Value |
255
|--------|-------|
256
| Accuracy | {metrics.get("accuracy", 0):.2%} |
257
| Precision | {metrics.get("precision", 0):.2%} |
258
| Recall | {metrics.get("recall", 0):.2%} |
259
| F1 Score | {metrics.get("f1", 0):.2%} |
260
261
## Training Information
262
- **Training Samples**: {training_info.get("samples", "N/A")}
263
- **Training Duration**: {training_info.get("duration", "N/A")}
264
- **Learning Rate**: {training_info.get("learning_rate", "N/A")}
265
266
## Intended Use
267
This model is intended for sentiment analysis on customer reviews.
268
269
## Limitations
270
- English language only
271
- Maximum sequence length: 512 tokens
272
- Performance degrades on domain-specific jargon
273
274
## Ethical Considerations
275
Model may exhibit bias on certain demographic groups. Regular monitoring recommended.
276
'''
277
return MarkdownString(md.strip())
278
```
279
280
### CSV Export
281
282
```python
283
from zenml import step
284
from zenml.types import CSVString
285
286
@step
287
def export_experiment_results(experiments: list) -> CSVString:
288
"""Export experiment results as CSV."""
289
# Header
290
csv_lines = ["experiment_id,model_type,accuracy,precision,recall,f1_score"]
291
292
# Data rows
293
for exp in experiments:
294
csv_lines.append(
295
f"{exp['id']},"
296
f"{exp['model_type']},"
297
f"{exp['accuracy']},"
298
f"{exp['precision']},"
299
f"{exp['recall']},"
300
f"{exp['f1']}"
301
)
302
303
csv = "\n".join(csv_lines)
304
return CSVString(csv)
305
306
# Usage
307
experiments = [
308
{"id": "exp1", "model_type": "rf", "accuracy": 0.90, "precision": 0.88, "recall": 0.92, "f1": 0.90},
309
{"id": "exp2", "model_type": "xgb", "accuracy": 0.93, "precision": 0.91, "recall": 0.95, "f1": 0.93},
310
{"id": "exp3", "model_type": "lgbm", "accuracy": 0.95, "precision": 0.93, "recall": 0.97, "f1": 0.95},
311
]
312
313
csv_result = export_experiment_results(experiments)
314
```
315
316
### JSON Configuration
317
318
```python
319
from zenml import step
320
from zenml.types import JSONString
321
import json
322
323
@step
324
def export_model_config(model: dict) -> JSONString:
325
"""Export model configuration as JSON."""
326
config = {
327
"model_architecture": {
328
"type": model.get("type", "transformer"),
329
"layers": model.get("layers", 12),
330
"hidden_size": model.get("hidden_size", 768),
331
"attention_heads": model.get("attention_heads", 12)
332
},
333
"training_config": {
334
"optimizer": "AdamW",
335
"learning_rate": 0.001,
336
"batch_size": 32,
337
"epochs": 10,
338
"warmup_steps": 1000
339
},
340
"preprocessing": {
341
"max_length": 512,
342
"tokenizer": "bert-base-uncased",
343
"lowercase": True
344
},
345
"inference": {
346
"batch_size": 64,
347
"temperature": 1.0,
348
"top_k": 50,
349
"top_p": 0.9
350
}
351
}
352
353
return JSONString(json.dumps(config, indent=2))
354
```
355
356
### Combined Report Types
357
358
```python
359
from zenml import step, pipeline
360
from zenml.types import HTMLString, MarkdownString, CSVString
361
from typing import Tuple
362
363
@step
364
def generate_comprehensive_report(
365
metrics: dict
366
) -> Tuple[HTMLString, MarkdownString, CSVString]:
367
"""Generate report in multiple formats."""
368
369
# HTML report
370
html = HTMLString(f'''
371
<html><body>
372
<h1>Report</h1>
373
<p>Accuracy: {metrics["accuracy"]}</p>
374
</body></html>
375
''')
376
377
# Markdown report
378
md = MarkdownString(f'''
379
# Report
380
**Accuracy**: {metrics["accuracy"]}
381
''')
382
383
# CSV export
384
csv = CSVString(f'''metric,value
385
accuracy,{metrics["accuracy"]}
386
''')
387
388
return html, md, csv
389
390
@pipeline
391
def multi_format_pipeline():
392
"""Pipeline generating reports in multiple formats."""
393
metrics = {"accuracy": 0.95}
394
html, md, csv = generate_comprehensive_report(metrics)
395
return html, md, csv
396
```
397
398
### Visualization in Dashboard
399
400
All structured string types (HTMLString, MarkdownString, CSVString, JSONString) are automatically rendered appropriately in the ZenML dashboard:
401
402
- **HTMLString**: Rendered as interactive HTML
403
- **MarkdownString**: Rendered with Markdown formatting
404
- **CSVString**: Displayed as formatted table with download option
405
- **JSONString**: Syntax-highlighted JSON with collapsible sections
406
407
These types enable rich, automatically-visualized outputs from pipeline steps without requiring custom visualization code.
408