chore: Allow outputting non-acsii without escape and excludes fields in the dumped yaml files in the yaml_utils.py

Also excludes `_adk_recordings_config` for `adk conformance create` command.

PiperOrigin-RevId: 808865049
This commit is contained in:
Wei Sun (Jack)
2025-09-18 21:23:35 -07:00
committed by Copybara-Service
parent f39df4155e
commit 006a406f5b
3 changed files with 49 additions and 4 deletions
+9 -3
View File
@@ -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
+11 -1
View File
@@ -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.
)
+29
View File
@@ -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: 你好世界
"""