Repository navigation
fix(adapters): serialize non-JSON values one at a time in serialize_for_json - #10634
Merged
Merged
Conversation
…or_json One value pydantic can't serialize (e.g. a numpy scalar from a pandas row) made serialize_for_json fall back to str() of the whole value. With InstructionProposer as GEPA's default, this turned every reflection example into a Python repr, dropped dspy.Image parts, and ignored custom __str__. Pass fallback=str so only the offending value is stringified. Amp-Thread-ID: https://ampcode.com/threads/T-01a12205-bc16-7750-9c23-8d86826ea0ec Co-authored-by: Isaac Miller <isaac@cmpnd.ai>
Contributor
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Changes Description
Since #10555 made
InstructionProposerGEPA's default, the reflective dataset keeps raw input and output values and the adapter renders them throughserialize_for_json. If any one value can't be serialized by pydantic, for example anumpy.int64read from a pandas row,serialize_for_jsonfalls back tostr()of the whole examples list. When that happens:'claim_amount': np.int64(1200), 'photo': Image(url=...));dspy.Imageinputs reach the reflection LM as repr text instead of image parts;__str__.This PR passes
fallback=strto the existingTypeAdapter(...).dump_python(...)call (supported since pydantic 2.11, DSPy's minimum), so only the value pydantic can't serialize becomesstr(value). Values that already serialize go through the same call and render unchanged. A top-level unknown value, or a serializer that raises, still falls back tostr()of the whole value as before. Other adapters benefit the same way: a container holding a numpy scalar now renders as JSON instead of a repr. Numpy scalars render as JSON strings ("1200"), matching 3.4.0's per-valuestr().Testing:
test_examples_reach_adapter_as_structured_valuesnow includes anumpy.int64and an object with a custom__str__next to a nesteddspy.Image. It fails on main (no image part).pytest tests/adapters tests/teleprompt/test_gepa*.py tests/primitives tests/predict→ 1131 passed, 267 skipped. Also checked with a pandas +dspy.ImageGEPA repro and a liveInstructionProposercall on gpt-4o.AI disclosure: written with Amp (Claude), in a maintainer-directed session. The prompt was to find existing DSPy code that formats values one at a time and reuse it to fix this regression with a minimal diff.
✅ Contributor Checklist
None.