You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example shows how to run the Agents SDK with a model provided by the [AI SDK](https://www.npmjs.com/package/@ai-sdk/openai).
3
+
These examples show how to wrap models from the [AI SDK](https://www.npmjs.com/package/@ai-sdk/openai) (and compatible providers) with the `aisdk()` helper from `@openai/agents-extensions`, then run them inside the Agents runtime.
4
4
5
-
The [ai-sdk-model.ts](./ai-sdk-model.ts) script:
5
+
## Available scripts
6
6
7
-
- Wraps the AI SDK `openai` provider with `aisdk` from `@openai/agents-extensions`.
8
-
- Creates a simple `get_weather` tool that returns a mock weather string.
9
-
- Defines a data agent that uses this model and tool.
10
-
- Runs a parent agent that hands off to the data agent to answer a weather question.
|[index.ts](./index.ts)|`pnpm -F ai-sdk start`| OpenRouter (`OPENROUTER_API_KEY`) | Runs a parent agent that hands off a weather question to a child agent equipped with a `get_weather` tool. |
10
+
|[gpt-5.ts](./gpt-5.ts)|`pnpm -F ai-sdk start:gpt-5`| OpenAI (`OPENAI_API_KEY`) | Shows a single agent that must call the `get_weather` tool using `gpt-5-mini` with custom provider options. |
11
+
|[stream.ts](./stream.ts)|`pnpm -F ai-sdk start:stream`| OpenAI (`OPENAI_API_KEY`) | Demonstrates streaming text output from an AI SDK model wrapped with `aisdk()`. |
12
+
|[image-tool-output.ts](./image-tool-output.ts)|`pnpm -F ai-sdk start:image-tool-output`| OpenRouter (`OPENROUTER_API_KEY`) | Returns a `ToolOutputImage` from a tool call and asks the model to describe the image. |
11
13
12
-
## Running the script
14
+
## Prerequisites
13
15
14
-
From the repository root, execute:
15
-
16
-
```bash
17
-
pnpm -F ai-sdk start:sdk-model
18
-
```
19
-
20
-
The script prints the final output produced by the runner.
16
+
- Run `pnpm install` at the repo root.
17
+
- Export the relevant API key(s) shown in the table above before running a script.
21
18
19
+
Once the environment variable is set, run the corresponding command from the repository root. Each script prints the final output produced by the Agents runner.
// Run all handlers in parallel so a slow onStream callback does not block on(...) handlers (and vice versa).
597
+
awaitPromise.allSettled(
598
+
candidates.map((handler)=>
599
+
Promise.resolve().then(()=>handler(event)),
600
+
),
601
+
);
602
+
};
603
+
constbaseTool=tool({
552
604
name: toolName??toFunctionToolName(this.name),
553
605
description: toolDescription??'',
554
606
parameters: AgentAsToolNeedApprovalSchame,
@@ -560,10 +612,42 @@ export class Agent<
560
612
thrownewModelBehaviorError('Agent tool called with invalid input');
561
613
}
562
614
construnner=newRunner(runConfig??{});
563
-
constresult=awaitrunner.run(this,data.input,{
564
-
context,
565
-
...(runOptions??{}),
566
-
});
615
+
// Only flip to streaming mode when a handler is provided to avoid extra overhead for callers that do not need events.
616
+
// Flip to streaming if either a legacy onStream callback or event handlers are registered; otherwise stay on the non-stream path to avoid extra overhead.
0 commit comments