diff --git a/src/google/adk/cli/conformance/cli_create.py b/src/google/adk/cli/conformance/cli_create.py index 865b223d..7ce3857f 100644 --- a/src/google/adk/cli/conformance/cli_create.py +++ b/src/google/adk/cli/conformance/cli_create.py @@ -91,9 +91,15 @@ async def _create_conformance_test_files( dump_pydantic_to_yaml( updated_session, generated_session_file, - indent=2, - sort_keys=False, - exclude_none=True, + sort_keys=False, # Output keys in the declaration order. + exclude={ + "state": {"_adk_recordings_config": True}, + "events": { + "__all__": { + "actions": {"state_delta": {"_adk_recordings_config": True}} + } + }, + }, ) return generated_session_file diff --git a/src/google/adk/utils/yaml_utils.py b/src/google/adk/utils/yaml_utils.py index b4755208..556e705f 100644 --- a/src/google/adk/utils/yaml_utils.py +++ b/src/google/adk/utils/yaml_utils.py @@ -15,11 +15,16 @@ from __future__ import annotations from pathlib import Path +from typing import Optional +from typing import TYPE_CHECKING from typing import Union from pydantic import BaseModel import yaml +if TYPE_CHECKING: + from pydantic.main import IncEx + def dump_pydantic_to_yaml( model: BaseModel, @@ -29,6 +34,7 @@ def dump_pydantic_to_yaml( sort_keys: bool = True, exclude_none: bool = True, exclude_defaults: bool = True, + exclude: Optional[IncEx] = None, ) -> None: """Dump a Pydantic model to a YAML file with multiline strings using | style. @@ -38,10 +44,14 @@ def dump_pydantic_to_yaml( indent: Number of spaces for indentation (default: 2). sort_keys: Whether to sort dictionary keys (default: True). exclude_none: Exclude fields with None values (default: True). + exclude_defaults: Exclude fields with default values (default: True). + exclude: Fields to exclude from the output. Can be a set of field names or + a nested dict for fine-grained exclusion (default: None). """ model_dict = model.model_dump( exclude_none=exclude_none, exclude_defaults=exclude_defaults, + exclude=exclude, mode='json', ) @@ -74,6 +84,6 @@ def dump_pydantic_to_yaml( Dumper=_MultilineDumper, indent=indent, sort_keys=sort_keys, - default_flow_style=False, width=1000000, # Essentially disable text wraps + allow_unicode=True, # Do not escape non-ascii characters. ) diff --git a/tests/unittests/utils/test_yaml_utils.py b/tests/unittests/utils/test_yaml_utils.py index 93a4be7a..6d4c105b 100644 --- a/tests/unittests/utils/test_yaml_utils.py +++ b/tests/unittests/utils/test_yaml_utils.py @@ -123,3 +123,32 @@ items: [] name: Test """ assert yaml_file.read_text(encoding="utf-8") == expected + + +def test_non_ascii_character_preservation(tmp_path: Path): + """Test that non-ASCII characters are preserved in YAML output.""" + model = SimpleModel( + name="你好世界", # Chinese + age=30, + active=True, + multiline_text="🌍 Hello World 🌏\nこんにちは世界\nHola Mundo 🌎", + items=["Château", "naïve", "café", "🎉"], + ) + yaml_file = tmp_path / "test.yaml" + + dump_pydantic_to_yaml(model, yaml_file) + + assert yaml_file.read_text(encoding="utf-8") == """\ +active: true +age: 30 +items: + - Château + - naïve + - café + - 🎉 +multiline_text: |- + 🌍 Hello World 🌏 + こんにちは世界 + Hola Mundo 🌎 +name: 你好世界 +"""