Proxy Authentication Required 407

Hello. Please help to improve code to communicate with model via class ChatOpenAI (from langchain_openai import ChatOpenAI).

Now I have error “Proxy Authentication Required 407”

I’ve tried different options for about 2 weeks and I’m already giving up.I tried using clientx, but in vain.

my code:


from langchain_openai import ChatOpenAI
from langchain_core.message import HumanMessage, AIMessage
class LLMClient:

def init(self):
self.model = “Qwen3-Coder-Next-FP8”
self.headers = {
“Authorization”: “Bearer eyJ…”
}

self.llm = ChatOpenAI (
  model = self.model,
  api_key = "None",
  base_url = "https://mysite.com/codegen/v1",
  temperature=0.5,
  default_headers=self.headers
)

def SendMessage:
messages =
messages.appdend({“role”:“user”, “content”: “Hello my friend”})

langchain_messages = []
for msg in messages:
   if msg["role"] == "user":
     langchain_messages.append(HumanMessage(content=msg["content"]))
     
response = await self.llm.ainvoke([HumanMessage(content=langchain_messages)])
print(response.content)

---
Another way exclude ChatOpenAI working good:
import asyncio
from aiohttp import ClientSession, ClientTimeout

class HTTPClient():
  def __init__(self):
    self.model = "Qwen3-Coder-Next-FP8",
    self.api_key = "None",
    self.max_retries = 3
    self.base_url = "https://mysite.com/codegen/v1/chat/completions",
    self.temperature=0.5,
    self.headers = {
      "Content-Type": "application/json",
      "Authentication": "Bearer eyJ......"
    }
  
  async def _call_llm_with_rety(self, messages:list, max_retries: int = None) -> Optional[str]:
    url = self.base_url
    
    for attempt in range (max_retries + 1 ):
      try:
        timeout = ClientTimeout(total=self.timeout)
        async with ClintSession(timeout=timeout) as session:
          headers= self.headers
          payload = { "model": self.model, "messages": messages, "temperature": 0.1 }
          
          async with session.post(url, json=payload, headers=headers) as response:
            if response.status == 200:
              data = await response.json()
              return data.get("choises", [{}])[0].get("message", {}).get("content", "")

--- Please help!

Hi @letsmoto welcome to the langchain community, 407 means an HTTP proxy wants credentials, not that your API rejected the Bearer token (that’s usually 401/403).

ChatOpenAI uses httpx, which follows HTTP_PROXY / HTTPS_PROXY and system proxy settings. Your aiohttp call likely skips the proxy, so it works while LangChain hits the proxy and gets 407.

Check:

import os
print({k: os.environ[k] for k in os.environ if "proxy" in k.lower()})

If you don’t need a proxy (same path as aiohttp):

import httpx
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

llm = ChatOpenAI(
    model="Qwen3-Coder-Next-FP8",
    api_key="YOUR_TOKEN",
    base_url="https://mysite.com/codegen/v1",
    http_client=httpx.Client(trust_env=False),
    http_async_client=httpx.AsyncClient(trust_env=False),
)
response = await llm.ainvoke([HumanMessage(content="Hello my friend")])

If you need the proxy, set credentials via openai_proxy="http://user:pass@proxy:8080" or HTTPS_PROXY with auth.

Also fix in your snippet:

  • Use api_key="YOUR_TOKEN", not "None".
  • base_url = .../v1 only (no /chat/completions).
  • from langchain_core.messages import HumanMessage

Something else helped me. Everything turned out to be much simpler.
Let me clarify that I have Windows.
I added
import truststore
import pip-system-certs
next:
llm = ChatOpenAI(
model=model,
openai_api_key=my_token,
openai_api_base=url,
temperature=0.7,
openai_proxy=None
)

@letsmoto if that works for can you mark your answer as solution so it is helpful for other members of the community.