Skip to main content

XML parsing for LLM outputs

Project description

llmxml - XML parsing for LLM outputs

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.

Parser Demo

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>

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

llmxml-0.3.4.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmxml-0.3.4-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file llmxml-0.3.4.tar.gz.

File metadata

  • Download URL: llmxml-0.3.4.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.0

File hashes

Hashes for llmxml-0.3.4.tar.gz
Algorithm Hash digest
SHA256 a64194d44677d082c022de7893c0b02b6449823f5f0934fa0dc05938c9f4a970
MD5 2e674efe62f0a80c7ea7bda20903acd8
BLAKE2b-256 27778d122ecb35a0499e4d687ed73462835298c1cfaa8c8944923498023eb885

See more details on using hashes here.

File details

Details for the file llmxml-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: llmxml-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.0

File hashes

Hashes for llmxml-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 53c817371c7198e32d1bb58041a37399e1ee40f8b1f872c54aac4006eb1b4940
MD5 10f2f89caab2eaed59cef3d515d47e87
BLAKE2b-256 bef288f36cae8cab5a7ad77ec366e997e9724f50ee753d70f54af3c8d0d7886f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page