Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Preserve output-format reminders in History replay (#10554)

Isaac Millerf207d35cf477
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.py
index 96d565adb..028df7a0d 100644
--- a/dspy/adapters/base.py
+++ b/dspy/adapters/base.py
@@ -542,7 +542,11 @@ class Adapter:
else None
)
- user_content = self.format_user_message_content(signature, message)
+ # Preserve the format reminder from the original request for prompt-cache reuse.
+ # Output-only events must not acquire a synthetic user message containing just the reminder.
+ user_content = self.format_user_message_content(
+ signature, message, main_request=any(name in message for name in signature.input_fields)
+ )
if user_content:
messages.append({"role": "user", "content": user_content})
diff --git a/tests/adapters/test_chat_adapter.py b/tests/adapters/test_chat_adapter.py
index bc240df26..81a264e9f 100644
--- a/tests/adapters/test_chat_adapter.py
+++ b/tests/adapters/test_chat_adapter.py
@@ -370,9 +370,13 @@ def test_chat_adapter_format_exact_messages_with_history():
"[[ ## completed ## ]]\n"
"In adhering to this structure, your objective is: \n"
" Given the fields `history`, `question`, produce the fields `answer`."},
- {"role": "user", "content": "[[ ## question ## ]]\nWhat is 1+1?"},
+ {"role": "user", "content": "[[ ## question ## ]]\nWhat is 1+1?\n\n"
+ "Respond with the corresponding output fields, starting with the field `[[ ## answer ## ]]`, "
+ "and then ending with the marker for `[[ ## completed ## ]]`."},
{"role": "assistant", "content": "[[ ## answer ## ]]\n2\n\n[[ ## completed ## ]]\n"},
- {"role": "user", "content": "[[ ## question ## ]]\nWhat is 2+2?"},
+ {"role": "user", "content": "[[ ## question ## ]]\nWhat is 2+2?\n\n"
+ "Respond with the corresponding output fields, starting with the field `[[ ## answer ## ]]`, "
+ "and then ending with the marker for `[[ ## completed ## ]]`."},
{"role": "assistant", "content": "[[ ## answer ## ]]\n4\n\n[[ ## completed ## ]]\n"},
{"role": "user",
"content": "[[ ## question ## ]]\n"
@@ -669,7 +673,10 @@ def test_chat_adapter_format_exact_messages_with_history_demo_pydantic_tools_and
'["math", "machines"]}\n'
'\n'
'[[ ## question ## ]]\n'
- 'Who is Ada?'},
+ 'Who is Ada?\n\n'
+ 'Respond with the corresponding output fields, starting with the field `[[ ## answer ## ]]` '
+ '(must be formatted as a valid Python AnswerCard), and then ending with the marker '
+ 'for `[[ ## completed ## ]]`.'},
{"role": "assistant",
"content": '[[ ## answer ## ]]\n'
'{"answer": "Ada is a mathematician.", "sources": ["memory"]}\n'
@@ -1589,7 +1596,10 @@ def test_chat_adapter_format_exact_messages_with_non_native_tool_history():
" Given the fields `question`, `history`, `tools`, produce the fields `next_thought`, "
"`tool_calls`.",
},
- {"role": "user", "content": "[[ ## question ## ]]\nQ1"},
+ {"role": "user", "content": "[[ ## question ## ]]\nQ1\n\n"
+ "Respond with the corresponding output fields, starting with the field `[[ ## next_thought ## ]]`, then "
+ '`[[ ## tool_calls ## ]]` (must be a JSON object like {"tool_calls": [{"name": "...", "args": {...}}]}), '
+ "and then ending with the marker for `[[ ## completed ## ]]`."},
{
"role": "assistant",
"content": "[[ ## next_thought ## ]]\n"
@@ -1994,7 +2004,12 @@ def test_chat_adapter_format_exact_messages_kitchen_sink():
'[2] «older note»\n'
'\n'
'[[ ## question ## ]]\n'
- 'Who is Ada?'},
+ 'Who is Ada?\n\n'
+ 'Respond with the corresponding output fields, starting with the field `[[ ## answer ## ]]` '
+ '(must be formatted as a valid Python AnswerCard), then `[[ ## verdict ## ]]` '
+ "(must be formatted as a valid Python Literal['yes', 'no']), then `[[ ## confidence ## ]]` "
+ '(must be formatted as a valid Python float), and then ending with the marker '
+ 'for `[[ ## completed ## ]]`.'},
{"role": "assistant",
"content": '[[ ## answer ## ]]\n'
'{"answer": "Ada is a mathematician.", "sources": ["memory"]}\n'
@@ -2380,6 +2395,23 @@ def test_citations_output_field_keeps_json_schema_in_prompt():
assert "Type description of Citations" in system_content
[email protected]("adapter", [dspy.ChatAdapter(), dspy.JSONAdapter(), dspy.XMLAdapter()])
+def test_history_replay_preserves_previous_request_prefix(adapter):
+ class Conversation(dspy.Signature):
+ question: str = dspy.InputField()
+ history: dspy.History = dspy.InputField()
+ answer: str = dspy.OutputField()
+
+ history = dspy.History(messages=[])
+ previous = None
+ for question, answer in [("What is 3+4?", "7"), ("Double that?", "14"), ("Subtract five?", "9")]:
+ messages = adapter.format(Conversation, [], {"question": question, "history": history})
+ if previous is not None:
+ assert messages[:len(previous)] == previous
+ history.messages.append({"question": question, "answer": answer})
+ previous = messages
+
+
def test_chat_adapter_formats_conversation_history():
class MySignature(dspy.Signature):
question: str = dspy.InputField()
@@ -2397,9 +2429,13 @@ def test_chat_adapter_formats_conversation_history():
messages = adapter.format(MySignature, [], {"question": "What is the capital of France?", "history": history})
assert len(messages) == 6
- assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?"
+ reminder = (
+ "\n\nRespond with the corresponding output fields, starting with the field `[[ ## answer ## ]]`, "
+ "and then ending with the marker for `[[ ## completed ## ]]`."
+ )
+ assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?" + reminder
assert messages[2]["content"] == "[[ ## answer ## ]]\nParis\n\n[[ ## completed ## ]]\n"
- assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?"
+ assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?" + reminder
assert messages[4]["content"] == "[[ ## answer ## ]]\nBerlin\n\n[[ ## completed ## ]]\n"
diff --git a/tests/adapters/test_json_adapter.py b/tests/adapters/test_json_adapter.py
index 5d847733b..9cd9c1375 100644
--- a/tests/adapters/test_json_adapter.py
+++ b/tests/adapters/test_json_adapter.py
@@ -305,7 +305,9 @@ def test_json_adapter_format_exact_messages_with_history_demo_pydantic_tools_and
'["math", "machines"]}\n'
'\n'
'[[ ## question ## ]]\n'
- 'Who is Ada?'},
+ 'Who is Ada?\n\n'
+ 'Respond with a JSON object in the following order of fields: `answer` '
+ '(must be formatted as a valid Python AnswerCard).'},
{"role": "assistant",
"content": '{\n'
' "answer": {\n'
@@ -1226,9 +1228,10 @@ def test_json_adapter_formats_conversation_history():
messages = adapter.format(MySignature, [], {"question": "What is the capital of France?", "history": history})
assert len(messages) == 6
- assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?"
+ reminder = "\n\nRespond with a JSON object in the following order of fields: `answer`."
+ assert messages[1]["content"] == "[[ ## question ## ]]\nWhat is the capital of France?" + reminder
assert messages[2]["content"] == '{\n "answer": "Paris"\n}'
- assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?"
+ assert messages[3]["content"] == "[[ ## question ## ]]\nWhat is the capital of Germany?" + reminder
assert messages[4]["content"] == '{\n "answer": "Berlin"\n}'
diff --git a/tests/adapters/test_xml_adapter.py b/tests/adapters/test_xml_adapter.py
index 36340e3f2..4d69c0534 100644
--- a/tests/adapters/test_xml_adapter.py
+++ b/tests/adapters/test_xml_adapter.py
@@ -829,7 +829,10 @@ def test_xml_adapter_format_exact_messages_with_history_demo_pydantic_tools_and_
'\n'
'<question>\n'
'Who is Ada?\n'
- '</question>'},
+ '</question>\n\n'
+ 'Respond with the corresponding output fields wrapped in XML tags `<answer>`. '
+ 'Use this nested XML structure: '
+ '<answer><answer>...</answer><sources><item>...</item></sources></answer>'},
{"role": "assistant",
"content": "<answer><answer>Ada is a mathematician.</answer><sources><item>memory</item></sources></answer>"},
{"role": "user",