#10498·dspy

ChatAdapter JSON fallback re-calls the LM on Literal/validation errors and masks the real error

Author: michaelkillgtaCreated Sep 25, 2026Updated Sep 25, 2026

Describe the bug

With default use_json_adapter_fallback=True, ChatAdapter treats type-validation AdapterParseErrors (e.g. wrong Literal member in an otherwise well-formed chat response) as format failures and re-calls the LM via JSONAdapter.

That:

  1. Causes an extra LM call (cost) when the chat format was fine
  2. Often replaces a clear Literal/validation error with LM response cannot be serialized to a JSON object

Docstring says the fallback is for invalid format.

To Reproduce

python
import dspy
from dspy.utils.dummies import DummyLM
from dspy.adapters.chat_adapter import ChatAdapter
from typing import Literal

class Color(dspy.Signature):
    q: str = dspy.InputField()
    color: Literal["red", "blue"] = dspy.OutputField()

class CountingLM(DummyLM):
    def __init__(self, *a, **k):
        super().__init__(*a, **k)
        self.calls = 0
    def forward(self, *a, **k):
        self.calls += 1
        return super().forward(*a, **k)

lm = CountingLM([{"color": "green"}, {"color": "green"}])
dspy.configure(lm=lm, adapter=ChatAdapter(use_json_adapter_fallback=True))
try:
    dspy.Predict(Color)(q="pick")  # well-formed [[ ## color ## ]] green
except Exception as e:
    print(lm.calls, str(e).splitlines()[0])
# → calls=2, JSON serialization error (Literal error lost)

# Same with use_json_adapter_fallback=False → 1 call, clear Literal validation error

Expected behavior

Well-formed Chat markers with a value outside Literal[...] fail once with the pydantic/Literal error; no second LM call.

Environment

  • dspy==3.4.0
  • Python 3.13

Additional context

ChatAdapter.parse wraps parse_value failures as AdapterParseError; ChatAdapter.__call__ then falls back for all AdapterParseErrors with no distinction between missing markers vs typed value invalid. Related but different from #7693 (retries feature) / #7925 (partial constraints).