Skip to content

Commit 091c499

Browse files
authored
change format_as_xml defaults (#2228)
1 parent 09bd7dd commit 091c499

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

pydantic_ai_slim/pydantic_ai/format_prompt.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
def format_as_xml(
1515
obj: Any,
16-
root_tag: str = 'examples',
17-
item_tag: str = 'example',
18-
include_root_tag: bool = True,
16+
root_tag: str | None = None,
17+
item_tag: str = 'item',
1918
none_str: str = 'null',
2019
indent: str | None = ' ',
2120
) -> str:
@@ -32,8 +31,6 @@ def format_as_xml(
3231
root_tag: Outer tag to wrap the XML in, use `None` to omit the outer tag.
3332
item_tag: Tag to use for each item in an iterable (e.g. list), this is overridden by the class name
3433
for dataclasses and Pydantic models.
35-
include_root_tag: Whether to include the root tag in the output
36-
(The root tag is always included if it includes a body - e.g. when the input is a simple value).
3734
none_str: String to use for `None` values.
3835
indent: Indentation string to use for pretty printing.
3936
@@ -55,7 +52,7 @@ def format_as_xml(
5552
```
5653
"""
5754
el = _ToXml(item_tag=item_tag, none_str=none_str).to_xml(obj, root_tag)
58-
if not include_root_tag and el.text is None:
55+
if root_tag is None and el.text is None:
5956
join = '' if indent is None else '\n'
6057
return join.join(_rootless_xml_elements(el, indent))
6158
else:

tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ async def model_logic( # noqa: C901
502502
)
503503
elif m.content.startswith('Write a list of 5 very rude things that I might say'):
504504
raise UnexpectedModelBehavior('Safety settings triggered', body='<safety settings details>')
505-
elif m.content.startswith('<examples>\n <user>'):
505+
elif m.content.startswith('<user>\n <name>John Doe</name>'):
506506
return ModelResponse(
507507
parts=[ToolCallPart(tool_name='final_result_EmailOk', args={}, tool_call_id='pyd_ai_tool_call_id')]
508508
)

tests/test_format_as_xml.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,35 +123,35 @@ class ExamplePydanticModel(BaseModel):
123123
),
124124
],
125125
)
126-
def test(input_obj: Any, output: str):
127-
assert format_as_xml(input_obj) == output
126+
def test_root_tag(input_obj: Any, output: str):
127+
assert format_as_xml(input_obj, root_tag='examples', item_tag='example') == output
128128

129129

130130
@pytest.mark.parametrize(
131131
'input_obj,output',
132132
[
133-
pytest.param('a string', snapshot('<examples>a string</examples>'), id='string'),
134-
pytest.param('a <ex>foo</ex>', snapshot('<examples>a &lt;ex&gt;foo&lt;/ex&gt;</examples>'), id='string'),
135-
pytest.param(42, snapshot('<examples>42</examples>'), id='int'),
133+
pytest.param('a string', snapshot('<item>a string</item>'), id='string'),
134+
pytest.param('a <ex>foo</ex>', snapshot('<item>a &lt;ex&gt;foo&lt;/ex&gt;</item>'), id='string'),
135+
pytest.param(42, snapshot('<item>42</item>'), id='int'),
136136
pytest.param(
137137
[1, 2, 3],
138138
snapshot("""\
139-
<example>1</example>
140-
<example>2</example>
141-
<example>3</example>\
139+
<item>1</item>
140+
<item>2</item>
141+
<item>3</item>\
142142
"""),
143143
id='list[int]',
144144
),
145145
pytest.param(
146146
[[1, 2], [3]],
147147
snapshot("""\
148-
<example>
149-
<example>1</example>
150-
<example>2</example>
151-
</example>
152-
<example>
153-
<example>3</example>
154-
</example>\
148+
<item>
149+
<item>1</item>
150+
<item>2</item>
151+
</item>
152+
<item>
153+
<item>3</item>
154+
</item>\
155155
"""),
156156
id='list[list[int]]',
157157
),
@@ -166,24 +166,22 @@ def test(input_obj: Any, output: str):
166166
pytest.param(
167167
[datetime(2025, 1, 1, 12, 13), date(2025, 1, 2)],
168168
snapshot("""\
169-
<example>2025-01-01T12:13:00</example>
170-
<example>2025-01-02</example>\
169+
<item>2025-01-01T12:13:00</item>
170+
<item>2025-01-02</item>\
171171
"""),
172172
id='list[date]',
173173
),
174174
],
175175
)
176176
def test_no_root(input_obj: Any, output: str):
177-
assert format_as_xml(input_obj, include_root_tag=False) == output
177+
assert format_as_xml(input_obj) == output
178178

179179

180180
def test_no_indent():
181-
assert format_as_xml([1, 2, 3], indent=None) == snapshot(
182-
'<examples><example>1</example><example>2</example><example>3</example></examples>'
183-
)
184-
assert format_as_xml([1, 2, 3], indent=None, include_root_tag=False) == snapshot(
185-
'<example>1</example><example>2</example><example>3</example>'
181+
assert format_as_xml([1, 2, 3], indent=None, root_tag='example') == snapshot(
182+
'<example><item>1</item><item>2</item><item>3</item></example>'
186183
)
184+
assert format_as_xml([1, 2, 3], indent=None) == snapshot('<item>1</item><item>2</item><item>3</item>')
187185

188186

189187
def test_invalid_value():
@@ -197,8 +195,8 @@ def test_invalid_key():
197195

198196

199197
def test_set():
200-
assert '<example>1</example>' in format_as_xml({1, 2, 3})
198+
assert '<example>1</example>' in format_as_xml({1, 2, 3}, item_tag='example')
201199

202200

203201
def test_custom_null():
204-
assert format_as_xml(None, none_str='nil') == snapshot('<examples>nil</examples>')
202+
assert format_as_xml(None, none_str='nil') == snapshot('<item>nil</item>')

0 commit comments

Comments
 (0)