mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-19 04:14:46 +08:00
feat: be compatible with case: json_object is not supported by used model (#673)
* feat: 兼容使用的模型不支持json结构化输出的情况 * fix: add explicit validation that the response content is valid JSON before proceeding to parse it --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
@@ -282,10 +282,7 @@ def planner_node(
|
|||||||
if configurable.enable_deep_thinking:
|
if configurable.enable_deep_thinking:
|
||||||
llm = get_llm_by_type("reasoning")
|
llm = get_llm_by_type("reasoning")
|
||||||
elif AGENT_LLM_MAP["planner"] == "basic":
|
elif AGENT_LLM_MAP["planner"] == "basic":
|
||||||
llm = get_llm_by_type("basic").with_structured_output(
|
llm = get_llm_by_type("basic")
|
||||||
Plan,
|
|
||||||
method="json_mode",
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
llm = get_llm_by_type(AGENT_LLM_MAP["planner"])
|
llm = get_llm_by_type(AGENT_LLM_MAP["planner"])
|
||||||
|
|
||||||
@@ -299,7 +296,10 @@ def planner_node(
|
|||||||
full_response = ""
|
full_response = ""
|
||||||
if AGENT_LLM_MAP["planner"] == "basic" and not configurable.enable_deep_thinking:
|
if AGENT_LLM_MAP["planner"] == "basic" and not configurable.enable_deep_thinking:
|
||||||
response = llm.invoke(messages)
|
response = llm.invoke(messages)
|
||||||
full_response = response.model_dump_json(indent=4, exclude_none=True)
|
if hasattr(response, "model_dump_json"):
|
||||||
|
full_response = response.model_dump_json(indent=4, exclude_none=True)
|
||||||
|
else:
|
||||||
|
full_response = get_message_content(response) or ""
|
||||||
else:
|
else:
|
||||||
response = llm.stream(messages)
|
response = llm.stream(messages)
|
||||||
for chunk in response:
|
for chunk in response:
|
||||||
@@ -307,6 +307,20 @@ def planner_node(
|
|||||||
logger.debug(f"Current state messages: {state['messages']}")
|
logger.debug(f"Current state messages: {state['messages']}")
|
||||||
logger.info(f"Planner response: {full_response}")
|
logger.info(f"Planner response: {full_response}")
|
||||||
|
|
||||||
|
# Validate explicitly that response content is valid JSON before proceeding to parse it
|
||||||
|
if not full_response.strip().startswith('{') and not full_response.strip().startswith('['):
|
||||||
|
logger.warning("Planner response does not appear to be valid JSON")
|
||||||
|
if plan_iterations > 0:
|
||||||
|
return Command(
|
||||||
|
update=preserve_state_meta_fields(state),
|
||||||
|
goto="reporter"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return Command(
|
||||||
|
update=preserve_state_meta_fields(state),
|
||||||
|
goto="__end__"
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
curr_plan = json.loads(repair_json_output(full_response))
|
curr_plan = json.loads(repair_json_output(full_response))
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
|
|||||||
Reference in New Issue
Block a user