Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
code:
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import OpenAI from 'openai'
import { fetch, ProxyAgent } from 'undici';
const app = new Hono()
const proxyUrl = process.env.HTTPS_PROXY
?? process.env.HTTP_PROXY
?? process.env.https_proxy;
console.log(`proxy ${proxyUrl}`);
const dispatcher = proxyUrl ? new ProxyAgent(proxyUrl) : undefined;
console.log(`dispatcher ${!!dispatcher}`);
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
fetchOptions: {
dispatcher,
}
})
app.post('/chat', async (c) => {
const { message } = await c.req.json()
const response =
await client.chat.completions.create({
model: 'gpt-4.1-mini',
messages: [
{
role: 'user',
content: message,
},
],
})
return c.json({
reply: response.choices[0].message.content,
})
})
serve(
{
fetch: app.fetch,
port: 3000,
},
() => {
console.log(
'🚀 Server running at http://localhost:3000',
)
},
)
error:
APIConnectionError: Connection error.
at OpenAI.makeRequest (/Users/app/ai-code/node_modules/.pnpm/openai@6.38.0/node_modules/openai/src/client.ts:792:13)
at async Array.<anonymous> (/Users/app/ai-code/src/index-openai-proxy.ts:43:5)
at async responseViaResponseObject (/Users/app/ai-code/node_modules/.pnpm/@hono+node-server@2.0.2_hono@4.12.19/node_modules/@hono/node-server/dist/index.cjs:710:13)
at async Server.<anonymous> (/Users/app/ai-code/node_modules/.pnpm/@hono+node-server@2.0.2_hono@4.12.19/node_modules/@hono/node-server/dist/index.cjs:808:11) {
status: undefined,
headers: undefined,
requestID: undefined,
error: undefined,
code: undefined,
param: undefined,
type: undefined,
cause: TypeError: fetch failed
at node:internal/deps/undici/undici:13502:13
at async OpenAI.fetchWithTimeout (/Users/app/ai-code/node_modules/.pnpm/openai@6.38.0/node_modules/openai/src/client.ts:969:14)
at async OpenAI.fetchWithAuth (/Users/app/ai-code/node_modules/.pnpm/openai@6.38.0/node_modules/openai/src/client.ts:934:22)
at async OpenAI.makeRequest (/Users/app/ai-code/node_modules/.pnpm/openai@6.38.0/node_modules/openai/src/client.ts:744:22)
at async Array.<anonymous> (/Users/app/ai-code/src/index-openai-proxy.ts:43:5)
at async responseViaResponseObject (/Users/app/ai-code/node_modules/.pnpm/@hono+node-server@2.0.2_hono@4.12.19/node_modules/@hono/node-server/dist/index.cjs:710:13)
at async Server.<anonymous> (/Users/app/ai-code/node_modules/.pnpm/@hono+node-server@2.0.2_hono@4.12.19/node_modules/@hono/node-server/dist/index.cjs:808:11) {
[cause]: InvalidArgumentError: invalid onRequestStart method
at assertRequestHandler (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/core/util.js:568:11)
at new Request (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/core/request.js:271:5)
at [dispatch] (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/client.js:342:21)
at Client.dispatch (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/dispatcher-base.js:171:29)
at [dispatch] (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/pool-base.js:164:28)
at Pool.dispatch (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/dispatcher-base.js:171:29)
at [dispatch] (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/agent.js:138:23)
at Agent.dispatch (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/dispatcher-base.js:171:29)
at ProxyAgent.dispatch (/Users/app/ai-code/node_modules/.pnpm/undici@8.3.0/node_modules/undici/lib/dispatcher/proxy-agent.js:250:25)
at node:internal/deps/undici/undici:11136:55 {
code: 'UND_ERR_INVALID_ARG'
}
}
}
To Reproduce
- run
HTTPS_PROXY=http://127.0.0.1:8888 OPENAI_API_KEY=sk-proj-QA56X-*** npx tsx src/index.ts
- run
curl -X POST http://localhost:3000/chat \
-H "Content-Type: application/json" \
-d '{"message":"what is AI"}'
Code snippets
OS
macOS
Node version
v22.14.0
Library version
^6.38.0
Confirm this is a Node library issue and not an underlying OpenAI API issue
Describe the bug
code:
error:
To Reproduce
HTTPS_PROXY=http://127.0.0.1:8888 OPENAI_API_KEY=sk-proj-QA56X-*** npx tsx src/index.tsCode snippets
OS
macOS
Node version
v22.14.0
Library version
^6.38.0