Skip to content

fix(terminology): keep exported XML well-formed for text with & and < - #1547

Open
borealisalastor-byte wants to merge 1 commit into
dataease:mainfrom
borealisalastor-byte:fix/xml-escape-preserve-structure
Open

borealisalastor-byte wants to merge 1 commit into
dataease:mainfrom
borealisalastor-byte:fix/xml-escape-preserve-structure

Conversation

@borealisalastor-byte

Copy link
Copy Markdown
Contributor

What breaks

to_xml_string in apps/terminology/curd/terminology.py and apps/data_training/curd/data_training.py builds the document with ElementTree, then restores &lt;, &gt; and &amp; to raw characters. Any terminology description or example SQL containing &, < or > therefore produces markup that no longer parses:

description = '投入 < 10% & 增长 > 5%'
-> <description>投入 < 10% & 增长 > 5%</description>
-> ExpatError: not well-formed (invalid token)

That string is inserted into the prompt template, so the model receives broken structure rather than escaped text — and text containing < can look like a tag boundary.

Change

  • Restore only &quot; and &apos;, which cannot break the document; keep the structural entities escaped. Both helpers get the same treatment.
  • backend/tests/test_xml_escaping.py loads the helpers through AST (like the other isolated tests, no ORM/web stack), then checks that descriptions and example SQL with &/</> parse and round-trip, that quotes are still readable, and that the plain-text shape is unchanged.

Verification

  • python -m pytest tests/test_xml_escaping.py -q: 7 passed.
  • The same input fails on 287935a6 with ExpatError: not well-formed (invalid token) (reproduced by loading the pre-fix function).
  • Terminology and data-training only; no prompt content is changed for text without these characters.

The terminology and data-training exporters build the document with
ElementTree, then restore '&lt;', '&gt;' and '&amp;' to raw characters. A
description such as '投入 < 10% & 增长 > 5%' therefore came out as markup that
no longer parses, and that string is inserted into a prompt template, so the
model received broken structure instead of escaped text.

Restore only the quote entities, which cannot break the document, and keep
the structural ones escaped.
@borealisalastor-byte
borealisalastor-byte force-pushed the fix/xml-escape-preserve-structure branch from 5222ce3 to 404e810 Compare December 6, 2026 09:12

This branch has not been deployed

No deployments
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