ToolEvals¶
ToolEvals lets you write and run unit tests for your CXAS tools — without needing a full agent session. Tests are described in simple YAML files, which makes them easy to version-control alongside your tool code and review in pull requests.
Each test case specifies a tool name, the input args, optional variables (session state), and expectations that assert things about the response using the Operator enum:
| Operator | Meaning |
|---|---|
equals | exact match |
contains | substring or element check |
greater_than / less_than | numeric comparison |
length_equals / length_greater_than / length_less_than | collection size |
is_null / is_not_null | presence check |
run_tool_tests() returns a pandas DataFrame with columns for test name, tool, status, latency, and errors — easy to save as a CSV or display in a notebook.
Quick Example¶
from cxas_scrapi import ToolEvals
app_name = "projects/my-project/locations/us/apps/my-app-id"
te = ToolEvals(app_name=app_name)
# Load tests from a YAML file
test_cases = te.load_tool_test_cases_from_file("tool_tests/lookup_account.yaml")
# Run them and get a results DataFrame
results_df = te.run_tool_tests(test_cases)
print(results_df[["test_name", "tool", "status", "latency (ms)"]])
# Generate a summary report
report_df = ToolEvals.generate_report(results_df)
print(report_df)
A minimal YAML test file looks like this:
tests:
- name: lookup_known_customer
tool: lookup_account
args:
customer_id: "C-1234"
expectations:
response:
- path: "$.account_status"
operator: equals
value: "active"
- path: "$.balance"
operator: is_not_null
Reference¶
ToolEvals ¶
Utility class for testing CXAS Tools.
Initializes the ToolEvals class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_name | str | CXAS App name (projects/{project}/locations/{location}/apps/{app}). | required |
creds | Any | Optional Google Cloud credentials. | None |
Source code in src/cxas_scrapi/evals/tool_evals.py
load_tool_test_cases_from_file ¶
Loads tool tests from a YAML file.
Source code in src/cxas_scrapi/evals/tool_evals.py
load_tool_tests_from_dir ¶
Recursively loads all YAML tool tests from a directory.
Source code in src/cxas_scrapi/evals/tool_evals.py
load_tool_test_cases_from_yaml ¶
Loads tool tests from a YAML string.
Source code in src/cxas_scrapi/evals/tool_evals.py
load_tool_test_cases_from_data ¶
Loads tool tests from a list of dictionaries.
Source code in src/cxas_scrapi/evals/tool_evals.py
validate_tool_test ¶
Validates the tool response and variables against expectations.
Returns:
| Type | Description |
|---|---|
list[str] | List of error messages. Empty list if all expectations pass. |
Source code in src/cxas_scrapi/evals/tool_evals.py
run_tool_tests ¶
Runs a list of tool tests.
Returns:
| Type | Description |
|---|---|
DataFrame | A pandas DataFrame of results with status and errors. |
Source code in src/cxas_scrapi/evals/tool_evals.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | |
generate_tool_tests ¶
generate_tool_tests(target_dir='tool_tests', include_tools=None, exclude_tools=None, overwrite=False, mine_tool_data=False, mine_conversations_limit=50)
Generates configurable YAML test templates for tools defined in the app.
Parses the application's OpenAPI tool schemas or Python underlying functions to try and intelligently scaffold the request arguments and expected responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_dir | str | The directory path where the generated YAML files will be saved. Defaults to 'tool_tests'. | 'tool_tests' |
include_tools | list[str] | None | An optional list of tool display names to restrict the generation. If None, all tools in the app are evaluated. | None |
exclude_tools | list[str] | None | An optional list of tool display names (or prefixes) to exclude from generation. Matches if a tool's display name starts with any string in this list. | None |
overwrite | bool | If True, existing YAML test templates in the target directory will be overwritten. If False, existing files are skipped. | False |
mine_tool_data | bool | If True, queries recent conversations to populate generated tests with real tool payload arguments. | False |
mine_conversations_limit | int | The maximum number of conversations to scan when mining real tool arguments. | 50 |
Source code in src/cxas_scrapi/evals/tool_evals.py
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 | |
tool_tests_to_dataframe ¶
Converts tool test results to a pandas DataFrame for reporting.
Source code in src/cxas_scrapi/evals/tool_evals.py
generate_report staticmethod ¶
Generates a summary report DataFrame capturing key metrics from tool evaluation results.
Source code in src/cxas_scrapi/evals/tool_evals.py
Operator ¶
Bases: str, Enum
Operators for testing expectations.
ToolTestCase ¶
Bases: BaseModel
Data model for a tool test case.
Expectation ¶
Bases: BaseModel
Data model for a single test expectation.