[Bug]: Using claude-sonnet-5 throws “LLM must be a FunctionCallingLLM”

在 LlamaIndex 中使用 llama-index-llms-anthropic==0.11.7 或更高版本,初始化 Anthropic LLM 时使用 model="claude-sonnet-5" ,然后将其传递给 FunctionAgent 并调用 agent.run() 。运行时会抛出

[Bug]: Using claude-sonnet-5 throws "LLM must be a FunctionCallingLLM"

[Bug]: Using claude-sonnet-5 throws “LLM must be a FunctionCallingLLM”

快速结论:该报错发生在调用 FunctionAgent 并传递 claude-sonnet-5 模型时,原因是 is_function_calling_model 字符串匹配逻辑未包含 "-5",导致模型被错误识别为不支持函数调用。优先检查 llama-index-llms-anthropic/utils.pyis_function_calling_model 函数的匹配规则。

问题场景

LlamaIndex 中使用 llama-index-llms-anthropic==0.11.7 或更高版本,初始化 Anthropic LLM 时使用 model="claude-sonnet-5",然后将其传递给 FunctionAgent 并调用 agent.run()。运行时会抛出 "LLM must be a FunctionCallingLLM" 异常。

报错原文

LLM must be a FunctionCallingLLM

原因分析

已知是模型名称检测逻辑存在漏洞。在 llama-index-llms-anthropicutils.py 中,is_function_calling_model 函数使用简单的字符串匹配来判断模型是否支持函数调用:

def is_function_calling_model(modelname: str) -> bool:
    return "-3" in modelname or "-4" in modelname

由于 "claude-sonnet-5" 字符串中既没有包含 "-3" 也没包含 "-4",该函数返回 False,导致 FunctionAgent 拒绝接受该 LLM 实例。

环境排查

  • 确认 llama-index-core 版本(示例中为 0.14.8)。
  • 确认 llama-index-llms-anthropic 版本(示例中为 0.11.7)。
  • 运行以下测试代码以确认问题:
from llama_index.llms.anthropic import Anthropic
from llama_index.llms.anthropic.utils import is_function_calling_model

llm = Anthropic(model="claude-sonnet-5", api_key="dummy")
print("is_function_calling_model:", is_function_calling_model("claude-sonnet-5"))
print("metadata:", llm.metadata.is_function_calling_model)

如果两行均输出 False,则确认是匹配逻辑缺失所致。

解决步骤

  1. 临时解决(无需修改源码):创建一个 Anthropic 的子类,重写 metadata 属性,强制将 is_function_calling_model 设为 True。此方法可优先尝试。
from llama_index.llms.anthropic import Anthropic
from llama_index.core.llms import LLMMetadata

class AnthropicClaude5(Anthropic):
    @property
    def metadata(self) -> LLMMetadata:
        md = super().metadata
        return LLMMetadata(
            context_window=md.context_window,
            num_output=md.num_output,
            is_chat_model=True,
            model_name=md.model_name,
            is_function_calling_model=True,
        )

llm = AnthropicClaude5(model="claude-sonnet-5", api_key="your-key")
  1. 永久修复(修改源码):编辑 llama_index/llms/anthropic/utils.py 中的 is_function_calling_model 函数,将匹配规则扩展为:
def is_function_calling_model(modelname: str) -> bool:
    return "-3" in modelname or "-4" in modelname or "-5" in modelname

或者采用更健壮的正则表达式匹配所有 Claude 版本 ≥ 3 的模型。

验证方法

在应用修复后,重新运行开头的问题复现代码:

  • is_function_calling_model("claude-sonnet-5") 应返回 True
  • llm.metadata.is_function_calling_model 应为 True
  • FunctionAgent 调用 agent.run() 不再抛出 "LLM must be a FunctionCallingLLM" 异常。

参考来源

run-llama/llama_index #22216 — 讨论中包含 原始 is_function_calling_model 源码及 Dosu 提供的完整分析和两种解决方案。

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

celebrityanime
celebrityanime
文章: 10860

发表回复

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