Skip to content

Commit 6cba5e2

Browse files
authored
agent-sdk: raise error on example validation failure (google#923)
When the schema manager loads the schema with examples, it should validate the A2UI example. For validation failures, it should fail early so that users can fix them.
1 parent 4f47d2e commit 6cba5e2

2 files changed

Lines changed: 53 additions & 14 deletions

File tree

agent_sdks/python/src/a2ui/core/schema/catalog.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,23 @@ def load_examples(self, path: Optional[str], validate: bool = False) -> str:
162162
if filename.endswith(".json"):
163163
full_path = os.path.join(path, filename)
164164
basename = os.path.splitext(filename)[0]
165-
try:
166-
with open(full_path, "r", encoding="utf-8") as f:
167-
content = f.read()
168-
if validate and not self._validate_example(full_path, basename, content):
169-
continue
170-
merged_examples.append(
171-
f"---BEGIN {basename}---\n{content}\n---END {basename}---"
172-
)
173-
except Exception as e:
174-
logging.warning(f"Failed to load example {full_path}: {e}")
165+
with open(full_path, "r", encoding="utf-8") as f:
166+
content = f.read()
167+
168+
if validate:
169+
self._validate_example(full_path, basename, content)
170+
171+
merged_examples.append(
172+
f"---BEGIN {basename}---\n{content}\n---END {basename}---"
173+
)
174+
175175
if not merged_examples:
176176
return ""
177177
return "\n\n".join(merged_examples)
178178

179-
def _validate_example(self, full_path: str, basename: str, content: str) -> bool:
179+
def _validate_example(self, full_path: str, basename: str, content: str) -> None:
180180
try:
181181
json_data = json.loads(content)
182182
self.validator.validate(json_data)
183183
except Exception as e:
184-
logging.warning(f"Failed to validate example {full_path}: {e}")
185-
return False
186-
return True
184+
raise ValueError(f"Failed to validate example {full_path}: {e}") from e

agent_sdks/python/tests/core/schema/test_catalog.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@ def test_load_examples(tmp_path):
7474
assert "ignored" not in examples_str
7575

7676

77+
def test_load_examples_validation_fails_on_bad_json(tmp_path):
78+
example_dir = tmp_path / "examples"
79+
example_dir.mkdir()
80+
(example_dir / "bad.json").write_text("{ this is bad json }")
81+
82+
catalog = A2uiCatalog(
83+
version=VERSION_0_8,
84+
name=BASIC_CATALOG_NAME,
85+
s2c_schema={},
86+
common_types_schema={},
87+
catalog_schema={"catalogId": "basic"},
88+
)
89+
90+
with pytest.raises(ValueError, match="Failed to validate example.*bad.json"):
91+
catalog.load_examples(str(example_dir), validate=True)
92+
93+
94+
def test_load_examples_validation_fails_on_schema_error(tmp_path):
95+
example_dir = tmp_path / "examples"
96+
example_dir.mkdir()
97+
(example_dir / "invalid.json").write_text('{"myKey": "stringValue"}')
98+
99+
# A schema that expects myKey to be an integer
100+
schema = {
101+
"type": "object",
102+
"properties": {"myKey": {"type": "integer"}},
103+
"required": ["myKey"],
104+
}
105+
106+
catalog = A2uiCatalog(
107+
version=VERSION_0_8,
108+
name=BASIC_CATALOG_NAME,
109+
s2c_schema=schema,
110+
common_types_schema={},
111+
catalog_schema={"catalogId": "basic"},
112+
)
113+
114+
with pytest.raises(ValueError, match="Failed to validate example.*invalid.json"):
115+
catalog.load_examples(str(example_dir), validate=True)
116+
117+
77118
def test_load_examples_none_or_invalid_path():
78119
catalog = A2uiCatalog(
79120
version=VERSION_0_8,

0 commit comments

Comments
 (0)