[Bug]: `experimental_use_latest_role_message_only` erroneously sending tool and assistant prompts to guardrail

用户在 LiteLLM Proxy 中配置了 Bedrock Guardrail,并启用了 experimental_use_latest_role_message_only: true (该功能预期只发送最新的系统提示和第一条用户提示)。但在实际使用中,当最新角色消息是 tool 或 assist

[Bug]: `experimental_use_latest_role_message_only` erroneously sending tool and assistant prompts to guardrail

[Bug]: experimental_use_latest_role_message_only erroneously sending tool and assistant prompts to guardrail

快速结论:该 Bug 发生在 LiteLLM Proxy 使用 Bedrock Guardrail 并启用 experimental_use_latest_role_message_only: true 时。配置期望仅将最新角色的用户消息发送给护栏检查,但实际错误地将 role: "tool"role: "assistant" 的提示也发送给了护栏,导致预期外的拦截。优先排查是否在 pre_call 模式下错误地包含了非用户角色的消息。

问题场景

用户在 LiteLLM Proxy 中配置了 Bedrock Guardrail,并启用了 experimental_use_latest_role_message_only: true(该功能预期只发送最新的系统提示和第一条用户提示)。但在实际使用中,当最新角色消息是 toolassistant 时,这些消息也被发送给了护栏检查,即使护栏配置为 mode: [pre_call](仅调用前执行)。用户预期护栏应过滤掉非用户角色的消息,但实际发生了误判。

报错原文

Error: Guardrail blocked due to assistant prompt sent during pre_call mode.
Showing role: "tool" prompt getting send to guardrail.
Showing role: "assistance" prompt getting sent to guardrail even though it's configured for only pre_call.
Desired behaviour is that the system role will be validated in the post_call.

原因分析

可能原因:experimental_use_latest_role_message_only 在判断“最新角色消息”时存在逻辑漏洞。

  • 角色过滤不严格:该功能设计上可能仅过滤了 role: "user" 的消息,但未正确处理或排除 role: "tool"role: "assistant" 的消息。导致这些非用户角色的消息被错误地纳入护栏检查的有效负载中。
  • 预调用模式逻辑缺陷:配置为 mode: [pre_call] 时,系统本应只在调用前检查用户输入。但当前代码可能错误地将 toolassistant 角色的消息也视为需要检查的“最新角色消息”。

环境排查

  • LiteLLM 版本:v1.81.0(用户报告版本)
  • 护栏提供商:AWS Bedrock Guardrail
  • 护栏配置:mode: [pre_call],并设置 experimental_use_latest_role_message_only: true

解决步骤

  1. 确认 Bug 存在:guardrails 配置中移除 experimental_use_latest_role_message_only: true,改为默认行为。观察护栏是否还会错误拦截 toolassistant 消息。这可以排除是否有其他配置干扰。
  2. 等待上游修复:该 Issue 已由维护者 @krrish-berri-2 标记优先处理,但 Issue 关闭时并未提供具体修复补丁。建议关注 Repo 中的最新 Release 或 PR,更新至 v1.81.0 之后的版本以获取修复。
  3. 临时规避方案:如果必须使用 experimental_use_latest_role_message_only,可在应用层确保发送给护栏的消息结构中,手动过滤掉 role: "tool"role: "assistant" 的消息,只保留 role: "user" 和必要的 system 提示。但需注意这并非根本解决方法。
  4. 确认 post_call 行为:如果护栏在 post_call 模式下执行,Bug 可能影响不同。开发者应确认在 post_call 模式下,系统角色消息是否会被正确处理。Issue 中提到“Desired behaviour is that the system role will be validated in the post_call”,意味着当前 post_call 对此逻辑也可能有隐患,需谨慎测试。

验证方法

在修复后的版本中,使用包含 toolassistant 角色的最后一条消息(如用户消息后跟工具调用结果)再次发送请求。确认护栏仅拦截包含冒犯性内容的用户消息,而不再拦截 toolassistant 角色消息。如果环境允许,可以对比 experimental_use_latest_role_message_only: true 开启前后护栏的拦截日志,确保非用户角色消息被正确跳过。

参考来源

BerriAI/litellm #23476

GamsGo AI

AI 工具推荐

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

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

了解 GamsGo AI

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

celebrityanime
celebrityanime
文章: 10205

发表回复

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