Skip to main content
Open In ColabOpen on GitHub

OUTPUT_PARSING_FAILURE

期望的输出解析器(output parser)无法按预期方式处理模型输出。

为了说明这一点,假设您有一个输出解析器,它期望聊天模型将 JSON 格式的输出用 markdown 代码标签(三个反引号 ```)包围起来。以下是一个符合要求的输入示例:

from langchain_core.messages import AIMessage
from langchain_core.output_parsers import JsonOutputParser

message = AIMessage(content='\`\`\`\n{"foo": "bar"}\n\`\`\`')
output_parser = JsonOutputParser()
output_parser.invoke(message)
API Reference:AIMessage | JsonOutputParser
{'foo': 'bar'}

内部地,我们的 JSON 解析器会剥离 markdown 围栏和换行符,然后运行 json.loads

如果聊天模型生成了格式错误的 JSON 输出,我们将收到一个错误:

message = AIMessage(content='\`\`\`\n{{"foo":\n\`\`\`')
output_parser = JsonOutputParser()
output_parser.invoke(message)
---------------------------------------------------------------------------
``````output
JSONDecodeError Traceback (most recent call last)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/output_parsers/json.py:83, in JsonOutputParser.parse_result(self, result, partial)
82 try:
---> 83 return parse_json_markdown(text)
84 except JSONDecodeError as e:
``````output
File ~/langchain/oss-py/libs/core/langchain_core/utils/json.py:144, in parse_json_markdown(json_string, parser)
143 json_str = json_string if match is None else match.group(2)
--> 144 return _parse_json(json_str, parser=parser)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/utils/json.py:160, in _parse_json(json_str, parser)
159 # Parse the JSON string into a Python dictionary
--> 160 return parser(json_str)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/utils/json.py:118, in parse_partial_json(s, strict)
115 # If we got here, we ran out of characters to remove
116 # and still couldn't parse the string as JSON, so return the parse error
117 # for the original string.
--> 118 return json.loads(s, strict=strict)
``````output
File ~/.pyenv/versions/3.11.4/lib/python3.11/json/__init__.py:359, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
358 kw['parse_constant'] = parse_constant
--> 359 return cls(**kw).decode(s)
``````output
File ~/.pyenv/versions/3.11.4/lib/python3.11/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
333 """Return the Python representation of ``s`` (a ``str`` instance
334 containing a JSON document).
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
``````output
File ~/.pyenv/versions/3.11.4/lib/python3.11/json/decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
352 try:
--> 353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
``````output
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
``````output

The above exception was the direct cause of the following exception:
``````output
OutputParserException Traceback (most recent call last)
``````output
Cell In[9], line 3
1 message = AIMessage(content='```\n{{"foo":\n```')
2 output_parser = JsonOutputParser()
----> 3 output_parser.invoke(message)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/output_parsers/base.py:193, in BaseOutputParser.invoke(self, input, config, **kwargs)
186 def invoke(
187 self,
188 input: Union[str, BaseMessage],
189 config: Optional[RunnableConfig] = None,
190 **kwargs: Any,
191 ) -> T:
192 if isinstance(input, BaseMessage):
--> 193 return self._call_with_config(
194 lambda inner_input: self.parse_result(
195 [ChatGeneration(message=inner_input)]
196 ),
197 input,
198 config,
199 run_type="parser",
200 )
201 else:
202 return self._call_with_config(
203 lambda inner_input: self.parse_result([Generation(text=inner_input)]),
204 input,
205 config,
206 run_type="parser",
207 )
``````output
File ~/langchain/oss-py/libs/core/langchain_core/runnables/base.py:1927, in Runnable._call_with_config(self, func, input, config, run_type, serialized, **kwargs)
1923 context = copy_context()
1924 context.run(_set_config_context, child_config)
1925 output = cast(
1926 Output,
-> 1927 context.run(
1928 call_func_with_variable_args, # type: ignore[arg-type]
1929 func, # type: ignore[arg-type]
1930 input, # type: ignore[arg-type]
1931 config,
1932 run_manager,
1933 **kwargs,
1934 ),
1935 )
1936 except BaseException as e:
1937 run_manager.on_chain_error(e)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/runnables/config.py:396, in call_func_with_variable_args(func, input, config, run_manager, **kwargs)
394 if run_manager is not None and accepts_run_manager(func):
395 kwargs["run_manager"] = run_manager
--> 396 return func(input, **kwargs)
``````output
File ~/langchain/oss-py/libs/core/langchain_core/output_parsers/base.py:194, in BaseOutputParser.invoke.<locals>.<lambda>(inner_input)
186 def invoke(
187 self,
188 input: Union[str, BaseMessage],
189 config: Optional[RunnableConfig] = None,
190 **kwargs: Any,
191 ) -> T:
192 if isinstance(input, BaseMessage):
193 return self._call_with_config(
--> 194 lambda inner_input: self.parse_result(
195 [ChatGeneration(message=inner_input)]
196 ),
197 input,
198 config,
199 run_type="parser",
200 )
201 else:
202 return self._call_with_config(
203 lambda inner_input: self.parse_result([Generation(text=inner_input)]),
204 input,
205 config,
206 run_type="parser",
207 )
``````output
File ~/langchain/oss-py/libs/core/langchain_core/output_parsers/json.py:86, in JsonOutputParser.parse_result(self, result, partial)
84 except JSONDecodeError as e:
85 msg = f"Invalid json output: {text}"
---> 86 raise OutputParserException(msg, llm_output=text) from e
``````output
OutputParserException: Invalid json output: ```
{{"foo":

请注意,某些预构建的构造(如[旧版 LangChain 代理](/docs/how_to/agent_executor)和链)可能会在内部使用输出解析器,因此即使您没有显式实例化和使用输出解析器,也可能会看到此错误。

## 故障排除

以下方法可能有助于解决此错误:

- 考虑在可能的情况下使用[工具调用或其他结构化输出技术](/docs/how_to/structured_output/),而无需输出解析器来可靠地输出可解析的值。
- 如果您正在使用预构建的链或代理,请改用[LangGraph](https://langchain-ai.github.io/langgraph/)来显式组合您的逻辑。
- 为您的提示添加更精确的格式说明。在上面的示例中,将`"You must always return valid JSON fenced by a markdown code block. Do not return any additional text."`添加到您的输入中,可能有助于引导模型返回预期的格式。
- 如果您使用的是功能较小或功能较弱的模型,请尝试使用功能更强的模型。
- 添加[LLM 支持的重试机制](/docs/how_to/output_parser_fixing/)。