{
  "id": "llm/structured-output-enum-violation-in-json-mode",
  "signature": "LLM returns value 'medium' not in allowed enum ['low', 'high'] when using JSON mode with constrained decoding",
  "signature_zh": "LLM在使用JSON模式进行约束解码时，返回值'medium'不在允许的枚举['low', 'high']中",
  "regex": "not in allowed enum|enum value not valid|JSON schema enum violation",
  "domain": "llm",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "JSON mode or constrained decoding (e.g., using functions or response_format) does not enforce enum constraints on string values, allowing the LLM to output values outside the specified set.",
  "root_cause_type": "generic",
  "root_cause_zh": "JSON模式或约束解码（例如，使用函数或response_format）不对字符串值强制执行枚举约束，允许LLM输出指定集合之外的值。",
  "versions": [
    {
      "version": "openai==1.18.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "anthropic==0.32.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "gpt-4o-2024-05-13",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "claude-3-haiku-20240307",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "outlines==0.0.34",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "lmql==0.4.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Adding more examples to the prompt with correct enum values doesn't guarantee the LLM will follow them, especially for edge cases.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using a stricter JSON schema with additionalProperties: false doesn't prevent enum violations because the schema doesn't enforce value constraints at generation time.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Lowering temperature to 0 doesn't fix enum violations because the model may still sample from improbable tokens.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Post-process the LLM output to validate enum values and either reject or map invalid values to a default (e.g., `if value not in ['low', 'high']: value = 'low'`).",
      "success_rate": 0.95,
      "how": "Post-process the LLM output to validate enum values and either reject or map invalid values to a default (e.g., `if value not in ['low', 'high']: value = 'low'`).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a constrained decoding library like `outlines` or `lmql` that forces the LLM to output only tokens matching the regex or grammar of the enum.",
      "success_rate": 0.9,
      "how": "Use a constrained decoding library like `outlines` or `lmql` that forces the LLM to output only tokens matching the regex or grammar of the enum.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "后处理LLM输出以验证枚举值，并拒绝无效值或将其映射到默认值（例如：`if value not in ['low', 'high']: value = 'low'`）。",
    "使用像`outlines`或`lmql`这样的约束解码库，强制LLM仅输出与枚举的正则表达式或语法匹配的令牌。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://platform.openai.com/docs/guides/structured-outputs",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.92,
  "resolvable": "true",
  "first_seen": "2024-05-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}