Docs: JSON schema ‘title’ requirement not clearly documented

该报错通常出现在 LangChain 中向 create_agent 或结构化输出传递自定义 JSON schema 时,schema 缺少顶层 title 键。优先在 schema 中添加 title (如 "title": "MySchema" ),或改用 Pydantic 模型。

快速结论:该报错通常出现在 LangChain 中向 create_agent 或结构化输出传递自定义 JSON schema 时,schema 缺少顶层 title 键。优先在 schema 中添加 title(如 "title": "MySchema"),或改用 Pydantic 模型。

适用环境:LangChain(任意版本,该行为自引入 convert_to_openai_function 后存在);操作系统:macOS(Issue 中报告的系统,非必需)。

最快修复方案:在 JSON schema 字典顶层添加 title 键(值任意,通常为描述功能的名称)。例如原 schema:

{
  "type": "object",
  "description": "Contact information for a person.",
  "properties": {...},
  "required": [...]
}

改为:

{
  "title": "ContactInfo",
  "type": "object",
  "description": "Contact information for a person.",
  "properties": {...},
  "required": [...]
}

注意事项:LangChain 内部在将 JSON schema 转为 OpenAI 函数工具时,使用 title 作为工具名称;虽然 JSON Schema 规范中 title 为可选,但在 LangChain 中为必需。若不愿手动添加,推荐改用 Pydantic 模型自动处理。

问题场景

用户在 LangChain 中使用 create_agentresponse_format 等结构化输出接口时,传递一个自定义的 JSON schema 字典(例如用于提取联系人信息的 schema),但未包含顶层 title 键,导致 LangChain 在校验时抛出错误。

报错原文

Functions must be passed in as Dict, pydantic.BaseModel, or Callable. If they're a dict they must either be in OpenAI function format or valid JSON schema with top-level 'title' and 'description' keys.

原因分析

LangChain 的 convert_to_openai_functionconvert_to_openai_tool 函数在接收字典参数时,要求字典必须为 OpenAI 函数格式,或为包含顶层 titledescription 的 JSON schema。原因在于 LangChain 需要从一个稳定的字段中提取工具名称,而 title 被用作该标识。文档未明确说明此要求,导致用户传入标准 JSON schema(即缺少 title,因为它在规范中为可选)时触发错误。

环境排查

  • LangChain 版本:建议使用最新稳定版(该行为自 langchain-core 早期版本存在至当前最新版),可运行 python -c "import langchain; print(langchain.__version__)" 确认。
  • Python 版本:通常不影响,但建议 Python ≥ 3.9。
  • 底层模型:Issue 中示例使用 GPT,该错误与模型无关,是 LangChain 自身的 schema 校验。

解决步骤

以下三种方案均可解决,任选其一(按推荐顺序排列):

  1. 方案 A:手动添加 title
    在 JSON schema 字典顶层添加 "title": "YourSchemaName"。例如:

    contact_info_schema = {
        "title": "ContactInfo",
        "type": "object",
        "description": "Contact information for a person.",
        "properties": {
            "name": {"type": "string", "description": "The name of the person"},
            "email": {"type": "string", "description": "The email address of the person"},
            "phone": {"type": "string", "description": "The phone number of the person"}
        },
        "required": ["name", "email", "phone"]
    }
  2. 方案 B:改用 Pydantic 模型(推荐)
    使用 Pydantic 模型定义 schema,LangChain 自动提取模型名作为 title

    from pydantic import BaseModel, Field
    
    class ContactInfo(BaseModel):
        """Contact information for a person."""
        name: str = Field(description="The name of the person")
        email: str = Field(description="The email address of the person")
        phone: str = Field(description="The phone number of the person")
    
    # 直接传入模型类或实例
    result = agent.invoke(...)
  3. 方案 C:使用 OpenAI 函数格式
    直接使用 OpenAI 标准的工具格式,包含 nameparameters

    contact_info_tool = {
    "name": "extract_contact_info",
    "description": "Extract contact information for a person.",
    "parameters": {
    "type": "object",
    "properties": {
    "

    参考来源

    langchain-ai/langchain #34662

    GamsGo AI

    AI 工具推荐

    想把多个 AI 模型放在一个入口?

    GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

    了解 GamsGo AI

    推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

    这个方案解决了吗?

celebrityanime
celebrityanime
文章: 15228

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注