Signature BuilderΒΆ
DSPy signature building from output specifications.
Phase 6: Extracted from dspy_engine.py to reduce file size and improve modularity.
This module handles all signature-related logic for DSPy program execution, including semantic field naming, pluralization, batching, and fan-out support.
ClassesΒΆ
DSPySignatureBuilder ΒΆ
Builds DSPy signatures from output group specifications.
Responsibilities: - Convert Pydantic models to snake_case field names - Pluralize field names for batching and fan-out - Generate DSPy signatures with semantic naming - Build execution payloads matching signatures - Extract outputs from predictions
FunctionsΒΆ
prepare_signature_for_output_group ΒΆ
prepare_signature_for_output_group(dspy_mod, *, agent, inputs: EvalInputs, output_group, has_context: bool = False, batched: bool = False, engine_instructions: str | None = None) -> Any
Prepare DSPy signature dynamically based on OutputGroup with semantic field names.
This method generates signatures using semantic field naming: - Type names β snake_case field names (Task β "task", ResearchQuestion β "research_question") - Pluralization for fan-out (Idea β "ideas" for lists) - Pluralization for batching (Task β "tasks" for list[Task]) - Multi-input support for joins (multiple input artifacts with semantic names) - Collision handling (same input/output type β prefix with "input_" or "output_")
Examples:
Single output: .consumes(Task).publishes(Report) β {"task": (Task, InputField()), "report": (Report, OutputField())}
Multiple inputs (joins): .consumes(Document, Guidelines).publishes(Report) β {"document": (Document, InputField()), "guidelines": (Guidelines, InputField()), "report": (Report, OutputField())}
Multiple outputs: .consumes(Task).publishes(Summary, Analysis) β {"task": (Task, InputField()), "summary": (Summary, OutputField()), "analysis": (Analysis, OutputField())}
Fan-out: .publishes(Idea, fan_out=5) β {"topic": (Topic, InputField()), "ideas": (list[Idea], OutputField(...))}
Batching: evaluate_batch([task1, task2, task3]) β {"tasks": (list[Task], InputField()), "reports": (list[Report], OutputField())}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dspy_mod | DSPy module | required | |
agent | Agent instance | required | |
inputs | EvalInputs | EvalInputs with input artifacts | required |
output_group | OutputGroup defining what to generate | required | |
has_context | bool | Whether conversation context should be included | False |
batched | bool | Whether this is a batch evaluation (pluralizes input fields) | False |
engine_instructions | str | None | Optional override for engine instructions | None |
Returns:
Type | Description |
---|---|
Any | DSPy Signature with semantic field names |
Source code in src/flock/engines/dspy/signature_builder.py
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
prepare_execution_payload_for_output_group ΒΆ
prepare_execution_payload_for_output_group(inputs: EvalInputs, output_group, *, batched: bool, has_context: bool, context_history: list | None, sys_desc: str) -> dict[str, Any]
Prepare execution payload with semantic field names matching signature.
This method builds a payload dict with semantic field names that match the signature generated by prepare_signature_for_output_group()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs | EvalInputs | EvalInputs with input artifacts | required |
output_group | OutputGroup (not used here but kept for symmetry) | required | |
batched | bool | Whether this is a batch evaluation | required |
has_context | bool | Whether conversation context should be included | required |
context_history | list | None | Optional conversation history | required |
sys_desc | str | System description for the "description" field | required |
Returns:
Type | Description |
---|---|
dict[str, Any] | Dict with semantic field names ready for DSPy program execution |
Examples:
Single input: {"description": desc, "task": {...}} Multi-input: {"description": desc, "task": {...}, "topic": {...}} Batched: {"description": desc, "tasks": [{...}, {...}, {...}]}
Source code in src/flock/engines/dspy/signature_builder.py
extract_multi_output_payload ΒΆ
Extract semantic fields from DSPy Prediction for multi-output scenarios.
Maps semantic field names (e.g., "movie", "ideas") back to type names (e.g., "Movie", "Idea") for artifact materialization compatibility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prediction | DSPy Prediction object with semantic field names | required | |
output_group | OutputGroup defining expected outputs | required |
Returns:
Type | Description |
---|---|
dict[str, Any] | Dict mapping type names to extracted values |
Examples:
Prediction(movie={...}, summary={...}) β {"Movie": {...}, "Summary": {...}}
Prediction(ideas=[{...}, {...}, {...}]) β {"Idea": [{...}, {...}, {...}]}