Repository navigation
fix(dspy): keep fields when copying an Example with no visible fields - #10637
Zhuoxi2000 wants to merge 1 commit into
Conversation
Example.__init__ guarded the base branch with `if base and ...`. Since
__len__ skips dspy_* keys, an Example that only holds dspy_* metadata or
an input/label split (e.g. `Example().with_inputs("question")`) is falsy,
so copy(), without() and Example(base=...) silently dropped its store and
input keys. Check the type only, as the isinstance test already rules
out None.
|
📝 Changes Description
Example.__init__checksif base and isinstance(base, ...).__len__skipsdspy_*keys, so an Example that holds onlydspy_*metadata, or only an input split such asExample().with_inputs("question"), is falsy. In that casecopy(),without()andExample(base=...)return a blank Example and give no warning:This follows up #9946, which made copies keep the input/label split but did not change the truthiness check. This PR drops
base andfrom both branches.isinstancealready excludesNone, and an empty dict still gives an empty store.Regression test:
test_example_copy_preserves_fields_when_example_has_no_visible_fieldsintests/primitives/test_example.py. It fails on currentmain(2151e3a) withassert None == {'question'}and passes with this change.tests/primitives,tests/predict,tests/teleprompt,tests/evaluate,tests/datasetsandtests/adapterspass (1268 passed, 295 skipped), andruff check(v0.15.0) is clean on the changed files.✅ Contributor Checklist
None. Only falsy
Examplebases change behaviour. A falsy dict base gave an empty store before this change and still does.AI assistance
I used Claude (Claude Code) to look for correctness gaps in
dspy/primitives/example.py. It flagged the truthiness check and drafted the failing test and the two-line fix. I reproduced the failure onmain, reviewed the change, and can explain it. Prompts, roughly: