agents-cli icon indicating copy to clipboard operation
agents-cli copied to clipboard

eval optimize: crashes on inline dataset dicts (TypeError) and breaks on relative paths from subdirectories

Open halcyon-past opened this issue 3 weeks ago • 1 comments

What happened?

agents-cli eval optimize has two bugs in src/google/agents/cli/eval/cmd_optimize.py (_load_configs_and_datasets and cmd_optimize):

  1. Inline dataset dicts in --config crash with TypeError: The --config help text explicitly documents that train_dataset and validation_dataset may be either a file path or an inline dictionary:

    - train_dataset (Path to EvaluationDataset JSON file or dict in EvaluationDataset format)
    - validation_dataset (Path to EvaluationDataset JSON file or dict in EvaluationDataset format)
    

    However, _load_configs_and_datasets() unconditionally passes both values to open(...) (cmd_optimize.py lines 72–78):

    if train_dataset_path is not None:
        with open(train_dataset_path, encoding="utf-8") as f_in:
            train_dataset = json.load(f_in)
    

    When an inline dict is provided in the JSON config, open(dict) raises TypeError: expected str, bytes or os.PathLike object, not dict.

  2. Relative --config / --dataset paths break when invoked from a subdirectory: Neither --dataset nor --config uses click.Path(exists=True, dir_okay=False, resolve_path=True). Because cmd_optimize() calls chdir_project_root() before calling _load_configs_and_datasets(), relative paths passed from a subdirectory (e.g., tests/eval/) are resolved against the project root instead of the caller's working directory and fail with an unhandled FileNotFoundError.

Steps to Reproduce

1. Inline dict in config (TypeError):

agents-cli create my-agent -y
cd my-agent
cat <<'EOF' > tests/eval/inline_config.json
{
  "eval_config": {"criteria": {"final_response_quality": 0.8}},
  "train_dataset": {
    "eval_cases": [
      {"eval_case_id": "case_1", "prompt": "Hello", "reference": "Hi there!"}
    ]
  }
}
EOF
agents-cli eval optimize --config tests/eval/inline_config.json

2. Relative --config / --dataset from a subdirectory (FileNotFoundError):

cd tests/eval
echo '{"eval_config": {"criteria": {"final_response_quality": 0.8}}}' > file_config.json
echo '{"eval_cases": [{"eval_case_id": "case_1", "prompt": "Hello", "reference": "Hi there!"}]}' > my_dataset.json
agents-cli eval optimize --config file_config.json --dataset my_dataset.json

What did you expect to happen?

  1. Inline train_dataset and validation_dataset dicts in the optimization config JSON should be accepted directly (as documented in agents-cli eval optimize --help), and non-string/non-dict values should raise a clean ClickException.
  2. Relative --config and --dataset paths passed from any subdirectory inside a project should resolve relative to the caller's working directory before chdir_project_root() runs (and missing files should fail cleanly via Click validation).

Client information

Client Information
CLI version:        1.13.0
OS info:            macOS-26.5.2-arm64-arm-64bit

Command Output / Logs

Repro 1 (TypeError on inline dataset dict):

⚠️  Warning: `eval optimize` is experimental and may change.
agents-cli v1.13.0
Traceback (most recent call last):
  ...
  File ".../google/agents/cli/eval/cmd_optimize.py", line 246, in cmd_optimize
    opt_data = _load_configs_and_datasets(config_path, dataset_file, target_metric)
  File ".../google/agents/cli/eval/cmd_optimize.py", line 73, in _load_configs_and_datasets
    with open(train_dataset_path, encoding="utf-8") as f_in:
TypeError: expected str, bytes or os.PathLike object, not dict

Repro 2 (FileNotFoundError from subdirectory):

⚠️  Warning: `eval optimize` is experimental and may change.
Using project root directory: .../my-agent
agents-cli v1.13.0
Traceback (most recent call last):
  ...
  File ".../google/agents/cli/eval/cmd_optimize.py", line 246, in cmd_optimize
    opt_data = _load_configs_and_datasets(config_path, dataset_file, target_metric)
  File ".../google/agents/cli/eval/cmd_optimize.py", line 63, in _load_configs_and_datasets
    with open(config_path, encoding="utf-8") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'file_config.json'

Anything else we need to know?

Root cause & verified fix in src/google/agents/cli/eval/cmd_optimize.py:

  1. Add a helper to load dataset entries uniformly from either dict or str paths (raising click.ClickException on invalid types) and only load config datasets when --dataset is not overriding them:
    def _load_dataset_entry(value: Any, field_name: str) -> dict | None:
        """Load a dataset entry from an inline dict or a JSON file path."""
        if value is None:
            return None
        if isinstance(value, dict):
            return value
        if isinstance(value, str):
            with open(value, encoding="utf-8") as f_in:
                return json.load(f_in)
        raise click.ClickException(
            f"'{field_name}' in optimization config must be a file path or JSON object, "
            f"got {type(value).__name__}."
        )
    
    (Also guard (eval_config.get("criteria") or {}).get(target_metric) in case criteria is null.)
  2. Add type=click.Path(exists=True, dir_okay=False, resolve_path=True) to both --dataset and --config so Click resolves paths relative to the caller's cwd before chdir_project_root() runs.

halcyon-past avatar Nov 11 '26 14:11 halcyon-past

Hello @halcyon-past Thanks for reporting this issue. We will look into it and provide you an update.

asrujana-44 avatar Nov 11 '26 23:11 asrujana-44