![[Bug]: Using claude-sonnet-5 throws "LLM must be a FunctionCallingLLM"](https://www.chat-gpts.plus/wp-content/uploads/2026/07/22216-7a1b3db7.jpg)
[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.py 中 is_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-anthropic 的 utils.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,则确认是匹配逻辑缺失所致。
解决步骤
- 临时解决(无需修改源码):创建一个
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")
- 永久修复(修改源码):编辑
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 提供的完整分析和两种解决方案。



