解决基于LangGraph框架的DeerFlow使用Qwen3不能正常被解析的问题
修改 langchain_core/output_parsers/json.py
文件里的 class JsonOutputParser()
类,在其中的 parse_result()
方法中添加一个字符串替换:
def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:"""Parse the result of an LLM call to a JSON object.Args:result: The result of the LLM call.partial: Whether to parse partial JSON objects.If True, the output will be a JSON object containingall the keys that have been returned so far.If False, the output will be the full JSON object.Default is False.Returns:The parsed JSON object.Raises:OutputParserException: If the output is not valid JSON."""text = result[0].text# 解决Qwen3固定输出<think>导致不能解析Json格式的问题if '<think>' in text:text = text[text.find('</think>') + len('</think>'):]text = text.strip()if partial:try:return parse_json_markdown(text)except JSONDecodeError:return Noneelse:try:return parse_json_markdown(text)except JSONDecodeError as e:msg = f"Invalid json output: {text}"raise OutputParserException(msg, llm_output=text) from e