Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

feat(adapter): support nested XML data (#10239)

Isaac Miller33aaa19e03c3
diff --git a/docs/docs/diving-deeper/adapters.md b/docs/docs/diving-deeper/adapters.md
index 552595d27..f7a61a8b5 100644
--- a/docs/docs/diving-deeper/adapters.md
+++ b/docs/docs/diving-deeper/adapters.md
@@ -65,7 +65,7 @@ The default. Builds a chat-style prompt with field markers, parses the response
Outputs structured JSON. Internally extends ChatAdapter — formatting is similar, but the output instruction asks for JSON and parsing uses `json_repair`. The constructor’s `use_native_function_calling=True` default flips when tool calling is wired in.
**`dspy.XMLAdapter(callbacks=None)`**
-`<field_name>value</field_name>` tags. The parser is a regex (`r"<(\w+)>(.*?)</\1>"` with `DOTALL`); it’s robust to whitespace but doesn’t tolerate nested tags of the same name.
+`<field_name>value</field_name>` tags. Lists use repeated `<item>` tags.
**`dspy.TwoStepAdapter(extraction_model: BaseLM, **kwargs)`**
Two LM calls per inference. Use it when the main LM is a reasoning model that’s bad at formatting — the extractor is usually a cheap general-purpose LM with ChatAdapter. Doesn’t support finetuning yet.
diff --git a/dspy/adapters/xml_adapter.py b/dspy/adapters/xml_adapter.py
index 654fc2f70..e3617e560 100644
--- a/dspy/adapters/xml_adapter.py
+++ b/dspy/adapters/xml_adapter.py
@@ -1,42 +1,60 @@
-import re
-from typing import Any
+import types
+import xml.etree.ElementTree as ET
+from collections import defaultdict
+from typing import Any, Union, get_args, get_origin
+from xml.sax.saxutils import quoteattr
-from pydantic.fields import FieldInfo
+import pydantic
+from pydantic import TypeAdapter
+from typing_extensions import is_typeddict
from dspy.adapters.chat_adapter import ChatAdapter, FieldInfoWithName
-from dspy.adapters.utils import apply_output_field_defaults, format_field_value, translate_field_type
+from dspy.adapters.utils import (
+ apply_output_field_defaults,
+ format_field_value,
+ parse_value,
+ serialize_for_json,
+ translate_field_type,
+)
from dspy.signatures.signature import Signature
+from dspy.utils.exceptions import AdapterParseError
class XMLAdapter(ChatAdapter):
- field_pattern = re.compile(r"<(?P<name>\w+)>((?P<content>.*?))</\1>", re.DOTALL)
-
def format_field_with_value(self, fields_with_values: dict[FieldInfoWithName, Any]) -> str:
output = []
- for field, field_value in fields_with_values.items():
- formatted = format_field_value(field_info=field.info, value=field_value)
+ for field, value in fields_with_values.items():
+ serialized = serialize_for_json(value)
+ is_output = (field.info.json_schema_extra or {}).get("__dspy_field_type") == "output"
+ if is_output and self._uses_nested_xml(field.info.annotation) and isinstance(serialized, (dict, list)):
+ output.append(self._value_to_xml(serialized, field.name))
+ continue
+ formatted = format_field_value(field_info=field.info, value=value)
+ if is_output and field.info.annotation is str:
+ formatted = formatted.replace("&", "&amp;").replace("<", "&lt;")
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]):
+ def format_field(name, field):
+ if (field.json_schema_extra or {}).get("__dspy_field_type") == "output" and self._uses_nested_xml(
+ field.annotation
+ ):
+ return self._xml_schema(name, field.annotation)
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()
- },
+ {FieldInfoWithName(name=name, info=field): translate_field_type(name, field)}
)
- 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()
+ fields = (
+ "\n\n".join(format_field(name, field) for name, field in group.items())
+ for group in (signature.input_fields, signature.output_fields)
+ )
+ return "\n\n".join(
+ (
+ "All interactions will be structured in the following way, with the appropriate values filled in.",
+ *fields,
+ )
+ )
def format_user_message_content(
self,
@@ -46,75 +64,177 @@ class XMLAdapter(ChatAdapter):
suffix: str = "",
main_request: bool = False,
) -> str:
- messages = [prefix]
-
- messages.append(self.format_field_with_value(
- {
- FieldInfoWithName(name=k, info=v): inputs.get(k)
- for k, v in signature.input_fields.items() if k in inputs
- },
- ))
-
+ fields = {
+ FieldInfoWithName(name=k, info=v): inputs[k] for k, v in signature.input_fields.items() if k in inputs
+ }
+ messages = [prefix, self.format_field_with_value(fields)]
if main_request:
- output_requirements = self.user_message_output_requirements(signature)
- if output_requirements is not None:
- messages.append(output_requirements)
-
- messages.append(suffix)
- return "\n\n".join(messages).strip()
+ messages.append(self.user_message_output_requirements(signature))
+ return "\n\n".join((*messages, suffix)).strip()
def format_assistant_message_content(
- self,
- signature: type[Signature],
- outputs: dict[str, Any],
- missing_field_message=None,
+ 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()
- },
- )
+ fields = {
+ FieldInfoWithName(name=k, info=v): outputs.get(k, missing_field_message)
+ for k, v in signature.output_fields.items()
+ }
+ return self.format_field_with_value(fields)
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 += "."
- return message
+ fields = ", then ".join(f"`<{name}>`" for name in signature.output_fields)
+ schemas = [
+ self._xml_schema(name, field.annotation)
+ for name, field in signature.output_fields.items()
+ if self._uses_nested_xml(field.annotation)
+ ]
+ return f"Respond with the corresponding output fields wrapped in XML tags {fields}." + (
+ f" Use this nested XML structure: {' '.join(schemas)}" if schemas else ""
+ )
def parse(self, signature: type[Signature], completion: str) -> dict[str, Any]:
- fields = {}
- for match in self.field_pattern.finditer(completion):
- name = match.group("name")
- content = match.group("content").strip()
- if name in signature.output_fields and name not in fields:
- fields[name] = content
- # Cast values using base class parse_value helper
- for k, v in fields.items():
- fields[k] = self._parse_field_value(signature.output_fields[k], v, completion, signature)
- fields = apply_output_field_defaults(signature, fields)
- if fields.keys() != signature.output_fields.keys():
- from dspy.utils.exceptions import AdapterParseError
-
+ try:
+ root = ET.fromstring(f"<dspy_root>{completion}</dspy_root>")
+ except ET.ParseError as e:
raise AdapterParseError(
adapter_name="XMLAdapter",
signature=signature,
lm_response=completion,
- parsed_result=fields,
+ message=f"Failed to parse XML: {e}",
+ ) from e
+ elements = self._group_children(root)
+ fields = {}
+ for name, field in signature.output_fields.items():
+ if name not in elements:
+ continue
+ adapter = TypeAdapter(field.annotation)
+ schema = adapter.json_schema(by_alias=False)
+ value = None
+ for candidate in [schema, *schema.get("anyOf", [])]:
+ try:
+ value = self._elements_to_value(elements[name], candidate, schema.get("$defs", {}))
+ try:
+ fields[name] = parse_value(value, field.annotation)
+ except pydantic.ValidationError:
+ fields[name] = adapter.validate_python(value, by_name=True)
+ break
+ except Exception as e:
+ error = e
+ else:
+ raise AdapterParseError(
+ adapter_name="XMLAdapter",
+ signature=signature,
+ lm_response=completion,
+ message=f"Failed to parse field {field} with value {value}: {error}",
+ ) from error
+ fields = apply_output_field_defaults(signature, fields)
+ if fields.keys() != signature.output_fields.keys():
+ raise AdapterParseError(
+ adapter_name="XMLAdapter", signature=signature, lm_response=completion, parsed_result=fields
)
return fields
- def _parse_field_value(self, field_info, raw, completion, signature):
- from dspy.adapters.utils import parse_value
-
- try:
- return parse_value(raw, field_info.annotation)
- except Exception as e:
- from dspy.utils.exceptions import AdapterParseError
+ @classmethod
+ def _value_to_xml(cls, value: Any, tag: str, key: str | None = None) -> str:
+ attrs = f" key={quoteattr(key)}" if key is not None else ""
+ if isinstance(value, list):
+ children = "".join(cls._value_to_xml(item, "item") for item in value)
+ return f"<{tag}{attrs}>{children}</{tag}>" if children else f"<{tag}{attrs} />"
+ if isinstance(value, dict):
+ children = []
+ for name, child in value.items():
+ name = str(name)
+ valid_name = (name[:1] + name[1:].replace("-", "_").replace(".", "_")).isidentifier()
+ children.append(
+ cls._value_to_xml(child, name) if valid_name else cls._value_to_xml(child, "entry", name)
+ )
+ return f"<{tag}{attrs}>{''.join(children)}</{tag}>"
+ return (
+ f"<{tag}{attrs}>{str(value).replace('&', '&amp;').replace('<', '&lt;')}</{tag}>"
+ if value is not None
+ else f"<{tag}{attrs} />"
+ )
- raise AdapterParseError(
- adapter_name="XMLAdapter",
- signature=signature,
- lm_response=completion,
- message=f"Failed to parse field {field_info} with value {raw}: {e}",
+ @classmethod
+ def _xml_schema(cls, tag: str, annotation: Any) -> str:
+ schema = TypeAdapter(annotation).json_schema(by_alias=False)
+ return cls._schema_to_xml(tag, schema, schema.get("$defs", {}), frozenset())
+
+ @classmethod
+ def _schema_to_xml(cls, tag: str, schema: dict, definitions: dict, seen: frozenset[str]) -> str:
+ if ref := schema.get("$ref"):
+ name = ref.rsplit("/", 1)[-1]
+ return (
+ f"<{tag}>...</{tag}>"
+ if name in seen
+ else cls._schema_to_xml(tag, definitions[name], definitions, seen | {name})
)
+ if choices := schema.get("anyOf"):
+ return cls._schema_to_xml(
+ tag, next((s for s in choices if s.get("type") != "null"), choices[0]), definitions, seen
+ )
+ if schema.get("type") == "array":
+ item = cls._schema_to_xml("item", schema.get("items", {}), definitions, seen)
+ return f"<{tag}>{item}</{tag}>"
+ children = "".join(
+ cls._schema_to_xml(name, child, definitions, seen) for name, child in schema.get("properties", {}).items()
+ )
+ return f"<{tag}>{children}</{tag}>" if children else f"<{tag}>...</{tag}>"
+
+ @classmethod
+ def _elements_to_value(cls, elements: list[ET.Element], schema: dict, definitions: dict) -> Any:
+ schema = definitions.get(schema.get("$ref", "").rsplit("/", 1)[-1], schema)
+ if choices := schema.get("anyOf"):
+ if {"type": "null"} in choices and not list(elements[0]) and not (elements[0].text or "").strip():
+ return None
+ choices = [choice for choice in choices if choice.get("type") != "null"]
+ schema = choices[0]
+ if list(elements[0]):
+ schema = next((choice for choice in choices if choice.get("type") != "string"), schema)
+ if schema.get("type") == "array":
+ if len(elements) == 1 and not list(elements[0]):
+ text = (elements[0].text or "").strip()
+ if not text or text.startswith("["):
+ return [] if not text else text
+ if len(elements) == 1 and (items := cls._group_children(elements[0]).get("item")):
+ elements = items
+ return [cls._elements_to_value([element], schema.get("items", {}), definitions) for element in elements]
+ element = elements[0]
+ if schema.get("type") == "string" and list(element):
+ return (element.text or "") + "".join(ET.tostring(child, encoding="unicode") for child in element)
+ children = cls._group_children(element)
+ if not children:
+ if schema.get("type") == "object" and not (element.text or "").strip():
+ return {}
+ values = [(element.text or "").strip() for element in elements]
+ return values[0] if len(values) == 1 else values
+ properties = schema.get("properties", {})
+ child_schema = schema.get("additionalProperties", {})
+ if not isinstance(child_schema, dict):
+ child_schema = {}
+ return {
+ name: cls._elements_to_value(items, properties.get(name, child_schema), definitions)
+ for name, items in children.items()
+ }
+
+ @staticmethod
+ def _group_children(element: ET.Element) -> dict[str, list[ET.Element]]:
+ children = defaultdict(list)
+ for child in element:
+ children[child.attrib.get("key", child.tag) if child.tag == "entry" else child.tag].append(child)
+ return children
+
+ @staticmethod
+ def _uses_nested_xml(annotation: Any) -> bool:
+ args = [arg for arg in get_args(annotation) if arg is not type(None)]
+ annotation = args[0] if len(args) == 1 and get_origin(annotation) in (Union, types.UnionType) else annotation
+ origin = get_origin(annotation)
+ item = get_args(annotation)[0] if origin is list and get_args(annotation) else annotation
+ is_dspy_model = (
+ isinstance(item, type) and issubclass(item, pydantic.BaseModel) and item.__module__.startswith("dspy.")
+ )
+ return not is_dspy_model and (
+ origin in (list, dict)
+ or is_typeddict(annotation)
+ or (isinstance(annotation, type) and issubclass(annotation, pydantic.BaseModel))
+ )
diff --git a/tests/adapters/test_xml_adapter.py b/tests/adapters/test_xml_adapter.py
index 139bf6394..77de0fc44 100644
--- a/tests/adapters/test_xml_adapter.py
+++ b/tests/adapters/test_xml_adapter.py
@@ -4,6 +4,7 @@ from unittest import mock
import pydantic
import pytest
from litellm import Choices, Message, ModelResponse
+from typing_extensions import TypedDict
import dspy
from dspy.adapters.chat_adapter import FieldInfoWithName
@@ -84,10 +85,195 @@ def test_xml_adapter_parse_raises_on_type_error():
assert "Failed to parse field" in str(e.value)
+def test_xml_adapter_repeated_dict_elements_and_empty_lists():
+ class TestSignature(dspy.Signature):
+ counts: dict[str, list[int]] = dspy.OutputField()
+
+ adapter = XMLAdapter()
+ completion = "<counts><first>3</first><first>4</first><second>5</second></counts>"
+ assert adapter.parse(TestSignature, completion) == {
+ "counts": {"first": [3, 4], "second": [5]},
+ }
+
+ counts = {"postal code": [3, 4], 'quoted "key" & more': [5], "line\nbreak": [6], "-status": [7], ".status": [8]}
+ field = FieldInfoWithName(name="counts", info=TestSignature.output_fields["counts"])
+ formatted = adapter.format_field_with_value({field: counts})
+ assert '<entry key="postal code"><item>3</item><item>4</item></entry>' in formatted
+ assert adapter.parse(TestSignature, formatted) == {"counts": counts}
+ assert adapter.parse(TestSignature, "<counts />") == {"counts": {}}
+
+ class EmptySignature(dspy.Signature):
+ items: list[str] = dspy.OutputField()
+
+ field = FieldInfoWithName(name="items", info=EmptySignature.output_fields["items"])
+ assert adapter.format_field_with_value({field: []}) == "<items />"
+ assert adapter.parse(EmptySignature, "<items />") == {"items": []}
+
+
+def test_xml_adapter_uses_pydantic_field_names_as_xml_tags():
+ class Address(pydantic.BaseModel):
+ postal_code: str = pydantic.Field(alias="postal code")
+ country_code: str = pydantic.Field(alias="country-code")
+
+ class TestSignature(dspy.Signature):
+ address: Address = dspy.OutputField()
+
+ adapter = XMLAdapter()
+ address = Address(**{"postal code": "94305", "country-code": "US"})
+ xml = adapter.format_assistant_message_content(TestSignature, {"address": address})
+
+ assert xml == "<address><postal_code>94305</postal_code><country_code>US</country_code></address>"
+ assert "<address><postal_code>...</postal_code><country_code>...</country_code></address>" in (
+ adapter.format_field_structure(TestSignature)
+ )
+ assert adapter.parse(TestSignature, xml) == {"address": address}
+
+
+def test_xml_adapter_typed_dict_schema_and_parsing():
+ class Address(TypedDict):
+ city: str
+
+ class Order(TypedDict):
+ order_id: int
+ address: Address | None
+ labels: list[str]
+
+ class TestSignature(dspy.Signature):
+ order: Order = dspy.OutputField()
+ orders: list[Order] = dspy.OutputField()
+
+ adapter = XMLAdapter()
+ order_schema = (
+ "<order><order_id>...</order_id><address><city>...</city></address>"
+ "<labels><item>...</item></labels></order>"
+ )
+ orders_schema = (
+ "<orders><item><order_id>...</order_id><address><city>...</city></address>"
+ "<labels><item>...</item></labels></item></orders>"
+ )
+ system_instructions = adapter.format_field_structure(TestSignature)
+ assert f"{order_schema}\n\n{orders_schema}" in system_instructions
+ assert f"Use this nested XML structure: {order_schema} {orders_schema}" in (
+ adapter.user_message_output_requirements(TestSignature)
+ )
+
+ completion = (
+ "<order><order_id>1</order_id><address><city>London</city></address><labels>new</labels></order>"
+ "<orders><order_id>2</order_id><address><city>Paris</city></address><labels>paid</labels></orders>"
+ )
+ assert adapter.parse(TestSignature, completion) == {
+ "order": {"order_id": 1, "address": {"city": "London"}, "labels": ["new"]},
+ "orders": [{"order_id": 2, "address": {"city": "Paris"}, "labels": ["paid"]}],
+ }
+
+
+def test_xml_adapter_parses_nullable_and_structured_union_fields():
+ class Profile(pydantic.BaseModel):
+ name: str
+
+ class Details(TypedDict):
+ label: str
+
+ class First(pydantic.BaseModel):
+ count: int
+
+ class Second(pydantic.BaseModel):
+ labels: list[str]
+
+ class FirstDict(TypedDict):
+ count: int
+
+ class SecondDict(TypedDict):
+ labels: list[str]
+
+ class NumberList(pydantic.BaseModel):
+ value: list[int]
+
+ class TextValue(pydantic.BaseModel):
+ value: str
+
+ class ScalarLabels(pydantic.BaseModel):
+ labels: str
+
+ class ListLabels(pydantic.BaseModel):
+ labels: list[str]
+
+ class Envelope(pydantic.BaseModel):
+ choice: ScalarLabels | ListLabels
+
+ class StringFirstEnvelope(pydantic.BaseModel):
+ child: str | ListLabels
+
+ class TestSignature(dspy.Signature):
+ text: str | None = dspy.OutputField()
+ profile: Profile | None = dspy.OutputField()
+ details: Details | None = dspy.OutputField()
+ model: First | Second = dspy.OutputField()
+ mapping: FirstDict | SecondDict = dspy.OutputField()
+ tied: NumberList | TextValue = dspy.OutputField()
+ nested: Envelope = dspy.OutputField()
+ string_first: StringFirstEnvelope = dspy.OutputField()
+
+ completion = (
+ "<text /><profile /><details />"
+ "<model><labels>one</labels><labels>two</labels></model>"
+ "<mapping><labels>three</labels></mapping>"
+ "<tied><value>later-branch-text</value></tied>"
+ "<nested><choice><labels>first</labels><labels>second</labels></choice></nested>"
+ "<string_first><child><labels>one</labels><labels>two</labels></child></string_first>"
+ )
+ assert XMLAdapter().parse(TestSignature, completion) == {
+ "text": None,
+ "profile": None,
+ "details": None,
+ "model": Second(labels=["one", "two"]),
+ "mapping": {"labels": ["three"]},
+ "tied": TextValue(value="later-branch-text"),
+ "nested": Envelope(choice=ListLabels(labels=["first", "second"])),
+ "string_first": StringFirstEnvelope(child=ListLabels(labels=["one", "two"])),
+ }
+
+
+def test_xml_adapter_recursive_model_schema_terminates():
+ class Node(pydantic.BaseModel):
+ value: str
+ children: list["Node"]
+
+ class TestSignature(dspy.Signature):
+ root: Node = dspy.OutputField()
+
+ adapter = XMLAdapter()
+ assert "<root><value>...</value><children><item>...</item></children></root>" in (
+ adapter.format_field_structure(TestSignature)
+ )
+ completion = "<root><value>parent</value><children><value>child</value><children /></children></root>"
+ assert adapter.parse(TestSignature, completion) == {
+ "root": Node(value="parent", children=[Node(value="child", children=[])])
+ }
+
+
+def test_xml_adapter_escapes_closing_tags_and_rejects_malformed_xml():
+ class TestSignature(dspy.Signature):
+ code: str = dspy.OutputField()
+
+ adapter = XMLAdapter()
+ value = "print('</code> & done')"
+ formatted = adapter.format_assistant_message_content(TestSignature, {"code": value})
+ assert "&lt;/code> &amp; done" in formatted
+ assert adapter.parse(TestSignature, formatted) == {"code": value}
+
+ with pytest.raises(dspy.utils.exceptions.AdapterParseError, match="Failed to parse XML"):
+ adapter.parse(TestSignature, "<code>print('</code>')</code>")
+
+
def test_xml_adapter_format_and_parse_nested_model():
+ class Address(pydantic.BaseModel):
+ city: str
+
class InnerModel(pydantic.BaseModel):
value: int
label: str
+ address: Address
class TestSignature(dspy.Signature):
question: str = dspy.InputField()
@@ -95,23 +281,16 @@ def test_xml_adapter_format_and_parse_nested_model():
adapter = XMLAdapter()
# Format output fields as XML
- fields_with_values = {
- FieldInfoWithName(name="result", info=TestSignature.output_fields["result"]): InnerModel(value=5, label="foo")
- }
+ result = InnerModel(value=5, label="foo", address=Address(city="London"))
+ fields_with_values = {FieldInfoWithName(name="result", info=TestSignature.output_fields["result"]): result}
xml = adapter.format_field_with_value(fields_with_values)
- # The output will be a JSON string inside the XML tag
- assert xml.strip().startswith("<result>")
- assert '"value": 5' in xml
- assert '"label": "foo"' in xml
- assert xml.strip().endswith("</result>")
-
- # Parse XML output (should parse as string, not as model)
- completion = '<result>{"value": 5, "label": "foo"}</result>'
+ assert xml == "<result><value>5</value><label>foo</label><address><city>London</city></address></result>"
+ assert adapter.parse(TestSignature, xml) == {"result": result}
+
+ # Legacy JSON values inside the outer XML field remain supported.
+ completion = '<result>{"value": 5, "label": "foo", "address": {"city": "London"}}</result>'
parsed = adapter.parse(TestSignature, completion)
- # The parse_value helper will try to cast to InnerModel
- assert isinstance(parsed["result"], InnerModel)
- assert parsed["result"].value == 5
- assert parsed["result"].label == "foo"
+ assert parsed == {"result": result}
def test_xml_adapter_format_and_parse_list_of_models():
@@ -126,20 +305,17 @@ def test_xml_adapter_format_and_parse_list_of_models():
items = [Item(name="a", score=1.1), Item(name="b", score=2.2)]
fields_with_values = {FieldInfoWithName(name="items", info=TestSignature.output_fields["items"]): items}
xml = adapter.format_field_with_value(fields_with_values)
- assert xml.strip().startswith("<items>")
- assert '"name": "a"' in xml
- assert '"score": 2.2' in xml
- assert xml.strip().endswith("</items>")
+ assert xml == (
+ "<items><item><name>a</name><score>1.1</score></item>"
+ "<item><name>b</name><score>2.2</score></item></items>"
+ )
+ assert adapter.parse(TestSignature, xml) == {"items": items}
- # Parse XML output
+ # Legacy JSON lists inside the outer XML field remain supported.
import json
completion = f"<items>{json.dumps([i.model_dump() for i in items])}</items>"
- parsed = adapter.parse(TestSignature, completion)
- assert isinstance(parsed["items"], list)
- assert all(isinstance(i, Item) for i in parsed["items"])
- assert parsed["items"][0].name == "a"
- assert parsed["items"][1].score == 2.2
+ assert adapter.parse(TestSignature, completion) == {"items": items}
def test_xml_adapter_with_tool_like_output():
@@ -167,8 +343,8 @@ def test_xml_adapter_with_tool_like_output():
}
xml = adapter.format_field_with_value(fields_with_values)
assert xml.strip().startswith("<tool_calls>")
- assert '"name": "get_weather"' in xml
- assert '"result": "125M"' in xml
+ assert "<name>get_weather</name>" in xml
+ assert "<result>125M</result>" in xml
assert xml.strip().endswith("</answer>")
import json
@@ -584,45 +760,40 @@ def test_xml_adapter_format_exact_messages_with_history_demo_pydantic_tools_and_
)
expected_messages = [{"role": "system",
- "content": 'Your input fields are:\n'
- '1. `history` (History): \n'
- '2. `image` (Image): \n'
- '3. `tools` (list[Tool]): \n'
- '4. `profile` (Profile): \n'
- '5. `question` (str):\n'
- 'Your output fields are:\n'
- '1. `answer` (AnswerCard):\n'
- 'All interactions will be structured in the following way, with the appropriate '
- 'values filled in.\n'
- '\n'
- '<history>\n'
- '{history}\n'
- '</history>\n'
- '\n'
- '<image>\n'
- '{image}\n'
- '</image>\n'
- '\n'
- '<tools>\n'
- '{tools}\n'
- '</tools>\n'
- '\n'
- '<profile>\n'
- '{profile}\n'
- '</profile>\n'
- '\n'
- '<question>\n'
- '{question}\n'
- '</question>\n'
- '\n'
- '<answer>\n'
- '{answer} # note: the value you produce must adhere to the JSON schema: '
- '{"type": "object", "properties": {"answer": {"type": "string", "title": "Answer"}, '
- '"sources": {"type": "array", "items": {"type": "string"}, "title": "Sources"}}, '
- '"required": ["answer", "sources"], "title": "AnswerCard"}\n'
- '</answer>\n'
- 'In adhering to this structure, your objective is: \n'
- ' Answer using all supplied context.'},
+ "content": "Your input fields are:\n"
+ "1. `history` (History): \n"
+ "2. `image` (Image): \n"
+ "3. `tools` (list[Tool]): \n"
+ "4. `profile` (Profile): \n"
+ "5. `question` (str):\n"
+ "Your output fields are:\n"
+ "1. `answer` (AnswerCard):\n"
+ "All interactions will be structured in the following way, with the appropriate "
+ "values filled in.\n"
+ "\n"
+ "<history>\n"
+ "{history}\n"
+ "</history>\n"
+ "\n"
+ "<image>\n"
+ "{image}\n"
+ "</image>\n"
+ "\n"
+ "<tools>\n"
+ "{tools}\n"
+ "</tools>\n"
+ "\n"
+ "<profile>\n"
+ "{profile}\n"
+ "</profile>\n"
+ "\n"
+ "<question>\n"
+ "{question}\n"
+ "</question>\n"
+ "\n"
+ "<answer><answer>...</answer><sources><item>...</item></sources></answer>\n"
+ "In adhering to this structure, your objective is: \n"
+ " Answer using all supplied context."},
{"role": "user",
"content": [{"type": "text",
"text": "This is an example of the task, though some input or output fields are not "
@@ -649,7 +820,7 @@ def test_xml_adapter_format_exact_messages_with_history_demo_pydantic_tools_and_
'What should we mention?\n'
'</question>'}]},
{"role": "assistant",
- "content": '<answer>\n{"answer": "Mention analytical engines.", "sources": ["demo"]}\n</answer>'},
+ "content": "<answer><answer>Mention analytical engines.</answer><sources><item>demo</item></sources></answer>"},
{"role": "user",
"content": '<profile>\n'
'{"name": "Ada", "location": {"city": "London", "country": "UK"}, "interests": '
@@ -660,7 +831,7 @@ def test_xml_adapter_format_exact_messages_with_history_demo_pydantic_tools_and_
'Who is Ada?\n'
'</question>'},
{"role": "assistant",
- "content": '<answer>\n{"answer": "Ada is a mathematician.", "sources": ["memory"]}\n</answer>'},
+ "content": "<answer><answer>Ada is a mathematician.</answer><sources><item>memory</item></sources></answer>"},
{"role": "user",
"content": [{"type": "text", "text": "<image>\n"},
{"type": "image_url", "image_url": {"url": "https://example.com/current.png"}},
@@ -684,7 +855,8 @@ def test_xml_adapter_format_exact_messages_with_history_demo_pydantic_tools_and_
'</question>\n'
'\n'
'Respond with the corresponding output fields wrapped in XML tags '
- '`<answer>`.'}]}]
+ '`<answer>`. Use this nested XML structure: '
+ '<answer><answer>...</answer><sources><item>...</item></sources></answer>'}]}]
assert messages == expected_messages
expected_lm_kwargs = {}
assert lm_kwargs == expected_lm_kwargs
@@ -705,33 +877,30 @@ def test_xml_adapter_format_exact_messages_with_nested_pydantic_output():
messages, lm_kwargs = format_messages_and_lm_kwargs(dspy.XMLAdapter(), PydanticSignature, [], {"question": "Summarize"})
expected_messages = [{"role": "system",
- "content": 'Your input fields are:\n'
- '1. `question` (str):\n'
- 'Your output fields are:\n'
- '1. `summary` (XmlSummary):\n'
- 'All interactions will be structured in the following way, with the appropriate '
- 'values filled in.\n'
- '\n'
- '<question>\n'
- '{question}\n'
- '</question>\n'
- '\n'
- '<summary>\n'
- '{summary} # note: the value you produce must adhere to the JSON schema: '
- '{"type": "object", "$defs": {"XmlAddress": {"type": "object", "properties": {"city": '
- '{"type": "string", "title": "City"}, "country": {"type": "string", "title": '
- '"Country"}}, "required": ["city", "country"], "title": "XmlAddress"}}, "properties": '
- '{"address": {"$ref": "#/$defs/XmlAddress"}, "title": {"type": "string", "title": '
- '"Title"}}, "required": ["title", "address"], "title": "XmlSummary"}\n'
- '</summary>\n'
- 'In adhering to this structure, your objective is: \n'
- ' Given the fields `question`, produce the fields `summary`.'},
+ "content": "Your input fields are:\n"
+ "1. `question` (str):\n"
+ "Your output fields are:\n"
+ "1. `summary` (XmlSummary):\n"
+ "All interactions will be structured in the following way, with the appropriate "
+ "values filled in.\n"
+ "\n"
+ "<question>\n"
+ "{question}\n"
+ "</question>\n"
+ "\n"
+ "<summary><title>...</title><address><city>...</city><country>...</country>"
+ "</address></summary>\n"
+ "In adhering to this structure, your objective is: \n"
+ " Given the fields `question`, produce the fields `summary`."},
{"role": "user",
"content": "<question>\n"
"Summarize\n"
"</question>\n"
"\n"
- "Respond with the corresponding output fields wrapped in XML tags `<summary>`."}]
+ "Respond with the corresponding output fields wrapped in XML tags `<summary>`. "
+ "Use this nested XML structure: "
+ "<summary><title>...</title><address><city>...</city><country>...</country>"
+ "</address></summary>"}]
assert messages == expected_messages
expected_lm_kwargs = {}
assert lm_kwargs == expected_lm_kwargs
@@ -831,13 +1000,9 @@ All interactions will be structured in the following way, with the appropriate v
{question}
</question>
-<answers>
-{answers} # note: the value you produce must adhere to the JSON schema: {"type": "array", "items": {"type": "string"}}
-</answers>
+<answers><item>...</item></answers>
-<scores>
-{scores} # note: the value you produce must adhere to the JSON schema: {"type": "array", "items": {"type": "number"}}
-</scores>
+<scores><item>...</item></scores>
In adhering to this structure, your objective is:\x20
Answer the question with multiple answers and scores"""
assert system_message == expected_system_message