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

使用curl调用Google PaLM API时JSON格式错误的解决方法

解决Google PaLM API调用中JSON payload引号格式错误问题

问题说明

在PyCharm中用Python通过curl调用Google PaLM模型API时,payload里的单引号(比如you've)或双引号会触发JSON格式错误,报错信息为:

"Invalid JSON payload received. Closing quote expected in string.\n\n^"

目前靠移除数据中的引号能临时解决,但不想修改原始数据,需要更合理的方案。

错误原因

当前代码使用shell=True执行curl命令,Python生成的JSON字符串会被shell解析,payload中的引号会和shell命令的引号冲突,直接破坏JSON结构。比如shell单引号包裹的内容里出现单引号时,会被shell提前截断,导致JSON格式失效。

解决方案

方案1:用Python requests库替代curl(推荐)

直接用requests发送POST请求,完全绕开shell解析引号的问题,原始数据无需修改,代码更简洁易维护。

示例代码:

import requests

# 原始数据,保留所有引号
d = [
    {
        "context": """
        I want you to act as an attentive and sympathetic customer service agent for Google.
            1) you should use the context as the only source of truth and DO NOT make up answers on your own. 
            """,
        "messages": [{"author": "user","content": "How to unlock Google account?"}]
    }
]

# 配置请求参数
token = "你的token"
proj = "你的项目ID"
model = "模型名称"
url = f"https://us-central1-aiplatform.googleapis.com/v1/projects/{proj}/locations/us-central1/publishers/google/models/{model}:predict"
headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json"
}
payload = {
    "instances": d,
    "parameters": {"temperature": 0.0,"maxDecodeSteps": 200,"topP": 0.8,"topK": 1}
}

# 发送请求并处理响应
response = requests.post(url, headers=headers, json=payload)
print(response.text)

方案2:用临时文件传递payload(保留curl调用)

把生成的完整JSON写入临时文件,让curl从文件读取payload,彻底避免shell解析引号的问题。

示例代码:

import subprocess
import json
import tempfile
import os

d = [
    {
        "context": """
        I want you to act as an attentive and sympathetic customer service agent for Google.
            1) you should use the context as the only source of truth and DO NOT make up answers on your own. 
            """,
        "messages": [{"author": "user","content": "How to unlock Google account?"}]
    }
]

token = "你的token"
proj = "你的项目ID"
model = "模型名称"

# 生成完整的payload JSON
payload = json.dumps({
    "instances": d,
    "parameters": {"temperature": 0.0,"maxDecodeSteps": 200,"topP": 0.8,"topK": 1}
}, ensure_ascii=True)

# 写入临时文件
with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as f:
    f.write(payload)
    temp_file_path = f.name

# 构造curl命令,通过@符号读取文件内容
curl_cmd = [
    "curl", "-X", "POST",
    "-H", f"Authorization: Bearer {token}",
    "-H", "Content-Type: application/json",
    f"https://us-central1-aiplatform.googleapis.com/v1/projects/{proj}/locations/us-central1/publishers/google/models/{model}:predict",
    "-d", f"@{temp_file_path}"
]

# 执行命令并输出结果
result = subprocess.run(curl_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(result.stdout)
print(result.stderr)

# 清理临时文件
os.unlink(temp_file_path)

方案3:用subprocess参数列表避免shell解析

不使用shell=True,而是把curl的每个参数作为列表元素传递,让subprocess直接执行命令,跳过shell的解析步骤,避免引号冲突。

示例代码:

import subprocess
import json

d = [
    {
        "context": """
        I want you to act as an attentive and sympathetic customer service agent for Google.
            1) you should use the context as the only source of truth and DO NOT make up answers on your own. 
            """,
        "messages": [{"author": "user","content": "How to unlock Google account?"}]
    }
]

token = "你的token"
proj = "你的项目ID"
model = "模型名称"

# 生成完整的payload JSON
payload_json = json.dumps({
    "instances": d,
    "parameters": {"temperature": 0.0,"maxDecodeSteps": 200,"topP": 0.8,"topK": 1}
}, ensure_ascii=True)

# 构造curl命令参数列表
curl_cmd = [
    "curl", "-X", "POST",
    "-H", f"Authorization: Bearer {token}",
    "-H", "Content-Type: application/json",
    f"https://us-central1-aiplatform.googleapis.com/v1/projects/{proj}/locations/us-central1/publishers/google/models/{model}:predict",
    "-d", payload_json
]

# 执行命令并输出结果
result = subprocess.run(curl_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print(result.stdout)
print(result.stderr)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 11:47:06