Skip to content

feat(adapter): support nested XML data - #10237

Closed
isaacbmiller wants to merge 6 commits into
mainfrom
feat/xml-adapter-nested-data
Closed

isaacbmiller wants to merge 6 commits into
mainfrom
feat/xml-adapter-nested-data

Conversation

@isaacbmiller

@isaacbmiller isaacbmiller commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #8481.
Closes #10102.
Supersedes #8482 with an implementation based on current main.

Summary

  • format nested Pydantic outputs, TypedDicts, and lists as nested XML
  • show the nested XML schema consistently in system instructions and the final user reminder, including TypedDict child fields
  • parse nested and repeated XML elements into signature field types\n- bound recursive Pydantic schema expansion so self-referential models format safely
  • retain support for legacy JSON values inside outer XML field tags
  • XML-escape string output values so embedded closing tags round-trip safely
  • reject malformed XML with AdapterParseError instead of silently truncating values
  • preserve existing DSPy transport and multimodal input formatting

What the final prompt looks like

Given a Person output containing a nested Address and list[str] interests, the LM receives this schema in both the system instructions and final user request:

<person><name>...</name><age>...</age><address><city>...</city><country>...</country></address><interests>...</interests> <interests>...</interests></person>

The final user message looks like:

<text>
Ada, 36, lives in London, UK and likes math and machines.
</text>

Respond with the corresponding output fields wrapped in XML tags `<person>`. Use this nested XML structure: <person><name>...</name><age>...</age><address><city>...</city><country>...</country></address><interests>...</interests> <interests>...</interests></person>

A matching response is parsed directly into the declared Pydantic types:

<person>
  <name>Ada</name>
  <age>36</age>
  <address><city>London</city><country>UK</country></address>
  <interests>math</interests>
  <interests>machines</interests>
</person>

The XMLAdapter documentation includes the runnable example, complete system and user prompt, matching response, and parsed result types.

Testing

  • pytest -q tests/adapters/test_xml_adapter.py (24 passed)
  • pytest -q tests/adapters (278 passed)
  • ruff check dspy/adapters/xml_adapter.py
  • ruff format --check dspy/adapters/xml_adapter.py
  • git diff --check

@greptile-apps

greptile-apps Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This change adds XML rendering and parsing for nested Pydantic models, TypedDicts, dictionaries, and repeated lists. It also bounds recursive model schema rendering, preserves JSON-in-XML compatibility, and documents the nested XML response format.

Confidence Score: 5/5

No blocking failure remains.

No accepted blocking findings remain after exercising nested TypedDict, recursive Pydantic, XML escaping, and parse round-trip behavior.

T-Rex T-Rex Logs

What T-Rex did

  • Ran the standalone XML adapter nested-path runtime validation script.
  • The script exercised module-scope TypedDict fields, self-recursive and mutually recursive Pydantic models, and XML-sensitive nested string values.
  • The exact standalone validation source and its captured execution output were verified, and all three scenario assertions passed with no security-impacting XML escaping failure observed.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (5): Last reviewed commit: "refactor(adapter): unify XML schema hand..." | Re-trigger Greptile

Comment thread dspy/adapters/xml_adapter.py Outdated
Comment thread dspy/adapters/xml_adapter.py Outdated
Comment thread dspy/adapters/xml_adapter.py Outdated
@isaacbmiller

Copy link
Copy Markdown
Collaborator Author

superseded by #10239.

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

Labels

None yet

Projects

None yet

2 participants