Skip to content

Commit b0b8e8b

Browse files
ampagentIsaac Miller
andcommitted
fix(XMLAdapter): strip str values that contain inline tags
The branch that keeps inline markup (e.g. <b>) in a str field returned the raw text with its surrounding newlines, while plain-text values are stripped. Strip it too, so the parsed value matches the streamed value. Amp-Thread-ID: https://ampcode.com/threads/T-01a12205-ee56-76b8-bf88-29e3d8708bff Co-authored-by: Isaac Miller <isaac@cmpnd.ai>
1 parent 1dcdf86 commit b0b8e8b

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎dspy/adapters/xml_adapter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def _elements_to_value(cls, elements: list[ET.Element], schema: dict, definition
201201
return [cls._elements_to_value([element], schema.get("items", {}), definitions) for element in elements]
202202
element = elements[0]
203203
if schema.get("type") == "string" and list(element):
204-
return (element.text or "") + "".join(ET.tostring(child, encoding="unicode") for child in element)
204+
return ((element.text or "") + "".join(ET.tostring(child, encoding="unicode") for child in element)).strip()
205205
children = cls._group_children(element)
206206
if not children:
207207
if schema.get("type") == "object" and not (element.text or "").strip():

‎tests/adapters/test_xml_adapter.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class TestSignature(dspy.Signature):
4444
assert parsed == {"answer": "Paris", "explanation": "The capital of France is Paris."}
4545

4646

47+
def test_xml_adapter_parse_strips_str_value_with_inline_tags():
48+
class TestSignature(dspy.Signature):
49+
question: str = dspy.InputField()
50+
answer: str = dspy.OutputField()
51+
52+
completion = "<answer>\nUse <b>bold</b> text.\n\nNext para.\n</answer>"
53+
parsed = XMLAdapter().parse(TestSignature, completion)
54+
assert parsed == {"answer": "Use <b>bold</b> text.\n\nNext para."}
55+
56+
4757
def test_xml_adapter_parse_raises_on_missing_field():
4858
class TestSignature(dspy.Signature):
4959
question: str = dspy.InputField()

0 commit comments

Comments
 (0)