Skip to content

Commit 006a406

Browse files
Jacksunweicopybara-github
authored andcommitted
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
1 parent f39df41 commit 006a406

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

src/google/adk/cli/conformance/cli_create.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,15 @@ async def _create_conformance_test_files(
9191
dump_pydantic_to_yaml(
9292
updated_session,
9393
generated_session_file,
94-
indent=2,
95-
sort_keys=False,
96-
exclude_none=True,
94+
sort_keys=False, # Output keys in the declaration order.
95+
exclude={
96+
"state": {"_adk_recordings_config": True},
97+
"events": {
98+
"__all__": {
99+
"actions": {"state_delta": {"_adk_recordings_config": True}}
100+
}
101+
},
102+
},
97103
)
98104

99105
return generated_session_file

src/google/adk/utils/yaml_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@
1515
from __future__ import annotations
1616

1717
from pathlib import Path
18+
from typing import Optional
19+
from typing import TYPE_CHECKING
1820
from typing import Union
1921

2022
from pydantic import BaseModel
2123
import yaml
2224

25+
if TYPE_CHECKING:
26+
from pydantic.main import IncEx
27+
2328

2429
def dump_pydantic_to_yaml(
2530
model: BaseModel,
@@ -29,6 +34,7 @@ def dump_pydantic_to_yaml(
2934
sort_keys: bool = True,
3035
exclude_none: bool = True,
3136
exclude_defaults: bool = True,
37+
exclude: Optional[IncEx] = None,
3238
) -> None:
3339
"""Dump a Pydantic model to a YAML file with multiline strings using | style.
3440
@@ -38,10 +44,14 @@ def dump_pydantic_to_yaml(
3844
indent: Number of spaces for indentation (default: 2).
3945
sort_keys: Whether to sort dictionary keys (default: True).
4046
exclude_none: Exclude fields with None values (default: True).
47+
exclude_defaults: Exclude fields with default values (default: True).
48+
exclude: Fields to exclude from the output. Can be a set of field names or
49+
a nested dict for fine-grained exclusion (default: None).
4150
"""
4251
model_dict = model.model_dump(
4352
exclude_none=exclude_none,
4453
exclude_defaults=exclude_defaults,
54+
exclude=exclude,
4555
mode='json',
4656
)
4757

@@ -74,6 +84,6 @@ def multiline_str_representer(dumper, data):
7484
Dumper=_MultilineDumper,
7585
indent=indent,
7686
sort_keys=sort_keys,
77-
default_flow_style=False,
7887
width=1000000, # Essentially disable text wraps
88+
allow_unicode=True, # Do not escape non-ascii characters.
7989
)

tests/unittests/utils/test_yaml_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,32 @@ def test_empty_list_formatting(tmp_path: Path):
123123
name: Test
124124
"""
125125
assert yaml_file.read_text(encoding="utf-8") == expected
126+
127+
128+
def test_non_ascii_character_preservation(tmp_path: Path):
129+
"""Test that non-ASCII characters are preserved in YAML output."""
130+
model = SimpleModel(
131+
name="你好世界", # Chinese
132+
age=30,
133+
active=True,
134+
multiline_text="🌍 Hello World 🌏\nこんにちは世界\nHola Mundo 🌎",
135+
items=["Château", "naïve", "café", "🎉"],
136+
)
137+
yaml_file = tmp_path / "test.yaml"
138+
139+
dump_pydantic_to_yaml(model, yaml_file)
140+
141+
assert yaml_file.read_text(encoding="utf-8") == """\
142+
active: true
143+
age: 30
144+
items:
145+
- Château
146+
- naïve
147+
- café
148+
- 🎉
149+
multiline_text: |-
150+
🌍 Hello World 🌏
151+
こんにちは世界
152+
Hola Mundo 🌎
153+
name: 你好世界
154+
"""

0 commit comments

Comments
 (0)