# json.decoder.JSONDecodeError: 在函数调用参数流中，第 1 行第 1023 列（字符 1022）处字符串未终止

- **ID:** `llm/function-call-arguments-truncated-in-stream`
- **领域:** llm
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

当流式传输函数调用时，参数作为 JSON 字符串发送，可能被分割到多个块中，导致过早解析时 JSON 不完整。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai-python>=1.0.0 | active | — | — |
| gpt-4-0613 | active | — | — |
| gpt-3.5-turbo-0613 | active | — | — |

## 解决方案

1. ```
   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`
   ```
2. ```
   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`.
   ```
3. ```
   Set `stream_options={"include_usage": True}` to get a final chunk with complete function call info, though this may not always include full arguments.
   ```

## 无效尝试

- **** — Max_tokens affects the total output length, not the chunking behavior; stream chunks are inherently arbitrary. (80% 失败率)
- **** — This works but defeats the purpose of streaming for user experience; also, it may not be feasible for long-running calls. (40% 失败率)
- **** — This is actually a valid approach but requires careful buffering; the dead end is when developers try to parse each chunk individually. (60% 失败率)
