Skip to content

Commit

Permalink
openai[patch]: set global ssl context (#29932)
Browse files Browse the repository at this point in the history
We set 
```python
global_ssl_context = ssl.create_default_context(cafile=certifi.where())
```
at the module-level and share it among httpx clients.
  • Loading branch information
ccurme authored Feb 24, 2025
1 parent 9ce0798 commit 291a232
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import os
import re
import ssl
import sys
import warnings
from functools import partial
Expand All @@ -33,6 +34,7 @@
)
from urllib.parse import urlparse

import certifi
import openai
import tiktoken
from langchain_core._api.deprecation import deprecated
Expand Down Expand Up @@ -104,6 +106,10 @@

logger = logging.getLogger(__name__)

# This SSL context is equivelent to the default `verify=True`.
# https://www.python-httpx.org/advanced/ssl/#configuring-client-instances
global_ssl_context = ssl.create_default_context(cafile=certifi.where())


def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
"""Convert a dictionary to a LangChain message.
Expand Down Expand Up @@ -564,7 +570,9 @@ def validate_environment(self) -> Self:
"Could not import httpx python package. "
"Please install it with `pip install httpx`."
) from e
self.http_client = httpx.Client(proxy=self.openai_proxy)
self.http_client = httpx.Client(
proxy=self.openai_proxy, verify=global_ssl_context
)
sync_specific = {"http_client": self.http_client}
self.root_client = openai.OpenAI(**client_params, **sync_specific) # type: ignore[arg-type]
self.client = self.root_client.chat.completions
Expand All @@ -577,7 +585,9 @@ def validate_environment(self) -> Self:
"Could not import httpx python package. "
"Please install it with `pip install httpx`."
) from e
self.http_async_client = httpx.AsyncClient(proxy=self.openai_proxy)
self.http_async_client = httpx.AsyncClient(
proxy=self.openai_proxy, verify=global_ssl_context
)
async_specific = {"http_client": self.http_async_client}
self.root_async_client = openai.AsyncOpenAI(
**client_params,
Expand Down

0 comments on commit 291a232

Please # to comment.