错误:模型 gemini-3.1-pro-preview: invalid character '<' looking for beginning of val...
🚨 错误信息
错误:模型 gemini-3.1-pro-preview: invalid character '<' looking for beginning of value
🤖 AI解决方案
报错分析报告
1.错误翻译
原文:invalid character '<' looking for beginning of value
译文:寻找值的起始位置时遇到无效字符‘<’
2. 原因分析
'<'是HTML的起始字符,而非JSON的{/[)。 gemini-3.1-pro-preview模型API响应的JSON阶段,因响应以<开头触发解析错误。3. 解决方案
python
import requests
import json配置API请求
url = "YOUR_DMXAPI_ENDPOINT" # 替换为实际端点
payload = {
"model": "gemini-3.1-pro-preview",
"messages": [{"role": "user", "content": "Hello"}] # 替换为实际参数
}try:
# 发送请求并检查状态码
response = requests.post(url, json=payload, timeout=10)
response.raise_for_status() # 非200状态码直接抛异常
# 安全解析JSON
try:
result = response.json()
print("解析成功:", result)
except json.JSONDecodeError:
print(f"JSON解析失败!响应内容(前200字): {response.text[:200]}")
raise # 或自定义错误处理
except requests.exceptions.RequestException as e:
print(f"API请求失败: {str(e)}")
4. 预防措施
HTTP状态码(非200时先处理错误,不直接解析);用try-except捕获JSONDecodeError并打印原始响应。 application/json类型的合法JSON。 requests库(自带raise_for_status())、JSON校验工具(如[JSONLint](https://jsonlint.com/))。