core: _parse_google_docstring mishandles continuation lines containing colons

用户在调用 LangChain 的 _parse_google_docstring 函数时触发,该函数位于 langchain_core/utils/function_calling.py 。当函数文档字符串(docstring)的参数描述包含多行,且续行中包含冒号(例如 "for finding

core: _parse_google_docstring mishandles continuation lines containing colons

core: _parse_google_docstring mishandles continuation lines containing colons

快速结论:该报错发生在使用 LangChain 框架解析 Google 风格文档字符串时,如果参数描述的续行中包含冒号,解析器会错误地将续行当作新参数定义。优先排查文档字符串中续行的缩进是否比参数定义行更深。

问题场景

用户在调用 LangChain 的 _parse_google_docstring 函数时触发,该函数位于 langchain_core/utils/function_calling.py。当函数文档字符串(docstring)的参数描述包含多行,且续行中包含冒号(例如 “for finding things: important ones”)时,解析器无法正确合并续行。

报错原文

core: _parse_google_docstring mishandles continuation lines containing colons

# 实际输出:3 个参数,而非 2 个
# 预期: query (description: "The search query to use for finding things: important ones"), top_k
# 实际: query, for finding things (treated as a new arg name), top_k

原因分析

根本原因是解析器使用简单的 if ":" in line 条件来检测参数定义行,但没有考虑行缩进。在 Google 风格文档字符串中,续行通常有更深的缩进(相对于参数定义行),但解析器没有利用这一特征来区分参数定义行和续行。

环境排查

  • 确认使用的 LangChain 版本(建议 >=0.3.0,但问题可能存在于多个版本)。
  • 确认文档字符串是否严格遵循 Google 风格:参数定义行使用浅缩进,续行使用更深缩进。
  • 检查文档字符串中续行是否包含冒号字符。

解决步骤

  1. 手动修复方式:在编写文档字符串时,避免在参数描述的续行中使用冒号。或者,将所有参数描述合并到一行中(不使用续行)。
  2. 代码级修复(推荐):从 Args: 部分的第一行参数定义提取基础缩进级别,将任何缩进更深的行视为当前参数描述的续行,无论该行是否包含冒号。
  3. 等待官方修复:该 Issue 已关闭(2026-06-23),且社区已提交修复 PR。更新到最新版本的 LangChain 可能包含此修复。

验证方法

使用 Issue 中提供的重现代码测试:

from langchain_core.utils.function_calling import _parse_google_docstring

def search(query: str, top_k: int = 5) -> str:
    """Search the knowledge base.

    Args:
        query: The search query to use
            for finding things: important ones
        top_k: Number of results to return
    """

result = _parse_google_docstring(search.__doc__, ["query", "top_k"])
print(result)

修复后应输出包含 query(描述包含 “for finding things: important ones”)和 top_k 两个参数的结果,而不是三个参数。

参考来源

langchain-ai/langchain #35679

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

celebrityanime
celebrityanime
文章: 10195

发表回复

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