A sloppy XML parser for Python designed to be used with LLMs
Project description
Sloppy XML is a single-file XML parser library that prioritizes resilience over strict XML compliance. In fact it tries not to be XML compliant at all. It's specifically designed to handle malformed XML commonly generated by LLMs, automated systems, and other sources where perfect XML structure cannot be guaranteed.
The parser provides both streaming and tree-building capabilities with robust error recovery mechanisms, making it ideal for parsing XML from unreliable sources while maintaining reasonable performance.
Note: this library was 100% AI generated with Claude Code and used experimentally for some evals I'm doing. I will try to fix it up as good as possible as I ran into issues, but I cannot vouch for the quality of it.
Goals
- Graceful Error Recovery: Handle malformed XML without crashing
- Dual API: Both streaming events and ElementTree construction
- Zero Dependencies: Single file with only standard library dependencies
- LLM-Friendly: Specifically designed for XML generated by language models
- Detailed Diagnostics: Rich error reporting with line/column information
Quick Example
import sloppy_xml
# Streaming API - handle malformed XML gracefully
xml_content = '''
<root>
<item name="test" broken-attr=>
Some text with <unclosed-tag>
<!-- Malformed comment --
</item>
</root>
'''
# Stream parsing with error recovery
for event in sloppy_xml.stream_parse(xml_content):
if isinstance(event, sloppy_xml.StartElement):
print(f"Start: {event.name}, attrs: {event.attrs}")
elif isinstance(event, sloppy_xml.EndElement):
print(f"End: {event.name}")
elif isinstance(event, sloppy_xml.Text):
print(f"Text: {repr(event.content)}")
elif isinstance(event, sloppy_xml.ParseError):
print(f"Error recovered: {event.message} at {event.line}:{event.column}")
# Tree parsing - get an ElementTree despite malformed input
root = sloppy_xml.tree_parse(xml_content)
print(f"Parsed tree with root: {root.tag}")
Event Types
The streaming parser emits these event types:
StartElement
- Opening tags with attributes and position infoEndElement
- Closing tags (including auto-closed mismatched tags)Text
- Text content with CDATA detectionComment
- XML commentsProcessingInstruction
- Processing instructions like<?xml?>
EntityRef
- Entity references with automatic resolutionParseError
- Recoverable parsing errors with diagnostic information
Error Recovery Features
- Tag Stack Management: Automatically closes mismatched opening tags
- Malformed Attribute Handling: Recovers from broken attribute syntax
- Entity Resolution: Handles standard HTML entities and numeric references
- CDATA Fallback: Treats malformed CDATA as regular text
- Comment Recovery: Handles unclosed comments gracefully
- State Recovery: Returns parser to valid state after errors
API Functions
Streaming API
sloppy_xml.stream_parse(xml_input)
Returns an iterator of events for streaming XML processing.
Tree API
sloppy_xml.tree_parse(xml_input)
Returns an xml.etree.ElementTree.Element
root node.
Installation
uv add sloppy-xml-py
Development
This project uses uv for dependency management:
# Setup
uv sync
# Run tests
uv run pytest
# Format code
uv run ruff format
# Check code quality
uv run ruff check
# Build package
uv build
Similar Projects
- xml.etree.ElementTree (stdlib) - Strict XML parsing, no error recovery
- lxml - Fast XML parsing with some error tolerance
- BeautifulSoup - HTML/XML parsing with tag soup handling
Sloppy XML fills the gap for applications that need structured XML parsing with aggressive error recovery, particularly for machine-generated content.
Sponsor
If you like the project and find it useful you can become a sponsor.
License and Links
- Issue Tracker
- License: Apache-2.0
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sloppy_xml-0.3.0.tar.gz
.
File metadata
- Download URL: sloppy_xml-0.3.0.tar.gz
- Upload date:
- Size: 29.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
dd479962ddd861a5881e1b48214b46c7a20dcb46c9fda98810e876b3023f737a
|
|
MD5 |
17e4009c630c8330c65fc60e654699ef
|
|
BLAKE2b-256 |
0cf6c30750c578aa6fe190057a602e8d9858d6e701008d30ff19c16daafa4c10
|
Provenance
The following attestation bundles were made for sloppy_xml-0.3.0.tar.gz
:
Publisher:
release.yml
on mitsuhiko/sloppy-xml-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
sloppy_xml-0.3.0.tar.gz
-
Subject digest:
dd479962ddd861a5881e1b48214b46c7a20dcb46c9fda98810e876b3023f737a
- Sigstore transparency entry: 245912794
- Sigstore integration time:
-
Permalink:
mitsuhiko/sloppy-xml-py@adec7d1ab2a72afc5036eb54be8aa51af1a9cabc
-
Branch / Tag:
refs/tags/0.3.0
- Owner: https://github.com/mitsuhiko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@adec7d1ab2a72afc5036eb54be8aa51af1a9cabc
-
Trigger Event:
push
-
Statement type:
File details
Details for the file sloppy_xml-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: sloppy_xml-0.3.0-py3-none-any.whl
- Upload date:
- Size: 20.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
b27398cf2bc1e9e262ab77aaf5c93ffa8ce3e4065604fe033900f30e5b9168c1
|
|
MD5 |
9cdca712cba8e0186ea4f05bf245bf47
|
|
BLAKE2b-256 |
8895f125e3e7b0c7c0cfabdd41970e46c1a41ad7ead9bf560cc02c9b090f0759
|
Provenance
The following attestation bundles were made for sloppy_xml-0.3.0-py3-none-any.whl
:
Publisher:
release.yml
on mitsuhiko/sloppy-xml-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
sloppy_xml-0.3.0-py3-none-any.whl
-
Subject digest:
b27398cf2bc1e9e262ab77aaf5c93ffa8ce3e4065604fe033900f30e5b9168c1
- Sigstore transparency entry: 245912797
- Sigstore integration time:
-
Permalink:
mitsuhiko/sloppy-xml-py@adec7d1ab2a72afc5036eb54be8aa51af1a9cabc
-
Branch / Tag:
refs/tags/0.3.0
- Owner: https://github.com/mitsuhiko
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@adec7d1ab2a72afc5036eb54be8aa51af1a9cabc
-
Trigger Event:
push
-
Statement type: