fix(llm): unify JSON output handling across providers and fix tool+json_output compatibility

- Remove redundant `response_format` injection for VOLCANO provider since it's unsupported; rely on system prompt injection instead
- Extend system prompt JSON injection logic to cover VOLCANO and tool-enabled cases universally
- Simplify model parameter construction by removing redundant `params["model_kwargs"] = model_kwargs` assignments
- Refactor `CompatibleChatOpenAI._get_request_payload` to strip `response_format` when tools are present, avoiding strict validation errors in langchain_openai
- Fix timestamp calculation order in `datetime_tool.py` to avoid integer truncation before multiplication
This commit is contained in:
Timebomb2018
2026-04-17 14:19:40 +08:00
parent 52f7ea7456
commit 377ddd2b9b
5 changed files with 51 additions and 26 deletions

View File

@@ -12,7 +12,7 @@ import time
from typing import Any, AsyncGenerator, Dict, List, Optional, Sequence
from langchain.agents import create_agent
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from langchain_core.tools import BaseTool
from langgraph.errors import GraphRecursionError
@@ -83,7 +83,12 @@ class LangChainAgent:
# ChatTongyi 要求 messages 含 'json' 字样才能使用 response_format
# 在 system prompt 中注入 JSON 要求
from app.models.models_model import ModelProvider
if json_output and provider.lower() == ModelProvider.DASHSCOPE and not is_omni:
if json_output and (
(provider.lower() == ModelProvider.DASHSCOPE and not is_omni)
or provider.lower() == ModelProvider.VOLCANO
# 有工具时 response_format 会被移除,所有 provider 都需要 system prompt 注入保证 JSON 输出
or bool(tools)
):
self.system_prompt += "\n请以JSON格式输出。"
logger.debug(
@@ -240,9 +245,7 @@ class LangChainAgent:
Returns:
List[BaseMessage]: 消息列表
"""
messages:list = [SystemMessage(content=self.system_prompt)]
# 添加系统提示词
messages: list = []
# 添加历史消息
if history: