Skip to content

ai peer dependency is not truly optional — top-level static import in tool.mjs crashes on load #327

@jaredatron

Description

@jaredatron

@stackone/ai declares ai as an optional peer dependency in peerDependenciesMeta, but dist/src/tool.mjs has a top-level static import:

  import { jsonSchema } from "ai";

This causes an immediate crash when the module is loaded without ai installed, even if you never call any method that uses it.

Reproduction:

  mkdir repro && cd repro
  npm init -y
  npm install @stackone/ai zod
  # **Note: do NOT install `ai`**
  node -e "async function main() { const { StackOneToolSet } = await import('@stackone/ai'); console.log('ok'); } main()"

Expected: The import succeeds since ai is marked as optional and I'm not using any AI SDK adapter methods.

Actual:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ai' imported from
.../node_modules/@stackone/ai/dist/src/tool.mjs

Analysis:

The other optional peer deps (@anthropic-ai/sdk, @anthropic-ai/claude-agent-sdk, openai) are correctly loaded lazily via tryImport() in dist/src/utils/try-import.mjs. But
jsonSchema from ai is imported statically at the top of tool.mjs and used in BaseTool.toClaudeAgentSdkTool().

Suggested fix:

Use tryImport for ai in toClaudeAgentSdkTool(), consistent with how the other optional peers are handled:

  async toClaudeAgentSdkTool() {
    const { jsonSchema } = await tryImport("ai", `npm install ai (requires ${peerDependencies.ai})`);
    const inputSchema = jsonSchema(this.toJsonSchema());
    // ...
  }

Versions:

  • @stackone/ai: 2.4.0
  • Node.js: v22.x

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions