llm data_error ai_generated true

OutputParserException: Parsing LLM output produced by 'PydanticOutputParser' failed. Error: 1 validation error for WeatherResponse temperature Input should be a valid number [type=float_type, input_value='72.5°F', input_type=str]

ID: llm/langchain-output-parser-schema-mismatch

Also available as: JSON · Markdown · 中文
78%Fix Rate
82%Confidence
1Evidence
2024-04-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
langchain 0.2.0 active
langchain-openai 0.1.0 active
pydantic 2.5.0 active
GPT-4 Turbo active
Claude 3 Opus active

Root Cause

LLM output contains extra non-numeric characters (e.g., units like '°F') that the Pydantic output parser cannot coerce into the expected numeric type.

generic

中文

LLM 输出包含额外的非数字字符(例如单位 '°F'),Pydantic 输出解析器无法将其强制转换为预期的数字类型。

Official Documentation

https://python.langchain.com/docs/modules/model_io/output_parsers/

Workarounds

  1. 85% success Add a post-processing step to strip units before parsing. In code: `raw_output = llm_output.replace('°F', '').replace('°C', ''); parsed = parser.parse(raw_output)`.
    Add a post-processing step to strip units before parsing. In code: `raw_output = llm_output.replace('°F', '').replace('°C', ''); parsed = parser.parse(raw_output)`.
  2. 80% success Use a custom prompt that explicitly forbids units, e.g., 'Return temperature as a plain number without units like °F or °C.'
    Use a custom prompt that explicitly forbids units, e.g., 'Return temperature as a plain number without units like °F or °C.'
  3. 75% success Switch to `JsonOutputParser` with a post-validation step using Pydantic, allowing more flexible parsing of numeric fields.
    Switch to `JsonOutputParser` with a post-validation step using Pydantic, allowing more flexible parsing of numeric fields.

中文步骤

  1. 在解析前添加后处理步骤以去除单位。代码示例:`raw_output = llm_output.replace('°F', '').replace('°C', ''); parsed = parser.parse(raw_output)`。
  2. 使用明确禁止单位的自定义提示,例如 '将温度作为纯数字返回,不要包含 °F 或 °C 等单位。'
  3. 切换到 `JsonOutputParser` 并结合 Pydantic 进行后验证,允许更灵活地解析数字字段。

Dead Ends

Common approaches that don't work:

  1. 70% fail

    The issue is not randomness; the LLM is following its training to include units. Temperature 0 still produces units in many cases.

  2. 90% fail

    The output is complete but includes extra text; max_tokens does not affect the content format.

  3. 60% fail

    LangChain's PydanticOutputParser does not use OpenAI's response_format internally; it parses arbitrary text output. The JSON mode may not be compatible with the parser's prompt template.