{
  "id": "llm/function-call-json-schema-violation-in-streaming",
  "signature": "InvalidRequestError: function_call arguments must be valid JSON — streaming mode detected malformed JSON",
  "signature_zh": "InvalidRequestError：function_call参数必须是有效的JSON — 流式模式检测到格式错误的JSON",
  "regex": "function_call arguments must be valid JSON|malformed JSON in stream|invalid json in function call",
  "domain": "llm",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "When streaming function calls, the LLM may emit incomplete or malformed JSON in intermediate chunks, causing the API to reject the request if validation is strict.",
  "root_cause_type": "generic",
  "root_cause_zh": "当流式传输函数调用时，LLM可能会在中间数据块中发出不完整或格式错误的JSON，如果验证严格，会导致API拒绝请求。",
  "versions": [
    {
      "version": "openai==1.14.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "anthropic==0.28.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "gpt-4-0613",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "claude-3-sonnet-20240229",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Disabling streaming entirely (stream=False) avoids the issue but defeats the purpose of real-time interaction.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Manually escaping JSON characters in the function schema doesn't help because the error is in the LLM output, not the schema.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Increasing temperature or top_p to force more varied output doesn't fix JSON structure issues.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Accumulate streaming chunks and parse JSON only after receiving the final chunk (e.g., `function_call_chunks = []; for chunk in stream: if chunk.choices[0].delta.function_call: function_call_chunks.append(chunk.choices[0].delta.function_call.arguments); full_json = ''.join(function_call_chunks); args = json.loads(full_json)`).",
      "success_rate": 0.9,
      "how": "Accumulate streaming chunks and parse JSON only after receiving the final chunk (e.g., `function_call_chunks = []; for chunk in stream: if chunk.choices[0].delta.function_call: function_call_chunks.append(chunk.choices[0].delta.function_call.arguments); full_json = ''.join(function_call_chunks); args = json.loads(full_json)`).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a JSON repair library like `json-repair` to fix malformed JSON from streaming chunks before validation.",
      "success_rate": 0.8,
      "how": "Use a JSON repair library like `json-repair` to fix malformed JSON from streaming chunks before validation.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "累积流式数据块，仅在接收完最后一个数据块后解析JSON（例如：`function_call_chunks = []; for chunk in stream: if chunk.choices[0].delta.function_call: function_call_chunks.append(chunk.choices[0].delta.function_call.arguments); full_json = ''.join(function_call_chunks); args = json.loads(full_json)`）。",
    "使用像`json-repair`这样的JSON修复库，在验证前修复来自流式数据块的格式错误的JSON。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://platform.openai.com/docs/guides/function-calling",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-02-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}