Core Module
The core module provides the main evaluation functions and result container.
Main Functions
- toolscore.core.evaluate(expected, actual, weights=None, strict=False, forbidden=None, final_answer=None, honesty=None)[source]
Evaluate tool calls by comparing actual against expected (in-memory).
This is the simplest way to use Toolscore - pass Python dicts directly, no file I/O required. Raw OpenAI, Anthropic, Gemini, LangGraph, Pydantic AI, OpenAI Agents SDK, and Claude Agent SDK responses are auto-detected and converted automatically, including list-shaped formats (Claude Agent SDK message lists and bare LangGraph message lists).
- Parameters:
expected (
list[dict[str,Any]]) – List of expected tool calls, each a dict with ‘tool’ and optional ‘args’.actual (
list[dict[str,Any]] |Any) – List of actual tool calls from your agent, same format. Also accepts raw OpenAI/Anthropic/Gemini response objects or dicts (auto-detected).weights (
dict[str,float] |None) – Optional custom weights for the composite score. Keys: ‘selection_accuracy’, ‘argument_f1’, ‘sequence_accuracy’, ‘redundant_rate’, ‘required_call_recall’ (0 by default; give it a weight to penalize required calls that were skipped or failed). Provided values are merged with the defaults then renormalized so that all weights sum to 1.0 before computing the composite score.strict (
bool) – When True, argument comparison uses pure equality (no int/float coercion, no string strip). Default is False (lenient matching).forbidden (
list[dict[str,Any]] |None) – Optional calls the agent must never make, as rule dicts withtool, optionalargs(values or matchers) and optionalreason. Violations are reported inmetrics["policy_metrics"]andEvaluationResult.policy_violations; they do not change the score. Seetoolscore.metrics.policy.final_answer (
str|None) – What the agent told the user at the end. When omitted it is read from a rawactualthat holds one (a chat history ending in an assistant message, a Responses output, an agent result). The honesty checks (metrics["honesty_metrics"]) use it; they do not change the score.honesty (
HonestyConfig|None) – Patterns and tool lists for the honesty checks (toolscore.metrics.honesty.HonestyConfig).
- Return type:
- Returns:
EvaluationResult with metrics and a composite .score property.
Example
>>> from toolscore import evaluate >>> result = evaluate( ... expected=[{"tool": "search", "args": {"q": "test"}}], ... actual=[{"tool": "search", "args": {"q": "test"}}], ... ) >>> result.score 1.0
- toolscore.core.assert_tools(expected, actual, min_score=0.9, weights=None, strict=False)[source]
Assert that actual tool calls meet a minimum composite score.
Convenience function for use in pytest or any test framework.
- Parameters:
expected (
list[dict[str,Any]]) – List of expected tool calls.actual (
list[dict[str,Any]] |Any) – List of actual tool calls.min_score (
float) – Minimum composite score required (0.0 to 1.0).weights (
dict[str,float] |None) – Optional custom weights for the composite score.strict (
bool) – When True, argument comparison uses pure equality (no int/float coercion, no string strip). Default is False.
- Return type:
- Returns:
EvaluationResult if assertion passes.
- Raises:
ValueError – If min_score is outside [0.0, 1.0].
ToolScoreAssertionError – If the composite score is below min_score.
- toolscore.core.test_agent(agent, input, expected, min_score=None, weights=None, strict=False)[source]
End-to-end test helper: run an agent, extract tool calls, evaluate.
Calls
agent(input), passes the response through auto-detection to extract tool calls, and evaluates against expected.- Parameters:
agent (
Callable[...,Any]) – Any callable that accepts a string and returns an LLM response (raw provider response or list of tool-call dicts). Must be a synchronous callable; usetest_agent_async()for async agents.input (
str) – The prompt / user message to send to the agent.expected (
list[dict[str,Any]]) – List of expected tool calls.min_score (
float|None) – If provided, raisesToolScoreAssertionErrorwhen the composite score is below this threshold.weights (
dict[str,float] |None) – Optional custom weights for the composite score.strict (
bool) – When True, argument comparison uses pure equality.
- Return type:
- Returns:
EvaluationResult with metrics and a composite
.scoreproperty.- Raises:
TypeError – If agent is an async function or returns an awaitable.
ValueError – If min_score is outside [0.0, 1.0].
ToolScoreAssertionError – If min_score is set and the score is below it.
- async toolscore.core.test_agent_async(agent, input, expected, min_score=None, weights=None, strict=False)[source]
Async end-to-end test helper: run an agent, extract tool calls, evaluate.
Works for both synchronous and asynchronous agents. Calls
agent(input)and, if the result is awaitable, awaits it.- Parameters:
agent (
Callable[...,Any]) – Any callable that accepts a string and returns an LLM response (raw provider response or list of tool-call dicts). May be sync or async.input (
str) – The prompt / user message to send to the agent.expected (
list[dict[str,Any]]) – List of expected tool calls.min_score (
float|None) – If provided, raisesToolScoreAssertionErrorwhen the composite score is below this threshold.weights (
dict[str,float] |None) – Optional custom weights for the composite score.strict (
bool) – When True, argument comparison uses pure equality.
- Return type:
- Returns:
EvaluationResult with metrics and a composite
.scoreproperty.- Raises:
ValueError – If min_score is outside [0.0, 1.0].
ToolScoreAssertionError – If min_score is set and the score is below it.
- toolscore.core.evaluate_trace(gold_file, trace_file, format='auto', validate_side_effects=True, judge=False, forbidden=None, weights=None, final_answer=None, honesty=None, injections=None)[source]
Evaluate an agent’s trace against gold standard.
- Parameters:
gold_file (
str|Path) – Path to gold standard specification, or a contract file (required / optional / forbidden calls and alternatives, seetoolscore.contract).format (
str) – Trace format (one ofTRACE_FORMATS; ‘auto’ detects it).validate_side_effects (
bool) – Whether to validate side effects.judge (
JudgeConfig|str|bool) – LLM-as-a-judge configuration for semantic evaluation.False(default) disables it.Trueuses a defaultJudgeConfig(). A string is treated as a model-name shorthand. AJudgeConfigis used as given. Provider is inferred from the model name (or set explicitly viaJudgeConfig):claude-*-> Anthropic,gemini-*-> Gemini, abase_url-> any OpenAI-compatible endpoint (Ollama/vLLM/Groq), otherwise OpenAI.forbidden (
list[dict[str,Any]] |None) – Optional calls the agent must never make (seeevaluate()); violations go tometrics["policy_metrics"].weights (
dict[str,float] |None) – Optional custom weights for the composite score (seeevaluate()).final_answer (
str|None) – The agent’s final answer, when the trace does not hold it; the honesty checks use it (metrics["honesty_metrics"]).honesty (
HonestyConfig|None) – Patterns and tool lists for the honesty checks.injections (
list[dict[str,Any]] |None) – Faults injected bytoolscore mcp proxy --inject, from a separate proxy session file; matched to the trace’s calls by tool name and order. A proxy session trace carries its own.
- Return type:
- Returns:
EvaluationResult containing all computed metrics.
- Raises:
FileNotFoundError – If files don’t exist.
ValueError – If file formats are invalid.
- toolscore.core.load_gold_standard(file_path)[source]
Load gold standard specification from JSON file.
- Parameters:
- Return type:
- Returns:
List of expected tool calls.
- Raises:
FileNotFoundError – If file doesn’t exist.
ValueError – If file format is invalid.
- toolscore.core.load_trace(file_path, format='auto')[source]
Load agent trace from JSON file.
- Parameters:
- Return type:
- Returns:
List of tool calls from the trace.
- Raises:
FileNotFoundError – If file doesn’t exist.
ValueError – If format is invalid or unsupported.
- toolscore.core.adapter_for(data, format='auto')[source]
The adapter that reads
datainformat("auto"detects it).- Parameters:
- Return type:
- Returns:
The adapter.
- Raises:
ValueError – If the format is not supported.
- class toolscore.transcript.Transcript(calls, final_answer=None, final_source=None, injections=list(), success=None)[source]
The calls of a run and what the agent said at the end.
- Variables:
final_answer (
str|None) – The agent’s final answer, orNonewhen the trace has none.final_source (
str|None) – Where the final answer was found (for reports), orNone.injections (
list[dict[str,Any]]) – Faults injected bytoolscore mcp proxy --inject(from a proxy session file); empty otherwise.success (
bool|None) – The agent’s own verdict on the run, when the trace records one (browser-use’sdonesuccess);Noneotherwise.
- Parameters:
- toolscore.transcript.load_transcript(file_path, format='auto', final_answer=None)[source]
Load a trace file as a
Transcript(calls, final answer, injected faults).- Parameters:
- Return type:
- Returns:
The transcript.
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If the format is not supported.
- toolscore.transcript.transcript_from_data(data, format='auto', final_answer=None)[source]
Build a
Transcriptfrom parsed trace data.- Parameters:
data (
Any) – The parsed trace.format (
str) – The trace format, as fortoolscore.core.load_trace().final_answer (
str|None) – The final answer, when you have it from elsewhere (it wins over anything found in the trace).
- Return type:
- Returns:
The transcript.
- toolscore.transcript.final_answer_of(data, calls=None)[source]
Find the agent’s final answer in a trace (see the module docstring).
- toolscore.transcript.normalize_tool_name(name)[source]
A tool name without host prefixes:
mcp__browser__click->click.Claude Code names MCP tools
mcp__<server>__<tool>, and some hosts writefunctions.<tool>or<server>.<tool>.
- toolscore.integrations.auto_extract(actual)[source]
Auto-detect the provider format of a response and extract tool calls.
This allows passing raw OpenAI, Anthropic, Gemini, LangGraph, Pydantic AI, OpenAI Agents SDK, or Claude Agent SDK responses directly to
evaluate()without manually calling the framework-specific helper.Detection order: 1. Already a list of dicts with
"tool"keys → pass through 2. Object withmodel_dump()→ convert to dict, then re-detect 3. Dict with"choices"→ OpenAI Chat Completions; a Responses API response (object == "response"orfunction_call/mcp_callitems inoutput) or item list →from_openai_responses()4. Dict with"content"list containing"type"keys → Anthropic format 5. Dict with"candidates"→ Gemini format 6. Dict/object with"messages"where some message hastool_calls→ LangGraph 7. Object with callableall_messages→ Pydantic AI 8. Object/dict withnew_items→ OpenAI Agents SDK 9. List whose items have content blocks withtype == "tool_use"→ Claude Agent SDK; a list of Pydantic AI messages, OpenAI Agents SDKtool_call_itemitems, or CrewAItool_name/tool_argsentries → that framework’s extractor 10. Bare list of messages where some message hastool_calls→ LangGraphOpenTelemetry GenAI tool spans (an OTLP export with
resourceSpans, or a span list containingexecute_tool/ MCPtools/callspans) and browser-use histories (anAgentHistoryList, its dump or its step list) are detected before step 2 and converted withfrom_otel()andfrom_browser_use().- Parameters:
actual (
Any) – A raw LLM provider response (object or dict), or an already-formatted list of tool-call dicts.- Return type:
- Returns:
List of dicts with ‘tool’ and ‘args’ keys.
- Raises:
TypeError – If the format cannot be detected.
ValueError – If a LangGraph/message-list shape with
tool_callsis detected but no tool calls can be extracted from it.
- toolscore.integrations.from_otel(spans)[source]
Extract tool calls from OpenTelemetry GenAI tool spans.
Works with any framework or platform that records tool executions with the OpenTelemetry semantic conventions for generative AI (
execute_toolspans) or for MCP (tools/callspans). Pass an OTLP JSON export ({"resourceSpans": [...]}), a list of span dicts, or a list of OpenTelemetry SDK span objects (for example from anInMemorySpanExporter).- Parameters:
spans (
Any) – The exported spans.- Return type:
- Returns:
One dict per tool span, in start-time order, with
tool,args,result,is_error,error,durationandid. Failures come fromerror.typeor an ERROR span status.
Example
>>> from toolscore import evaluate, from_otel >>> result = evaluate(expected=[{"tool": "search_orders"}], actual=from_otel(otlp_export))
- toolscore.integrations.from_openai_responses(response)[source]
Extract tool calls from an OpenAI Responses API response or item list.
Accepts an
openai.types.responses.Response(or its dict form), itsoutputlist, or the input item list of a multi-turn conversation.Handles:
function_callitems (name+ JSONarguments),mcp_callitems (remote MCP tools; theiroutputanderrorare kept),custom_tool_callitems (free-forminput, kept as{"input": ...}).
When the list also holds
function_call_output/custom_tool_call_outputitems, each output is attached to its call (bycall_id) asresult. An output is recorded as anerroronly when it clearly is one: a JSON object witherrororis_error: true, or the OpenAI Agents SDK’s “An error occurred while running the tool” message. Built-in tool items (web search, file search, code interpreter, …) have no tool name and arguments and are skipped.
Result Container
- class toolscore.core.EvaluationResult[source]
Container for evaluation results.
- DEFAULT_WEIGHTS: ClassVar[dict[str, float]] = {'argument_f1': 0.3, 'redundant_rate': 0.1, 'required_call_recall': 0.0, 'selection_accuracy': 0.4, 'sequence_accuracy': 0.2}
- property score: float
Compute weighted composite score from key metrics.
Default weights: selection_accuracy=0.4, argument_f1=0.3, sequence_accuracy=0.2, redundant_rate=0.1, required_call_recall=0.0.
Selection accuracy only judges the calls that were made, so by default a trace that skips required calls can still score well. Give
required_call_recalla weight (for exampleweights={"required_call_recall": 0.3}) to make missing and failed required calls lower the score.- Returns:
Composite score between 0.0 and 1.0.
- property selection_accuracy: float
proportion of calls matching expected tool names.
- Type:
Selection accuracy
- property required_call_recall: float | None
Share of required (expected) calls that were made and did not fail, counting repeats.
Nonewhen nothing was required. Seetoolscore.metrics.calculate_required_call_recall().
- property false_success: dict[str, Any] | None
The honesty summary counts (see
toolscore.metrics.honesty), orNone.
- property scored_calls: list[ToolCall]
The calls the score compares with the expected ones.
Every trace call, except those a contract’s
optionalentries left out of the score (markedmetadata["contract_optional"]).
- property evidence: dict[str, Any]
Tri-state checks (pass / fail / unknown) and evidence coverage.
A check that lacks the data it needs (no results captured, no final answer) is
unknown, never a pass. Seetoolscore.evidence.
- property policy_violations: list[dict[str, Any]]
Calls that matched a
forbiddenrule (empty when no rules were given).
- RECORD_SCHEMA_VERSION: ClassVar[str] = '2'
Version of the
to_dict()record layout. Bumped on any change that could break a consumer; new keys alone do not bump it.
- to_dict()[source]
Return a complete, JSON-safe record of the evaluation.
The record is meant to be stored or handed to other tools as-is:
schema_version: layout version of this record ("2").score,grade,weights: the composite score, its letter grade and the normalized weights that produced it.required_call_recall: seerequired_call_recall.metrics: every computed metric.evidence: the tri-state checks (pass/fail/unknown), their counts and the evidence coverage (evidence).calls:expectedandactualcalls with their arguments and, for actual calls,id,result,is_error,error,duration,costandtimestamp(nullwhen the trace does not have them: unknown, never 0). Values that are not JSON (matcher objects, arbitrary result objects) are stored as theirrepr.gold_calls_count,trace_calls_count: kept for compatibility.