{
  "id": "llm/function-call-arguments-truncated-in-stream",
  "signature": "json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1023 (char 1022) in function call arguments stream",
  "signature_zh": "json.decoder.JSONDecodeError: 在函数调用参数流中，第 1 行第 1023 列（字符 1022）处字符串未终止",
  "regex": "Unterminated string starting at: line \\d+ column \\d+ \\(char \\d+\\)",
  "domain": "llm",
  "category": "encoding_error",
  "subcategory": null,
  "root_cause": "When streaming function calls, the arguments are sent as a JSON string that may be split across multiple chunks, causing incomplete JSON when parsed prematurely.",
  "root_cause_type": "generic",
  "root_cause_zh": "当流式传输函数调用时，参数作为 JSON 字符串发送，可能被分割到多个块中，导致过早解析时 JSON 不完整。",
  "versions": [
    {
      "version": "openai-python>=1.0.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": "gpt-3.5-turbo-0613",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Max_tokens affects the total output length, not the chunking behavior; stream chunks are inherently arbitrary.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This works but defeats the purpose of streaming for user experience; also, it may not be feasible for long-running calls.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This is actually a valid approach but requires careful buffering; the dead end is when developers try to parse each chunk individually.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Buffer all function call arguments chunks until a complete JSON can be parsed. Example: `buffer = \"\"; for chunk in response: if chunk.choices[0].delta.function_call.arguments: buffer += chunk.choices[0].delta.function_call.arguments; try: args = json.loads(buffer); break; except JSONDecodeError: continue`",
      "success_rate": 0.85,
      "how": "Buffer all function call arguments chunks until a complete JSON can be parsed. Example: `buffer = \"\"; for chunk in response: if chunk.choices[0].delta.function_call.arguments: buffer += chunk.choices[0].delta.function_call.arguments; try: args = json.loads(buffer); break; except JSONDecodeError: continue`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use the OpenAI library's built-in function call handling which automatically accumulates arguments: `tool_calls = chunk.choices[0].delta.tool_calls` and use `accumulated_arguments[tool_call_index] += chunk.arguments`.",
      "success_rate": 0.9,
      "how": "Use the OpenAI library's built-in function call handling which automatically accumulates arguments: `tool_calls = chunk.choices[0].delta.tool_calls` and use `accumulated_arguments[tool_call_index] += chunk.arguments`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Set `stream_options={\"include_usage\": True}` to get a final chunk with complete function call info, though this may not always include full arguments.",
      "success_rate": 0.7,
      "how": "Set `stream_options={\"include_usage\": True}` to get a final chunk with complete function call info, though this may not always include full arguments.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Buffer all function call arguments chunks until a complete JSON can be parsed. Example: `buffer = \"\"; for chunk in response: if chunk.choices[0].delta.function_call.arguments: buffer += chunk.choices[0].delta.function_call.arguments; try: args = json.loads(buffer); break; except JSONDecodeError: continue`",
    "Use the OpenAI library's built-in function call handling which automatically accumulates arguments: `tool_calls = chunk.choices[0].delta.tool_calls` and use `accumulated_arguments[tool_call_index] += chunk.arguments`.",
    "Set `stream_options={\"include_usage\": True}` to get a final chunk with complete function call info, though this may not always include full arguments."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://platform.openai.com/docs/guides/function-calling/streaming-function-calls",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-01-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}