Skip to content

Conversation

@AstraBert
Copy link
Contributor

As per title, I implemented a to_xml filter that turns Pydantic models, dictionaries and deserializable strings into XML representations.

Here is an example usage (reported also in the docs):

from banks import Prompt
from pydantic import BaseModel
from typing import Dict

prompt_template = """
Please extract the contact details from this user:

{{ data | to_xml }}

Contact details:
"""

class User(BaseModel):
    username: str
    account_id: str
    registered_at: str
    email: str
    phone_number: str
    social_media_accounts: Dict[str, str]

user = User(username="example", account_id="0000", registered_at="10-25-2024", email="[email protected]", phone_number="0123456789", social_media_accounts={"BlueSky": "@example.com"})

p = Prompt(prompt_template)
print(p.text({"data": user}))

This will output:

Please extract the contact details from this user:

<user>
	<username>example</username>
	<account_id>0000</account_id>
	<registered_at>10-25-2024</registered_at>
	<email>[email protected]</email>
	<phone_number>0123456789</phone_number>
	<social_media_accounts>{'BlueSky': '@example.com'}</social_media_accounts>
</user>


Contact details:

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you fix the linter we're good to go!


def xml(value: Union[str, BaseModel, Dict[str, Any]]) -> str:
"""
Convert a Pydatic model, a deserializable string or a dictionary into an XML string.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Convert a Pydatic model, a deserializable string or a dictionary into an XML string.
Convert a Pydantic model, a deserializable string or a dictionary into an XML string.

@AstraBert
Copy link
Contributor Author

AstraBert commented Jul 18, 2025

Ok, I broke everything, let me revert it and fix it

@coveralls
Copy link

coveralls commented Jul 18, 2025

Coverage Status

coverage: 94.321% (-0.5%) from 94.792%
when pulling 48cc1bd on AstraBert:clelia/add-pydantic-input-and-xml-filter
into 17c1bc5 on masci:main.

@AstraBert
Copy link
Contributor Author

I think the test failure is not related to my change(?)

Copy link
Owner

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@masci masci merged commit e7a19fe into masci:main Jul 18, 2025
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants