fix: handle multiple root-level XML elements from non-conformant LLMs#409
Open
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
Open
fix: handle multiple root-level XML elements from non-conformant LLMs#409octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
Conversation
…fixes NevaMind-AI#407) Some LLMs (e.g. llama3.2 via Ollama) return one <item> element per memory instead of wrapping all memories in a single root element. This produces "junk after document element" when the slice captured by _find_xml_boundaries contains several top-level tags separated by numbering text (e.g. "2. "). Two changes to _parse_memory_type_response_xml: 1. Wrap-and-retry: if ET.fromstring raises ParseError on the first attempt, wrap the content in a synthetic <_root_> element and retry. Valid single-root responses are unaffected. 2. findall to iter: use root.iter("memory") so that <memory> elements are located regardless of whether they are direct children of the root (normal case) or grandchildren via intermediate <item> tags (wrapped multi-root case).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #407
Problem
Some LLMs (e.g. llama3.2 via Ollama) do not follow the expected XML output format precisely. Instead of wrapping all extracted memories in a single
<item>root element, they produce one<item>element per memory with natural-language numbering in between:_find_xml_boundariesextracts the slice from the first<item>to the last</item>, which contains multiple top-level tags separated by text (2.,3., …).ET.fromstringthen raisesParseError: junk after document elementbecause the first root element closes before the end of the string.Solution
Two minimal changes to
_parse_memory_type_response_xml:Wrap-and-retry: catch
ParseErrorfrom the firstET.fromstringcall, wrap the content in a synthetic<_root_>element, and retry. This makes the multi-<item>layout valid XML without affecting well-formed single-root responses.findall→iter: switch fromroot.findall("memory")toroot.iter("memory")so that<memory>nodes are found regardless of depth—direct children of the root (normal case) or grandchildren via<item>wrappers (wrapped multi-root case).Testing
Manually validated against the XML snippets from the issue report. The retry path successfully parses multiple top-level
<item>elements and returns all contained<memory>items.