Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix(adapters): serialize non-JSON values one at a time in serialize_for_json (#10634)

Isaac Miller2151e3aedb38
diff --git a/dspy/adapters/utils.py b/dspy/adapters/utils.py
index 1e30d89bb..7f63b175d 100644
--- a/dspy/adapters/utils.py
+++ b/dspy/adapters/utils.py
@@ -71,11 +71,11 @@ def serialize_for_json(value: Any) -> Any:
Returns:
The formatted value, which is serializable as a JSON string.
"""
- # Attempt to format the value as a JSON-compatible object using pydantic, falling back to
- # a string representation of the value if that fails (e.g. if the value contains an object
- # that pydantic doesn't recognize or can't serialize)
+ # Format the value as a JSON-compatible object using pydantic. A nested value pydantic can't
+ # serialize (e.g. a numpy scalar) becomes its string form without affecting its siblings; the
+ # whole value falls back to its string form only if pydantic can't handle it at all.
try:
- return TypeAdapter(type(value)).dump_python(value, mode="json")
+ return TypeAdapter(type(value)).dump_python(value, mode="json", fallback=str)
except Exception:
return str(value)
diff --git a/tests/teleprompt/test_gepa_builtin_instruction_proposer.py b/tests/teleprompt/test_gepa_builtin_instruction_proposer.py
index ef0f957a8..e8248562d 100644
--- a/tests/teleprompt/test_gepa_builtin_instruction_proposer.py
+++ b/tests/teleprompt/test_gepa_builtin_instruction_proposer.py
@@ -5,6 +5,7 @@ import logging
from pathlib import Path
from typing import Any
+import numpy as np
import pytest
from pydantic import ValidationError
@@ -643,12 +644,23 @@ def test_examples_reach_adapter_as_structured_values():
seen.append(inputs["examples_with_feedback"])
return super().format(signature, demos, inputs)
+ class Claim:
+ def __str__(self):
+ return "claim #7"
+
history = tool_history("short result")
image = dspy.Image(url="https://example.com/image.png")
repl = REPLHistory().append(code="print(1)", output="1")
examples = [
{
- "Inputs": {"Context": history, "nested": [{"image": image}], "repl": repl},
+ "Inputs": {
+ "Context": history,
+ "nested": [{"image": image}],
+ "repl": repl,
+ # Values pydantic can't serialize, e.g. a numpy scalar read from a pandas row.
+ "amount": np.int64(1200),
+ "claim": Claim(),
+ },
"Generated Outputs": {"answer": "cat"},
"Feedback": "ok",
}
@@ -660,6 +672,9 @@ def test_examples_reach_adapter_as_structured_values():
assert seen[0][0]["Inputs"]["repl"] is repl
content = lm.history[-1]["messages"][-1]["content"]
assert any(part.get("type") == "image_url" for part in content)
+ text = "".join(part.get("text", "") for part in content)
+ assert '"amount": "1200", "claim": "claim #7"' in text
+ assert '"Feedback": "ok"' in text
@pytest.mark.parametrize("builtin", [True, False])