DSPy: The framework for programming—not prompting—language models
Python
fix: XML adapter's markers (#8876)
Mathieu8224a99ca640
diff --git a/dspy/adapters/xml_adapter.py b/dspy/adapters/xml_adapter.pyindex 5c83cc41d..c0971b52d 100644--- a/dspy/adapters/xml_adapter.py+++ b/dspy/adapters/xml_adapter.py@@ -1,8 +1,10 @@import refrom typing import Any+from pydantic.fields import FieldInfo+from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName-from dspy.adapters.utils import format_field_value+from dspy.adapters.utils import format_field_value, translate_field_typefrom dspy.signatures.signature import Signaturefrom dspy.utils.callback import BaseCallback@@ -19,10 +21,43 @@ class XMLAdapter(ChatAdapter):output.append(f"<{field.name}>\n{formatted}\n</{field.name}>")return "\n\n".join(output).strip()+ def format_field_structure(self, signature: type[Signature]) -> str:+ """+ XMLAdapter requires input and output fields to be wrapped in XML tags like `<field_name>`.+ """++ parts = []+ parts.append("All interactions will be structured in the following way, with the appropriate values filled in.")++ def format_signature_fields_for_instructions(fields: dict[str, FieldInfo]):+ return self.format_field_with_value(+ fields_with_values={+ FieldInfoWithName(name=field_name, info=field_info): translate_field_type(field_name, field_info)+ for field_name, field_info in fields.items()+ },+ )++ parts.append(format_signature_fields_for_instructions(signature.input_fields))+ parts.append(format_signature_fields_for_instructions(signature.output_fields))+ return "\n\n".join(parts).strip()++ def format_assistant_message_content(+ self,+ signature: type[Signature],+ outputs: dict[str, Any],+ missing_field_message=None,+ ) -> str:+ return self.format_field_with_value(+ {+ FieldInfoWithName(name=k, info=v): outputs.get(k, missing_field_message)+ for k, v in signature.output_fields.items()+ },+ )+def user_message_output_requirements(self, signature: type[Signature]) -> str:message = "Respond with the corresponding output fields wrapped in XML tags"message += ", then ".join(f"`<{f}>`" for f in signature.output_fields)- message += ", and then end with the `<completed>` tag."+ message += "."return messagedef parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:diff --git a/tests/adapters/test_xml_adapter.py b/tests/adapters/test_xml_adapter.pyindex 51c744bb4..644b7d688 100644--- a/tests/adapters/test_xml_adapter.py+++ b/tests/adapters/test_xml_adapter.py@@ -261,3 +261,39 @@ def test_xml_adapter_with_code():{"question": "Write a python program to print 'Hello, world!'"},)assert result[0]["code"].code == 'print("Hello, world!")'+++def test_xml_adapter_full_prompt():+ class QA(dspy.Signature):+ query: str = dspy.InputField()+ context: str | None = dspy.InputField()+ answer: str = dspy.OutputField()++ adapter = dspy.XMLAdapter()+ messages = adapter.format(QA, [], {"query": "when was Marie Curie born"})++ assert len(messages) == 2+ assert messages[0]["role"] == "system"+ assert messages[1]["role"] == "user"++ expected_system = (+ "Your input fields are:\n"+ "1. `query` (str): \n"+ "2. `context` (UnionType[str, NoneType]):\n"+ "Your output fields are:\n"+ "1. `answer` (str):\n"+ "All interactions will be structured in the following way, with the appropriate values filled in.\n\n"+ "<query>\n{query}\n</query>\n\n"+ "<context>\n{context}\n</context>\n\n"+ "<answer>\n{answer}\n</answer>\n"+ "In adhering to this structure, your objective is: \n"+ " Given the fields `query`, `context`, produce the fields `answer`."+ )++ expected_user = (+ "[[ ## query ## ]]\nwhen was Marie Curie born\n\n"+ "Respond with the corresponding output fields wrapped in XML tags"+ )++ assert messages[0]["content"] == expected_system+ assert messages[1]["content"] == expected_userdiff --git a/tests/streaming/test_streaming.py b/tests/streaming/test_streaming.pyindex 9dbde86b5..eabd43ae4 100644--- a/tests/streaming/test_streaming.py+++ b/tests/streaming/test_streaming.py@@ -869,9 +869,6 @@ async def test_stream_listener_returns_correct_chunk_xml_adapter():yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="<"))])yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="/answer"))])yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=">"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="<"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="completed"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=">"))])async def xml_stream_2(*args, **kwargs):yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="<"))])@@ -885,9 +882,6 @@ async def test_stream_listener_returns_correct_chunk_xml_adapter():yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="<"))])yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="/judgement"))])yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=">"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="<"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content="completed"))])- yield ModelResponseStream(model="gpt-4o-mini", choices=[StreamingChoices(delta=Delta(content=">"))])stream_generators = [xml_stream_1, xml_stream_2]