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.
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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file llmxml-0.2.2.tar.gz.
File metadata
- Download URL: llmxml-0.2.2.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ce9d919e400186ab7a3f3cb9c01fef318a2d8e55142b87c4eb80b24da29125a
|
|
| MD5 |
882a85563c7ff28f7de9eebb048de4bc
|
|
| BLAKE2b-256 |
9d9830a981829f3cb4857b4325d72341a804b01c65ea5bc560a0be0cec12650d
|
File details
Details for the file llmxml-0.2.2-py3-none-any.whl.
File metadata
- Download URL: llmxml-0.2.2-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
968640fcf7d27b72cae3773cee2f95acec08c7c11a03c76bb0fb068fd89d97a8
|
|
| MD5 |
9b1ff882ba05001ce94fc33a818c5a09
|
|
| BLAKE2b-256 |
edcd3ebc431680b78baf2666e2dca6d09338971937398c19f6985a251340455a
|