llmxml - XML parsing for LLM outputs
Sponsored by Speck
XML outputs are a common format for LLM outputs, but they are not always easy to parse. This package provides a way to parse XML outputs into Pydantic models.
This supports nested models, lists, and unions as well as partial parsing for streamed responses. llmxml also supports generating a prompt template for the XML schema to pass to the LLM.
Usage
Parsing XML
from llmxml import parse_xml, generate_prompt_template
from pydantic import BaseModel, Field
class Movie(BaseModel):
title: str = Field(..., description="The title of the movie")
director: str = Field(..., description="The director of the movie")
class Response(BaseModel):
movies: list[Movie] = Field(
..., description="A list of movies that match the query"
)
class ResponseObject(BaseModel):
response: Response = Field(
..., description="The response object that contains the movies"
)
xml: str = """
<response>
<movies>
<movie>
<title>The Matrix</title>
<director>The Wachowskis</director>
</movie>
</movies>
</response>
"""
result: ResponseObject = parse_xml(ResponseObject, xml)
print(result)
Output:
response=Response(movies=[Movie(title='The Matrix', director='The Wachowskis')])
Generating a prompt template
prompt: str = generate_prompt_template(
model=Response, include_instructions=True # Default is true
)
print(prompt)
Output:
<response_instructions>
You are to understand the content and provide the parsed objects in xml that match the following xml_schema:
Make sure to return an instance of the XML, not the schema itself
Each field in the field_schema has a description and a type.
Example:
<field_name>
[type]
[description]
</field_name>
Schema:
<movies>
[type: list]
[A list of movies]
</movies>
</response_instructions>
Development
Running tests
$ uv run -m pytest -n auto tests/test_parser.py
License
This project is licensed under the MIT License. See the LICENSE file for details.
Metadata
Release files for llmxml 0.4.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| llmxml-0.4.4.tar.gz | 18.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| llmxml-0.4.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 34.1 kB
Release files / llmxml-0.4.4.tar.gz
| Download URL | llmxml-0.4.4.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5e31c46224c227a0b294c83585179c3272d022ecc0cc4a18107b904fca0c575
|
|
BLAKE2b-256 checksum How to use checksums |
20366135d01f64082620a8dacc2d32a5524c6188d1295f79d7bfb0a578f657d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.0
|
Release files / llmxml-0.4.4-py3-none-any.whl
| Download URL | llmxml-0.4.4-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79ceb351dcae159ad15e8cb4157a41c2b46ef7db41c151145aa25c889ce653b1
|
|
BLAKE2b-256 checksum How to use checksums |
4d1157f895fb9ac57d489ef8d33a326ec6399bd4fab4d66fd3042f813450845f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.5.0
|