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:
Zts0hg
2025-11-21 09:41:34 +08:00
committed by GitHub
parent 164ef5b8be
commit 2d1a0997eb

View File

@@ -282,10 +282,7 @@ def planner_node(
if configurable.enable_deep_thinking:
llm = get_llm_by_type("reasoning")
elif AGENT_LLM_MAP["planner"] == "basic":
llm = get_llm_by_type("basic").with_structured_output(
Plan,
method="json_mode",
)
llm = get_llm_by_type("basic")
else:
llm = get_llm_by_type(AGENT_LLM_MAP["planner"])
@@ -299,7 +296,10 @@ def planner_node(
full_response = ""
if AGENT_LLM_MAP["planner"] == "basic" and not configurable.enable_deep_thinking:
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:
response = llm.stream(messages)
for chunk in response:
@@ -307,6 +307,20 @@ def planner_node(
logger.debug(f"Current state messages: {state['messages']}")
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:
curr_plan = json.loads(repair_json_output(full_response))
except json.JSONDecodeError: