Skip to content

Add CLI for running and validating checks#2377

Open
raghavtries wants to merge 5 commits intoGiskard-AI:mainfrom
raghavtries:cli-tool-for-running-checks
Open

Add CLI for running and validating checks#2377
raghavtries wants to merge 5 commits intoGiskard-AI:mainfrom
raghavtries:cli-tool-for-running-checks

Conversation

@raghavtries
Copy link
Copy Markdown
Contributor

Description

Add a giskard CLI for running and validating serialized scenarios and suites.

This PR adds:

  • giskard run <file> for YAML/JSON scenario and suite definitions
  • giskard validate <file> to validate definitions without execution
  • giskard list checks to expose built-in check kinds
  • output formats for rich terminal output, JSON, and JUnit XML

It also preserves support for serialized trace_type values and ensures python:... target resolution prefers the definition file directory over the current working directory when module names collide.

Related Issue

Closes #2376

Type of Change

  • 📚 Examples / docs / tutorials / dependencies update
  • 🔧 Bug fix (non-breaking change which fixes an issue)
  • 🥂 Improvement (non-breaking change which improves an existing feature)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🔐 Security fix

Checklist

  • I've read the CODE_OF_CONDUCT.md document.
  • I've read the CONTRIBUTING.md guide.
  • I've written tests for all new methods and classes that I created.
  • I've written the docstring in NumPy format for all the methods and classes that I created or modified.
  • I've updated the uv.lock running uv lock (only applicable when pyproject.toml has been modified)
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a command-line interface for Giskard, enabling users to run, validate, and list checks using YAML or JSON definition files. The implementation includes a new giskard entry point, logic for dynamic Python target resolution, and support for multiple output formats like JSON and JUnit. Review feedback suggests simplifying the attribute resolution logic, ensuring parent directories are created before writing output files, and avoiding reliance on private internal registries for check discovery.

Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
if path is None:
print(output)
return
path.write_text(output, encoding="utf-8")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The path.write_text call will fail if the parent directory of the output file does not exist. For a better user experience, consider ensuring the parent directory exists before writing.

Suggested change
path.write_text(output, encoding="utf-8")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(output, encoding="utf-8")
Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
@kevinmessiaen kevinmessiaen self-requested a review April 10, 2026 01:39
Copy link
Copy Markdown
Member

@kevinmessiaen kevinmessiaen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raghavtries Thanks for your submission. This look good but I feel like we could simplify a bit by leveraging Pydantic capabilities

Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
Comment thread libs/giskard-checks/tests/cli/test_cli.py Outdated
Comment thread libs/giskard-checks/pyproject.toml Outdated
Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
Comment thread libs/giskard-checks/src/giskard/checks/cli.py Outdated
@raghavtries
Copy link
Copy Markdown
Contributor Author

Thanks for the details in the review, I addressed the feedback by moving scenario target, suite target, trace_type loading into Pydantic validators, along with simplifying CLI loading through TypeAdapter, keeping the serialized format canonical with checks/interacts. Also, adding public registered-kind/type accessors on Discriminated.

Also made YAML support optional and moved reusable CLI definitions into fixture files.

I also fixed output directory creation for --output.

@kevinmessiaen
Copy link
Copy Markdown
Member

@gemini-code-assist Can you review the updated code?

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a command-line interface for Giskard checks, enabling the execution, validation, and listing of scenarios and suites from JSON or YAML definitions. Key additions include a robust Python reference resolution system, Pydantic-based validation for dynamic targets, and support for various output formats like JUnit and JSON. Review feedback highlighted the need to prevent over-aggressive clearing of the module cache during reference resolution and to ensure parent directories are created when writing JUnit reports.

Comment on lines +88 to +94
if _module_exists_in_search_path(module_name, preferred_search_path):
if not _module_belongs_to_search_path(
cached_module, preferred_search_path
):
sys.modules.pop(module_name, None)
elif not _module_belongs_to_search_paths(cached_module, search_paths):
sys.modules.pop(module_name, None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The logic for clearing sys.modules is too aggressive. It currently removes any cached module that is not found in the provided search paths (e.g., standard library modules like os or json if they are referenced in a python:... string). This forces unnecessary re-imports and can lead to unexpected side effects in long-running processes. The cache should only be cleared if a collision is detected with a module in the preferred search path (the definition file's directory).

Suggested change
if _module_exists_in_search_path(module_name, preferred_search_path):
if not _module_belongs_to_search_path(
cached_module, preferred_search_path
):
sys.modules.pop(module_name, None)
elif not _module_belongs_to_search_paths(cached_module, search_paths):
sys.modules.pop(module_name, None)
if _module_exists_in_search_path(module_name, preferred_search_path):
if not _module_belongs_to_search_path(
cached_module, preferred_search_path
):
sys.modules.pop(module_name, None)
Comment on lines +147 to +150
elif args.format == "junit":
xml = _build_suite_result(execution).to_junit_xml(path=output_path)
if output_path is None:
print(xml)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When using the junit format with an --output path, the parent directories are not automatically created. This can cause the command to fail if the specified output path includes directories that do not yet exist. For consistency with the json format (which uses _write_text_output), ensure the parent directory is created before calling to_junit_xml.

Suggested change
elif args.format == "junit":
xml = _build_suite_result(execution).to_junit_xml(path=output_path)
if output_path is None:
print(xml)
elif args.format == "junit":
if output_path:
output_path.parent.mkdir(parents=True, exist_ok=True)
xml = _build_suite_result(execution).to_junit_xml(path=output_path)
if output_path is None:
print(xml)
Signed-off-by: Raghav Sub <theraghavs06@gmail.com>
@raghavtries raghavtries force-pushed the cli-tool-for-running-checks branch from dc03c14 to 657d953 Compare April 15, 2026 17:04
@raghavtries
Copy link
Copy Markdown
Contributor Author

raghavtries commented Apr 15, 2026

Hi there! I've made the needed edits from the Gemini review, please let me know if there's anything else I should change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment