---
title: "Charles 抓包导致 langchain 报错的解决方案"
description: "在开发环境中，使用 Charles 作为代理服务器来监控 Python 应用（如 LangChain 或 OpenAI SDK）的网络流量时抓包失败，显示 You may need to configure your browser or application to trust the Charles Root Certificate. See SSL Proxying in the Help menu"
canonical_url: "https://yangqiuyi.com/blog/kai-fa/charles-zhua-bao-dao-zhi-langchain-bao-cuo-de-jie-jue-fang-an"
---

> Index of every page on this site: [llms.txt](https://yangqiuyi.com/llms.txt)

# Charles 抓包导致 langchain 报错的解决方案

在开发环境中，使用 Charles 作为代理服务器来监控 Python 应用（如 LangChain 或 OpenAI SDK）的网络流量时抓包失败，显示 `You may need to configure your browser or application to trust the Charles Root Certificate. See SSL Proxying in the Help menu`

问题的核心在于，Python 生态中主流的 HTTP 客户端库，包括 requests 及其异步对应 httpx（LangChain 与新版 OpenAI SDK 的底层依赖），默认不调用操作系统的信任库。它们依赖于一个名为 certifi 的 Python 包，该包提供了一套独立的、硬编码的、受信任的根证书颁发机构（CA）证书集。因此，即使 Charles 根证书在系统中受信任，但它并未包含在 Python 环境所使用的 certifi 证书包内。这导致当 Python 应用尝试通过 Charles 代理与 HTTPS 端点通信时，由于无法在 certifi 的信任链中找到有效的根证书，SSL 证书验证失败。

## 方法一：设置环境变量

首先在 Charles 中导出 SSL 证书，然后在终端里设置环境变量：

- **macOS / Linux:**```bash
export SSL_CERT_FILE="/path/to/charles.pem"
python your_script.py
```
- **Windows (PowerShell):**```powershell
$env:SSL_CERT_FILE = "C:\path\to\charles.pem"
python your_script.py
```

### 方法二：把 Charles 证书加进 certifi 的信任列表里

1. **在 Charles 中导出 SSL 证书**
2. **找到 Python 证书包位置：**

```python
import certifi
print(certifi.where())
```

1. **把 Charles 证书加进去：**

```bash
# macOS / Linux
cat /path/to/charles.pem >> /path/to/certifi/cacert.pem

# Windows
type C:\path\to\charles.pem >> C:\path\to\certifi\cacert.pem
```

### 方法三：代码中关闭安全检查

修改代码，在运行时跳过验证：

```python
import httpx
from langchain_openai import ChatOpenAI

insecure_client = httpx.Client(verify=False)
insecure__async_client=httpx.AsyncClient(verify=False)

llm = ChatOpenAI(
    # ...
    http_client=insecure_client,
    async_client=insecure__async_client
)
```

## Sitemap

See the full [sitemap](https://yangqiuyi.com/sitemap.md) for all pages.
