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.2.3.tar.gz (12.5 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.2.3-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for llmxml-0.2.3.tar.gz
Algorithm Hash digest
SHA256 0d080c0e5a8a2f9ec4e3a3e9154bbe1cbee129270429cde55bdab91e3078e116
MD5 96f4a6858aa9e07f5e0ef520e86be36f
BLAKE2b-256 e9c56c46ede435f1f575dcbe6bc44c64e3e4e94a8f726f411e2576d1a63cd373

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for llmxml-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d7a939a2dd2db40ce57eadcbed36bb917a3386f6c2cba0174f7a7523ee0a2dfa
MD5 78172d85b6c84d84d1aa366c9c30c1e2
BLAKE2b-256 f8b16fbe099a64cbd02843d8c8b02a63bf2d908aba5cd4d2934e4a30b20b915d

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