在GCP Cloud Run调用OpenAI Completions API时遇CloudFlare 400错误
GCP Cloud Run调用OpenAI API遇CloudFlare 400错误(本地请求正常)
问题详情
在GCP Cloud Run环境中调用OpenAI的Chat Completions API时,收到CloudFlare返回的400错误,但本地发起完全相同的请求可正常执行。
错误响应
<html><head><title>400 Bad Request</title></head><body><center><h1>400 Bad Request</h1></center><hr><center>cloudflare</center></body></html>
请求信息
- 请求地址:
https://api.openai.com/v1/chat/completions - 请求方法:POST
- 请求体:
{"model":"gpt-4-turbo","messages":[{"role":"system","content":"Using the notes provided, write a summary of the critical information from the notes.\nThe summary should be in dotpoints. Prioritise the dotpoints by importance."},{"role":"user","content":" Use only the following information to generate the summary:\n There are the following notes about the contact:\n Note Content: \"Test\"\nNote Created At: 2024-09-26T07:08:04.304501Z\nNote is in the context of: Just the contact in the role of Unknown \nNote type: Manual"}]}
排查与解决方向
共享IP池被风控拦截
Cloud Run默认使用GCP共享出站IP池,这类IP可能被CloudFlare标记为高风险流量源。可通过为Cloud Run配置VPC连接器分配静态出站IP,避开共享IP池的风控限制。请求头隐性差异
即使表面无异常,Cloud Run环境可能自动注入X-Forwarded-For、Via等代理头,或User-Agent与本地请求不一致。需完全对齐本地请求的所有头信息(包括User-Agent、Accept、Content-Type等),并移除不必要的自动注入头。请求体格式篡改
检查Cloud Run环境中请求体是否被意外修改:确认编码为UTF-8,换行符、空格等格式未被篡改(比如本地\n是否变为\r\n),转义字符是否保持一致。区域访问限制
确认Cloud Run所在区域是否在OpenAI API允许访问的范围内,若存在区域限制,可通过VPC配置切换到允许的出站区域节点。
内容的提问来源于stack exchange,提问作者HJED
相关产品推荐
相关产品推荐

