You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perplexity User Location Filter调用报错:需同时提供经纬度与country参数

解决Perplexity API用户位置验证错误的方案

问题修复方法

根据错误提示,Perplexity API当前要求使用经纬度做本地化时,必须同时提供country参数。你需要在user_location对象中补充该字段,修改后的代码如下:

import requests

url = "https://api.perplexity.ai/chat/completions"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
payload = {
    "model": "sonar-pro",
    "messages": [
        {"role": "system", "content": "You are a helpful local guide."},
        {"role": "user", "content": "What are some good coffee shops nearby?"}
    ],
    "web_search_options": {
      "user_location": {
        "latitude": 37.7749,
        "longitude": -122.4194,
        "country": "US"  # 补充对应ISO 3166-1 alpha-2格式的国家代码
      }
    }
}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

country参数需传入ISO 3166-1 alpha-2标准的两位国家代码(如"CN"代表中国、"GB"代表英国),需与经纬度对应的国家匹配。

是否属于Bug?

这不属于API本身的Bug,而是官方文档的示例存在疏漏。当前API的验证规则明确要求经纬度必须与country参数同时提供,但文档示例未包含该字段,导致开发者按示例调用时触发错误。建议后续关注官方文档的更新,确保示例与API实际要求一致。

内容的提问来源于stack exchange,提问作者Udl David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 22:42:32