From 598c14122238bc457ffb16fb74fac01eb4280689 Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Wed, 16 Jul 2025 20:49:52 -0700 Subject: [PATCH 1/2] change format_as_xml defaults --- pydantic_ai_slim/pydantic_ai/format_prompt.py | 9 ++-- tests/test_format_as_xml.py | 48 +++++++++---------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/format_prompt.py b/pydantic_ai_slim/pydantic_ai/format_prompt.py index 311f4277d..34f06a40a 100644 --- a/pydantic_ai_slim/pydantic_ai/format_prompt.py +++ b/pydantic_ai_slim/pydantic_ai/format_prompt.py @@ -13,9 +13,8 @@ def format_as_xml( obj: Any, - root_tag: str = 'examples', - item_tag: str = 'example', - include_root_tag: bool = True, + root_tag: str | None = None, + item_tag: str = 'item', none_str: str = 'null', indent: str | None = ' ', ) -> str: @@ -32,8 +31,6 @@ def format_as_xml( root_tag: Outer tag to wrap the XML in, use `None` to omit the outer tag. item_tag: Tag to use for each item in an iterable (e.g. list), this is overridden by the class name for dataclasses and Pydantic models. - include_root_tag: Whether to include the root tag in the output - (The root tag is always included if it includes a body - e.g. when the input is a simple value). none_str: String to use for `None` values. indent: Indentation string to use for pretty printing. @@ -55,7 +52,7 @@ def format_as_xml( ``` """ el = _ToXml(item_tag=item_tag, none_str=none_str).to_xml(obj, root_tag) - if not include_root_tag and el.text is None: + if root_tag is None and el.text is None: join = '' if indent is None else '\n' return join.join(_rootless_xml_elements(el, indent)) else: diff --git a/tests/test_format_as_xml.py b/tests/test_format_as_xml.py index 0781164aa..37053a67f 100644 --- a/tests/test_format_as_xml.py +++ b/tests/test_format_as_xml.py @@ -123,35 +123,35 @@ class ExamplePydanticModel(BaseModel): ), ], ) -def test(input_obj: Any, output: str): - assert format_as_xml(input_obj) == output +def test_root_tag(input_obj: Any, output: str): + assert format_as_xml(input_obj, root_tag='examples', item_tag='example') == output @pytest.mark.parametrize( 'input_obj,output', [ - pytest.param('a string', snapshot('a string'), id='string'), - pytest.param('a foo', snapshot('a <ex>foo</ex>'), id='string'), - pytest.param(42, snapshot('42'), id='int'), + pytest.param('a string', snapshot('a string'), id='string'), + pytest.param('a foo', snapshot('a <ex>foo</ex>'), id='string'), + pytest.param(42, snapshot('42'), id='int'), pytest.param( [1, 2, 3], snapshot("""\ -1 -2 -3\ +1 +2 +3\ """), id='list[int]', ), pytest.param( [[1, 2], [3]], snapshot("""\ - - 1 - 2 - - - 3 -\ + + 1 + 2 + + + 3 +\ """), id='list[list[int]]', ), @@ -166,24 +166,22 @@ def test(input_obj: Any, output: str): pytest.param( [datetime(2025, 1, 1, 12, 13), date(2025, 1, 2)], snapshot("""\ -2025-01-01T12:13:00 -2025-01-02\ +2025-01-01T12:13:00 +2025-01-02\ """), id='list[date]', ), ], ) def test_no_root(input_obj: Any, output: str): - assert format_as_xml(input_obj, include_root_tag=False) == output + assert format_as_xml(input_obj) == output def test_no_indent(): - assert format_as_xml([1, 2, 3], indent=None) == snapshot( - '123' - ) - assert format_as_xml([1, 2, 3], indent=None, include_root_tag=False) == snapshot( - '123' + assert format_as_xml([1, 2, 3], indent=None, root_tag='example') == snapshot( + '123' ) + assert format_as_xml([1, 2, 3], indent=None) == snapshot('123') def test_invalid_value(): @@ -197,8 +195,8 @@ def test_invalid_key(): def test_set(): - assert '1' in format_as_xml({1, 2, 3}) + assert '1' in format_as_xml({1, 2, 3}, item_tag='example') def test_custom_null(): - assert format_as_xml(None, none_str='nil') == snapshot('nil') + assert format_as_xml(None, none_str='nil') == snapshot('nil') From a649663c315db1317238d3a814cfe6ed769d83fa Mon Sep 17 00:00:00 2001 From: Samuel Colvin Date: Mon, 21 Jul 2025 09:22:43 -0700 Subject: [PATCH 2/2] fix example test --- tests/test_examples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_examples.py b/tests/test_examples.py index 0fbe64bc3..4b6bc27bc 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -502,7 +502,7 @@ async def model_logic( # noqa: C901 ) elif m.content.startswith('Write a list of 5 very rude things that I might say'): raise UnexpectedModelBehavior('Safety settings triggered', body='') - elif m.content.startswith('\n '): + elif m.content.startswith('\n John Doe'): return ModelResponse( parts=[ToolCallPart(tool_name='final_result_EmailOk', args={}, tool_call_id='pyd_ai_tool_call_id')] )