ruvnet/ruflo · error · Error

Failed to import OpenAI

Error message

Failed to import OpenAI

What it means

Thrown when the dynamic import('openai') inside endpointOai fails (the catch wraps the underlying error as the cause). The OpenAI SDK is loaded lazily so that environments that never call the OpenAI endpoint do not need it installed; if the package cannot be resolved, the endpoint refuses to construct.

Source

Thrown at ruflo/src/ruvocal/src/lib/server/endpoints/openai/endpointOai.ts:73

): Promise<Endpoint> {
	const {
		baseURL,
		apiKey,
		completion,
		model,
		defaultHeaders,
		defaultQuery,
		multimodal,
		extraBody,
		useCompletionTokens,
		streamingSupported,
	} = endpointOAIParametersSchema.parse(input);

	let OpenAI;
	try {
		OpenAI = (await import("openai")).OpenAI;
	} catch (e) {
		throw new Error("Failed to import OpenAI", { cause: e });
	}

	// Store router metadata if captured
	let routerMetadata: { route?: string; model?: string; provider?: string } = {};

	// Custom fetch wrapper to capture response headers for router metadata
	const customFetch = async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
		const response = await fetch(url, init);

		// Capture router headers if present (fallback for non-streaming)
		const routeHeader = response.headers.get("X-Router-Route");
		const modelHeader = response.headers.get("X-Router-Model");
		const providerHeader = response.headers.get("x-inference-provider");

		if (routeHeader && modelHeader) {
			routerMetadata = {
				route: routeHeader,
				model: modelHeader,

View on GitHub (pinned to 6b01dc5a68)

Solutions

  1. Install the SDK in the ruvocal package: npm install openai (or pnpm add openai in that workspace).
  2. Run a clean reinstall: rm -rf node_modules package-lock.json && npm install.
  3. Check the underlying cause — read e.cause from the thrown error for the real resolution/import failure.
  4. Confirm the installed openai version satisfies the engines/Node version in use.

Example fix

// before — openai not installed → 'Failed to import OpenAI'

// after
// in ruflo/src/ruvocal:
npm install openai
// or pin: "openai": "^4.20.0" in package.json then npm install
Defensive patterns

Strategy: validation

Validate before calling

// at startup, confirm the SDK resolves
try { await import('openai'); } catch { throw new Error('openai package not installed — run npm install openai in ruflo/src/ruvocal'); }

Type guard

async function isOpenAiInstalled(): Promise<boolean> { try { await import('openai'); return true; } catch { return false; } }

Try / catch

try { return await endpointOai(input); } catch (e) { if (e instanceof Error && e.message === 'Failed to import OpenAI') throw new Error('OpenAI SDK missing — install it or check e.cause', { cause: e }); throw e; }

Prevention

When it happens

Trigger: The 'openai' npm package is not installed, not resolvable from the ruvocal package's node_modules, or its version throws on import (e.g. an incompatible ESM/CJS build, a broken hoist in a monorepo/pnpm setup, or a corrupt install).

Common situations: A fresh clone where npm install was not run; the openai dep was moved to optionalDependencies and skipped; pnpm hoisting/raycasting prevents resolution; a Node version older than the SDK's engines requirement; a bundler stripped the dynamic import target; a broken node_modules after a failed install.

Related errors


AI-assisted analysis of ruvnet/ruflo@6b01dc5a68 (2026-08-12). Data as JSON: /api/errors/288759c5ff5f3d38. Report an issue: GitHub.