Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions agent_sdks/python/src/a2ui/core/schema/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,23 @@ def load_examples(self, path: Optional[str], validate: bool = False) -> str:
if filename.endswith(".json"):
full_path = os.path.join(path, filename)
basename = os.path.splitext(filename)[0]
try:
with open(full_path, "r", encoding="utf-8") as f:
content = f.read()
if validate and not self._validate_example(full_path, basename, content):
continue
merged_examples.append(
f"---BEGIN {basename}---\n{content}\n---END {basename}---"
)
except Exception as e:
logging.warning(f"Failed to load example {full_path}: {e}")
with open(full_path, "r", encoding="utf-8") as f:
content = f.read()

if validate:
self._validate_example(full_path, basename, content)

merged_examples.append(
f"---BEGIN {basename}---\n{content}\n---END {basename}---"
)

if not merged_examples:
return ""
return "\n\n".join(merged_examples)

def _validate_example(self, full_path: str, basename: str, content: str) -> bool:
def _validate_example(self, full_path: str, basename: str, content: str) -> None:
try:
json_data = json.loads(content)
self.validator.validate(json_data)
except Exception as e:
logging.warning(f"Failed to validate example {full_path}: {e}")
return False
return True
raise ValueError(f"Failed to validate example {full_path}: {e}") from e
41 changes: 41 additions & 0 deletions agent_sdks/python/tests/core/schema/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@ def test_load_examples(tmp_path):
assert "ignored" not in examples_str


def test_load_examples_validation_fails_on_bad_json(tmp_path):
example_dir = tmp_path / "examples"
example_dir.mkdir()
(example_dir / "bad.json").write_text("{ this is bad json }")

catalog = A2uiCatalog(
version=VERSION_0_8,
name=BASIC_CATALOG_NAME,
s2c_schema={},
common_types_schema={},
catalog_schema={"catalogId": "basic"},
)

with pytest.raises(ValueError, match="Failed to validate example.*bad.json"):
catalog.load_examples(str(example_dir), validate=True)


def test_load_examples_validation_fails_on_schema_error(tmp_path):
example_dir = tmp_path / "examples"
example_dir.mkdir()
(example_dir / "invalid.json").write_text('{"myKey": "stringValue"}')

# A schema that expects myKey to be an integer
schema = {
"type": "object",
"properties": {"myKey": {"type": "integer"}},
"required": ["myKey"],
}

catalog = A2uiCatalog(
version=VERSION_0_8,
name=BASIC_CATALOG_NAME,
s2c_schema=schema,
common_types_schema={},
catalog_schema={"catalogId": "basic"},
)

with pytest.raises(ValueError, match="Failed to validate example.*invalid.json"):
catalog.load_examples(str(example_dir), validate=True)


def test_load_examples_none_or_invalid_path():
catalog = A2uiCatalog(
version=VERSION_0_8,
Expand Down
Loading