# InvalidRequestError：function_call参数必须是有效的JSON — 流式模式检测到格式错误的JSON

- **ID:** `llm/function-call-json-schema-violation-in-streaming`
- **领域:** llm
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

当流式传输函数调用时，LLM可能会在中间数据块中发出不完整或格式错误的JSON，如果验证严格，会导致API拒绝请求。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.14.0 | active | — | — |
| anthropic==0.28.0 | active | — | — |
| gpt-4-0613 | active | — | — |
| claude-3-sonnet-20240229 | active | — | — |

## 解决方案

1. ```
   累积流式数据块，仅在接收完最后一个数据块后解析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)`）。
   ```
2. ```
   使用像`json-repair`这样的JSON修复库，在验证前修复来自流式数据块的格式错误的JSON。
   ```

## 无效尝试

- **** — Disabling streaming entirely (stream=False) avoids the issue but defeats the purpose of real-time interaction. (70% 失败率)
- **** — Manually escaping JSON characters in the function schema doesn't help because the error is in the LLM output, not the schema. (90% 失败率)
- **** — Increasing temperature or top_p to force more varied output doesn't fix JSON structure issues. (80% 失败率)
