Skip to content

Commit 4a12acc

Browse files
abossardCopilot
andcommitted
Fix _format_optimized_prompt for different dump_state return types
dump_state() can return a list or dict depending on DSPy version. Handle both gracefully with type checks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 14dce2f commit 4a12acc

1 file changed

Lines changed: 26 additions & 18 deletions

File tree

notebooks/dspy_tasks/actions.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,35 +152,43 @@ def _current_model() -> str:
152152

153153
def _format_optimized_prompt(module) -> str:
154154
"""Format an optimized DSPy module's state into a human-readable string."""
155-
if not hasattr(module, 'dump_state'):
155+
try:
156+
state = module.dump_state()
157+
except Exception:
156158
return "(optimized)"
157159

158-
state = module.dump_state()
159-
parts = []
160+
# dump_state() may return a dict or a list depending on DSPy version
161+
if isinstance(state, list):
162+
return str(state)[:500]
163+
if not isinstance(state, dict):
164+
return str(state)[:500]
160165

166+
parts = []
161167
for predictor_name, predictor_state in state.items():
162-
# Extract instructions
168+
if not isinstance(predictor_state, dict):
169+
continue
163170
sig = predictor_state.get('signature', {})
164-
instructions = sig.get('instructions', '')
165-
if instructions:
166-
parts.append("━━━ INSTRUCTIONS ━━━")
167-
parts.append(instructions)
171+
if isinstance(sig, dict):
172+
instructions = sig.get('instructions', '')
173+
if instructions:
174+
parts.append("━━━ INSTRUCTIONS ━━━")
175+
parts.append(str(instructions))
168176

169-
# Extract demos (few-shot examples)
170177
demos = predictor_state.get('demos', [])
171-
if demos:
178+
if demos and isinstance(demos, list):
172179
parts.append(f"\n━━━ FEW-SHOT EXAMPLES ({len(demos)}) ━━━")
173180
for i, demo in enumerate(demos):
174181
parts.append(f"\n Example {i+1}:")
175-
for key, value in demo.items():
176-
if key == 'augmented':
177-
continue
178-
val_str = str(value)
179-
if len(val_str) > 200:
180-
val_str = val_str[:200] + "..."
181-
parts.append(f" {key}: {val_str}")
182+
if isinstance(demo, dict):
183+
for key, value in demo.items():
184+
if key == 'augmented':
185+
continue
186+
val_str = str(value)
187+
if len(val_str) > 200:
188+
val_str = val_str[:200] + "..."
189+
parts.append(f" {key}: {val_str}")
182190

183-
return "\n".join(parts) if parts else str(state)
191+
return "\n".join(parts) if parts else str(state)[:500]
184192

185193

186194
def _make_signature(base_sig, instructions: str):

0 commit comments

Comments
 (0)