OpenAI Agents SDK
Test OpenAI Agents SDK agents, handoffs and tools in Python and TypeScript with automatic capture and a scripted model.
Adapter status: built and tested in TypeScript (@openai/agents 0.22) and Python (openai-agents 0.26).
Install the Python extra with viewport-evals[openai-agents].
What is captured
The first trace() or YAML case run adds a tracing processor next to your own exporters (addTraceProcessor /
add_trace_processor), so nothing you configured is replaced.
| Span | Event |
|---|---|
agent | agent (its name), parent of everything the agent did |
response, generation | model_call with model, usage and response id |
function | tool_call with args and the result or the span's error |
handoff | handoff (from_agent, to_agent) |
guardrail | guardrail (name, triggered), assertable with events |
If tracing is disabled with OPENAI_AGENTS_DISABLE_TRACING, nothing is captured, and a run that asserts on the
trajectory fails with evals.nothing_captured naming the cause. In TypeScript the SDK turns tracing off under
NODE_ENV=test, which vitest sets; in that case only (and unless OPENAI_AGENTS_DISABLE_TRACING is set) the
adapter turns tracing on with Viewport's processor as the only one, so tests capture trajectories and export
nothing.
Test an agent
script(...).openai_agents() / .openaiAgents() is a Model that plays the script.
@function_tool
def lookup_order(order_id: str) -> str:
"""Look up an order by id."""
return f"{order_id}: delivered, damaged"
@function_tool
def issue_refund(order_id: str, amount: float) -> str:
"""Refund an order."""
return f"R-{order_id.removeprefix('A-')}"
import { Agent, type Model, Runner, tool } from "@openai/agents";
import { z } from "zod";
const lookupOrder = tool({
name: "lookup_order",
description: "Look up an order by id",
parameters: z.object({ order_id: z.string() }),
execute: async ({ order_id }) => ({ order_id, status: "delivered", total: 42.5 }),
});
const issueRefund = tool({
name: "issue_refund",
description: "Refund an order",
parameters: z.object({ order_id: z.string(), amount: z.number() }),
execute: async () => ({ refund_id: "R-1001" }),
});
/** A triage agent that hands off to a support agent, run with a `Runner`. */
export async function runSupport(model: Model, prompt: string): Promise<string> {
const support = new Agent({
name: "support",
instructions: "Resolve order problems.",
model,
tools: [lookupOrder, issueRefund],
});
const triage = new Agent({ name: "triage", instructions: "Route to support.", model, handoffs: [support] });
const result = await new Runner().run(triage, prompt);
return String(result.finalOutput);
}
async def test_refund_agent() -> None:
model = script(
tool_call("lookup_order", order_id="A-1001"),
tool_call("issue_refund", order_id="A-1001", amount=42.5),
reply("Refund R-1001 issued."),
)
agent = Agent(name="support", model=model.openai_agents(), tools=[lookup_order, issue_refund])
with trace() as t:
result = await Runner.run(agent, "My order A-1001 arrived damaged.")
assert t.tool_names == ["lookup_order", "issue_refund"]
assert t.called("issue_refund", order_id="A-1001", amount=lte(100))
assert t.within("support").tool_names == ["lookup_order", "issue_refund"]
assert len(t.model_calls) == 3
assert "R-1001" in result.final_output
model.assert_consumed()
import { lte, trace } from "@viewportai/evals";
import { reply, script, toolCall } from "@viewportai/evals/testing";
import { expect, test } from "vitest";
import { runSupport } from "../src/agent.js";
test("triage hands off to support, which refunds the order", async () => {
const model = script(
toolCall("transfer_to_support", {}),
toolCall("lookup_order", { order_id: "A-1001" }),
toolCall("issue_refund", { order_id: "A-1001", amount: 42.5 }),
reply("Refund R-1001 for $42.50 is on its way."),
{ model: "gpt-5" },
);
const t = await trace(() => runSupport(model.openaiAgents(), "Order A-1001 arrived damaged."));
expect(t.agents.map((a) => a.name)).toEqual(["triage", "support"]);
expect(t.within("support").toolNames).toEqual(["lookup_order", "issue_refund"]);
expect(t).toHaveCalledTool("issue_refund", { order_id: "A-1001", amount: lte(100) });
expect(t).not.toHaveCalledTool("delete_account");
expect(t).toHaveModelCalls(4);
expect(t).toBeWithinBudget({ tokens: 2000, cost_usd: 0.01 });
expect(t.output).toContain("R-1001");
model.assertConsumed();
});
The TypeScript example hands off from a triage agent to a support agent. Handoffs count as tool calls named
transfer_to_<agent> in the model's script and as handoff events in the trace; t.within("support") scopes the
tool assertions to the agent that did the work. In YAML:
expect:
agents: { exactly: [triage, support], within: { support: { tools: { exactly: [lookup_order, issue_refund] } } } }
handoffs: [support]With a real model
Use the default OpenAI model provider. Its HTTP traffic to api.openai.com is recorded on the first local run and
replayed after. See modes and fixtures.