🐛 fix: 修复 GuidelineMiddleware 导致 assistant message prefill 报错

enable_thinking 开启时,thinking 中间件将 AIMessage 追加到 messages 末尾,
导致不支持 assistant prefill 的模型返回 400 错误。
修复方式:在 AIMessage 后追加多语言 HumanMessage,确保消息以 user 结尾。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
朱潮 2026-04-09 15:02:28 +08:00
parent 05e22391ed
commit 513dda8bbb

View File

@ -6,7 +6,7 @@ from utils.fastapi_utils import (extract_block_from_system_prompt, format_messag
from langchain.chat_models import BaseChatModel
from langgraph.runtime import Runtime
from langchain_core.messages import SystemMessage
from langchain_core.messages import SystemMessage, HumanMessage
from typing import Any, Callable
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.outputs import LLMResult
@ -124,10 +124,11 @@ Action: Provide concise, friendly, and personified natural responses.
response.additional_kwargs["message_tag"] = "THINK"
response.content = f"<think>{response.content}</think>"
# 将响应添加到原始消息列表
state['messages'] = state['messages'] + [response]
# 将响应添加到原始消息列表,并追加 HumanMessage 确保消息以 user 结尾
# 某些模型不支持 assistant message prefill要求最后一条消息必须是 user
state['messages'] = state['messages'] + [response, HumanMessage(content=self._get_follow_up_prompt())]
return state
async def abefore_agent(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
if not self.guidelines:
return None
@ -148,10 +149,23 @@ Action: Provide concise, friendly, and personified natural responses.
response.additional_kwargs["message_tag"] = "THINK"
response.content = f"<think>{response.content}</think>"
# 将响应添加到原始消息列表
state['messages'] = state['messages'] + [response]
# 将响应添加到原始消息列表,并追加 HumanMessage 确保消息以 user 结尾
# 某些模型不支持 assistant message prefill要求最后一条消息必须是 user
state['messages'] = state['messages'] + [response, HumanMessage(content=self._get_follow_up_prompt())]
return state
def _get_follow_up_prompt(self) -> str:
"""根据语言返回引导主 agent 回复的提示"""
prompts = {
"ja": "以上の分析に基づいて、ユーザーに返信してください。",
"jp": "以上の分析に基づいて、ユーザーに返信してください。",
"zh": "请根据以上分析,回复用户。",
"zh-TW": "請根據以上分析,回覆用戶。",
"ko": "위 분석을 바탕으로 사용자에게 답변해 주세요.",
"en": "Based on the above analysis, please respond to the user.",
}
return prompts.get(self.language, prompts["en"])
def wrap_model_call(
self,
request: ModelRequest,