Skip to content

fix(dspy): escape nonstr XMLAdapter output fields - #97

Open
detail-app[bot] wants to merge 1 commit into
mainfrom
detail/bug-fix/fix-dspy-escape-nonstr-xmladapter-output-fields-2ac026
Open

fix(dspy): escape nonstr XMLAdapter output fields#97
detail-app[bot] wants to merge 1 commit into
mainfrom
detail/bug-fix/fix-dspy-escape-nonstr-xmladapter-output-fields-2ac026

Conversation

@detail-app

@detail-app detail-app Bot commented Sep 6, 2026

Copy link
Copy Markdown

Warning

GitHub issue creation failed

Detail attempted to publish this bug to GitHub, but the issue could not be created. This fix PR was created without that issue, and missing tracker references are shown as Unknown issue.

You can review and merge this PR normally. Please review your tracker integration settings before the next publish run.

Detail bug report: View on Detail

📝 Changes Description

This PR fixes malformed XML in XMLAdapter output for non-str output fields.

  • Bug: XMLAdapter.format_field_with_value gated XML escaping of </& on is_output and field.info.annotation is str (exact identity). Output fields whose annotation is not exactly str — notably str | None and dspy.Code (a pydantic.BaseModel, not a str subclass) — fell through unescaped, embedding raw </& into few-shot demo (assistant) messages. When the LM echoed the demonstrated pattern, XMLAdapter.parse raised AdapterParseError (end-to-end via Predict.forward -> adapter.__call__ -> XMLAdapter.parse). The bug was introduced in 33aaa19e, which added the escape but narrowed the gate to exact-str outputs.
  • Fix: Drop the ... is str identity gate so escaping applies to every plain-text output field. The nested-XML branch (_value_to_xml, which already escapes leaf text) and input-field rendering are unchanged. This is the narrower of the two options in the report; it covers str | None and dspy.Code without widening the blast radius to input rendering.

Closes Unknown issue

✅ Contributor Checklist

  • Pre-Commit checks are passing (locally and remotely)
  • Title of your PR / MR corresponds to the required format
  • Commit message follows required format {label}(dspy): {message}

Testing

  • Added two regression tests in tests/adapters/test_xml_adapter.py:
    • test_xml_adapter_escapes_non_str_output_fields — verifies str | None and dspy.Code outputs escape </&, produce well-formed XML (xml.etree.ElementTree.fromstring), and format->parse round-trip to the original literal value.
    • test_xml_adapter_does_not_escape_input_fields_with_special_chars — pins the scope boundary so an accidental over-broadening to input fields (which would break prompt snapshots) is caught.
  • Existing XMLAdapter suite (26 tests, including the str-output escape round-trip, None defaults, nested-XML, and all exact-message snapshots) remains green (28/28).
  • Full adapters suite, predict/streaming/teleprompt suites, and the CI-equivalent non-extra/non-deno suite (uv run pytest -m 'not extra and not deno' tests/) all pass with no regressions.
  • ruff check, pre-commit run, and uv build (wheel imports dspy 3.3.1) all pass.
  • Verified end-to-end with a real Ollama llama3.2:3b LM: the assistant demo for a dspy.Code signature whose prior demo contains < is now well-formed/escaped XML in the assembled prompt, and the default production path (use_json_adapter_fallback=True) returns the correct Code(code='return 3 < 5') with the literal < intact.

⚠️ Warnings

Authored by Detail: Automatic Fixes. Input-field escaping is intentionally left unchanged — XMLAdapter.parse only consumes output fields, so unescaped input XML is not parse-reachable; escaping inputs is a broader prompt-fidelity behavior change and is out of scope.


Automatic Fixes PRs can be configured here.

@greptile-apps

greptile-apps Bot commented Sep 6, 2026

Copy link
Copy Markdown

Greptile Summary

This PR broadens XML entity escaping from exact-str output fields to every plain-text output field while preserving the existing nested-XML and input-rendering behavior.

  • Prevents optional strings, dspy.Code, and other non-str output annotations from producing malformed XML when values contain < or &.
  • Adds regression coverage for optional-string and code outputs, XML validity, parse round-tripping, and the unchanged input-field boundary.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness, security, or repository-rule issues identified.

Output text is escaped exactly once and decoded before type conversion, while the tests exercise the reported malformed-XML path and preserve the intended input-field behavior.

Important Files Changed

Filename Overview
dspy/adapters/xml_adapter.py Broadens entity escaping for plain-text output values; parser decoding preserves the original typed value.
tests/adapters/test_xml_adapter.py Adds focused regression tests covering non-string output annotations, XML well-formedness, round-tripping, and unchanged input formatting.

Reviews (1): Last reviewed commit: "fix(dspy): escape nonstr XMLAdapter outp..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant