[Bug] Reserved <<CUSTOM-TYPE-*-IDENTIFIER>> markers leak into assistant messages when a custom Type is an output field in demos or History
What happened?
Custom dspy.Types whose format() returns a list (dspy.Image, dspy.Audio, dspy.File, dspy.experimental.Citations) serialize to <<CUSTOM-TYPE-START-IDENTIFIER>>[...]<<CUSTOM-TYPE-END-IDENTIFIER>>. Adapter.format expands those markers into content blocks only for user messages: _expand_legacy_custom_type_markers_in_chat_message in dspy/adapters/_legacy_type_markers.py returns early when role != "user".
So when such a type is an output field of a few-shot demo, or appears in an assistant turn of dspy.History, the assistant message is sent to the LM with the internal identifiers verbatim. Both ChatAdapter and JSONAdapter are affected (JSONAdapter emits the marker string as a JSON string value). The system prompt asks the LM to follow the field's JSON schema, so the demo contradicts the instructions, and models can echo the markers back, which then fails parsing.
tests/adapters/test_chat_adapter.py::test_chat_adapter_format_exact_messages_with_citations_output_demo currently snapshots the leaked markers as expected output.
Expected: non-user messages never contain the reserved identifiers; the demo shows the serialized payload (e.g. [{"type": "char_location", ...}]).
Proposed fix: for non-user messages, unwrap each marker to its payload instead of returning the message unchanged (about 10 lines in _legacy_type_markers.py, plus regression tests and the snapshot update). I'm aware #10401 rewrites this layer for 3.5; this would be a minimal patch for 3.4.x. I have it passing locally (tests/adapters tests/signatures tests/predict: 993 passed) and can open a PR.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); I reviewed the report before filing it.
Steps to reproduce
No API key needed, only adapter.format:
import json
import dspy
import dspy.experimental
MARK = "CUSTOM-TYPE-START-IDENTIFIER"
sig = dspy.make_signature({
"question": (str, dspy.InputField()),
"answer": (str, dspy.OutputField()),
"citations": (dspy.experimental.Citations, dspy.OutputField()),
})
demo = dspy.Example(
question="Capital of France?", answer="Paris",
citations=dspy.experimental.Citations(citations=[{"cited_text": "Paris is the capital.", "document_index": 0, "start_char_index": 0, "end_char_index": 21}]),
).with_inputs("question")
for adapter in (dspy.ChatAdapter(), dspy.JSONAdapter()):
messages = adapter.format(sig, [demo], {"question": "Capital of Spain?"})
assistant = [m for m in messages if m["role"] == "assistant"][0]["content"]
print(type(adapter).__name__, "marker leaked:", MARK in json.dumps(assistant))
print(assistant)Output on 3.4.0 / main:
ChatAdapter marker leaked: True
[[ ## answer ## ]]
Paris
[[ ## citations ## ]]
<<CUSTOM-TYPE-START-IDENTIFIER>>[{"type": "char_location", "cited_text": "Paris is the capital.", "document_index": 0, "start_char_index": 0, "end_char_index": 21}]<<CUSTOM-TYPE-END-IDENTIFIER>>
[[ ## completed ## ]]
JSONAdapter marker leaked: True
{
"answer": "Paris",
"citations": "<<CUSTOM-TYPE-START-IDENTIFIER>>[{\"type\": \"char_location\", ...}]<<CUSTOM-TYPE-END-IDENTIFIER>>"
}The same happens for an assistant turn of dspy.History that contains a Citations or dspy.Image value.
DSPy version
3.4.0 (also main @ 8de96ea), Python 3.12.13, macOS 15.3.1
Source: stanfordnlp/dspy