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

Vertex AI函数调用引号转义失败问题求助

Vertex AI Gemini 1.5 Pro函数调用时含单引号的字符串被截断问题

问题概述

在Vertex AI上使用Gemini 1.5 Pro进行人物属性标注,通过强制函数调用生成指定Schema的JSON时,遇到一个问题:当人物简介字符串包含单引号时,输出会在单引号位置直接截断,导致结果不符合预期。

比如要求生成"The person's name is Jack.",实际输出仅为"The person"。

复现代码

"""Helper functions for interacting with Vertex AI's LLMs"""

import os

import vertexai
from dotenv import load_dotenv
from google.auth import default
from vertexai.generative_models import GenerativeModel
from vertexai.preview.generative_models import (FunctionDeclaration, Tool,
                                                ToolConfig)

load_dotenv()


SCHEMA = {
    "type": "object",
    "properties": {
        "summary": {
            "type": "string",
            "description": "Summary about the person, using full sentences.",
        },
    },
    "required": ["bullet_points"],  # 配置错误:required字段与properties不匹配
}

PROMPT = "The person's summary should be exactly this: The person's name is Jack."
SYSTEM = "You are a person summarizer."

credentials, _ = default()
vertexai.init(project=os.getenv("GCP_PROJECT_NAME"), location="us-east4", credentials=credentials)

tool = Tool(
    function_declarations=[
        FunctionDeclaration(
            name="generate_content",
            parameters=SCHEMA,
        )
    ]
)

tool_config = ToolConfig(
    function_calling_config=ToolConfig.FunctionCallingConfig(
        mode=ToolConfig.FunctionCallingConfig.Mode.ANY,
        allowed_function_names=["generate_content"],
    )
)

model = GenerativeModel("gemini-1.5-pro", tools=[tool], tool_config=tool_config, system_instruction=SYSTEM)

response = model.generate_content(
    PROMPT,
    generation_config={
        "max_output_tokens": 200,
        "temperature": 0,
    },
    tools=[tool],
    tool_config=tool_config,
)

fc = response.candidates[0].content.parts[0].function_call

print(type(fc).to_dict(fc)["args"])

当前临时方案

在系统提示词中添加“确保转义简介中的所有引号符号”,但该方案稳定性不足,偶尔会失效。

问题排查与解决建议

1. 修正Schema的错误配置

你的Schema中required字段指定了["bullet_points"],但properties里并没有这个字段,这会导致模型对输出要求产生混淆,是明显的配置错误。将required修正为["summary"]:

SCHEMA = {
    "type": "object",
    "properties": {
        "summary": {
            "type": "string",
            "description": "Summary about the person, using full sentences.",
        },
    },
    "required": ["summary"],
}

2. 强制函数调用模式

将tool_config中的mode从Mode.ANY改为Mode.REQUIRED,强制模型必须调用指定函数并严格遵循Schema格式,减少格式异常:

tool_config = ToolConfig(
    function_calling_config=ToolConfig.FunctionCallingConfig(
        mode=ToolConfig.FunctionCallingConfig.Mode.REQUIRED,
        allowed_function_names=["generate_content"],
    )
)

3. 优化提示词精准度

将临时提示词替换为更明确的指令,确保模型正确处理单引号转义:

SYSTEM = "你是人物摘要生成器,必须严格按照指定Schema生成JSON。生成的summary字段值中,所有单引号必须用转义符\\转义,确保JSON格式合法,完整输出要求的内容。"

4. 检查是否为平台bug

如果完成上述调整后问题仍然存在,大概率是Vertex AI函数调用模块在处理字符串转义时的bug。此时可以:

  • 提交GCP技术支持工单,提供复现步骤和代码
  • 尝试切换Gemini模型的其他版本(如1.5 Flash)验证是否存在同样问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 03:47:34