Artifact MaterializerΒΆ
Artifact materialization from DSPy outputs.
Phase 6: Extracted from dspy_engine.py to reduce file size and improve modularity.
This module handles conversion of DSPy Prediction outputs to Flock artifacts, including JSON parsing, normalization, and fan-out support.
ClassesΒΆ
DSPyArtifactMaterializer ΒΆ
Materializes Flock artifacts from DSPy program outputs.
Responsibilities: - Normalize DSPy outputs (JSON parsing, BaseModel handling) - Select correct output payload from multi-output results - Create artifacts with fan-out support (count > 1) - Handle validation errors gracefully
FunctionsΒΆ
normalize_output_payload ΒΆ
Normalize raw DSPy output to dict format.
Handles: - BaseModel instances β model_dump() - JSON strings β parsed dict - DSPy streaming markers like [[ ## output ## ]] - Markdown fenced code blocks - Extracting JSON from text
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw | Any | Raw output from DSPy program | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Normalized dict payload |
Source code in src/flock/engines/dspy/artifact_materializer.py
materialize_artifacts ΒΆ
materialize_artifacts(payload: dict[str, Any], outputs: Iterable[Any], produced_by: str, pre_generated_id: Any = None)
Materialize artifacts from payload, handling fan-out (count > 1).
For fan-out outputs (count > 1), splits the list into individual artifacts. For single outputs (count = 1), creates one artifact from dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload | dict[str, Any] | Normalized output dict from DSPy | required |
outputs | Iterable[Any] | AgentOutput declarations defining what to create | required |
produced_by | str | Agent name | required |
pre_generated_id | Any | Pre-generated ID for streaming (only used for single outputs) | None |
Returns:
| Type | Description |
|---|---|
| Tuple of (artifacts list, errors list) |
Source code in src/flock/engines/dspy/artifact_materializer.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
select_output_payload ΒΆ
select_output_payload(payload: Mapping[str, Any], model_cls: type[BaseModel], type_name: str) -> dict[str, Any] | list[dict[str, Any]]
Select the correct output payload from the normalized output dict.
Handles both simple type names and fully qualified names (with module prefix). Returns either a dict (single output) or list[dict] (fan-out/batch).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload | Mapping[str, Any] | Normalized output dict | required |
model_cls | type[BaseModel] | Pydantic model class | required |
type_name | str | Type name from OutputSpec | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | list[dict[str, Any]] | Either dict (single) or list[dict] (fan-out/batch) |