Setting up proxy configuration for OpenAI API calls

I’m having trouble configuring a proxy server for my OpenAI API integration. When I enable the proxy settings, the connection fails with an error. Without the proxy, everything works perfectly but I need the requests to go through a proxy server. Here’s my current setup:

const { Configuration, OpenAIApi } = require('openai');
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const { HttpsProxyAgent } = require('https-proxy-agent');
require('dotenv').config();

const proxyAgent = new HttpsProxyAgent(process.env.PROXY_SERVER_URL);

const config = new Configuration({
  apiKey: process.env.OPENAI_KEY,
  baseOptions: {
    httpsAgent: proxyAgent
  }
});

const ai = new OpenAIApi(config);
const botId = process.env.BOT_ID;

const server = express();
const PORT = 3000;

server.use(cors({
  origin: ['http://localhost:5500'],
  methods: ['GET', 'POST'],
  credentials: true
}));

server.use(bodyParser.json());

server.post('/chat', async (req, res) => {
  res.setHeader('Content-Type', 'text/event-stream');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Connection', 'keep-alive');

  try {
    console.log('User input:', req.body.text);
    
    const bot = await ai.retrieveAssistant(botId);
    console.log('Bot retrieved:', bot);
    
    const conversation = await ai.createThread();
    console.log('Thread created:', conversation);

    await ai.createMessage(conversation.id, {
      role: "user",
      content: req.body.text
    });
    console.log('User message added:', req.body.text);

    const stream = ai.createRun(conversation.id, {
      assistant_id: bot.id
    })
    .on('textCreated', (text) => {
      res.write(`data: \nbot > \n`);
    })
    .on('textDelta', (delta) => {
      const cleanText = delta.value.replace(/ /g, '&nbsp;').replace(/\n/g, '<br>');
      res.write(`data: ${cleanText}\n`);
      console.log('Text chunk:', delta);
    })
    .on('end', () => {
      res.end();
      console.log('Stream finished.');
    });

  } catch (err) {
    console.error('Request failed:', err);
    res.status(500).json({ error: "API request failed" });
  }
});

server.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

When I comment out the httpsAgent line, the server works but doesn’t use the proxy. With the proxy enabled, I get this error:

User input: hello
Request failed: APIConnectionError: Connection error.
FetchError: request to https://api.openai.com/v1/assistants/asst_xyz failed, reason: Proxy connection ended before receiving CONNECT response

The proxy connection seems to close before getting a proper response. Has anyone solved this issue before?

This proxy connection issue usually happens when the OpenAI client can’t handle the proxy handshake properly. I hit the same problem setting up proxy support in a corporate environment. The newer OpenAI SDK versions changed how they handle HTTP connections. Don’t use httpsAgent in baseOptions - configure the proxy at the axios level instead since OpenAI uses axios under the hood. Create a custom axios instance with your proxy config and pass it to the OpenAI client. Also check if your proxy server supports the CONNECT method for HTTPS tunneling - lots of corporate proxies block this by default. Make sure your proxy URL format includes the protocol and any auth credentials are encoded correctly in the URL string.

Your proxy config looks right, but it’s probably an OpenAI client version issue. I hit the same proxy timeout problems and fixed it by adding explicit timeouts and retry logic. Set a longer timeout in your proxy agent - the default’s way too short for OpenAI responses. Also check if your proxy needs keep-alive disabled. I had to add keepAlive: false to my httpsAgent options to stop connections from hanging. One more thing - make sure your proxy handles streaming responses since you’re using server-sent events. Some proxies buffer everything which breaks streaming.

had the same issue b4 - try updating your https-proxy-agent version. check if your proxy needs auth too, sometimes the agent setup forgets username/password. adding timeout settings in baseOptions with httpsAgent worked for me.