# InvalidRequestError: function_call arguments must be valid JSON — streaming mode detected malformed JSON

- **ID:** `llm/function-call-json-schema-violation-in-streaming`
- **Domain:** llm
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai==1.14.0 | active | — | — |
| anthropic==0.28.0 | active | — | — |
| gpt-4-0613 | active | — | — |
| claude-3-sonnet-20240229 | active | — | — |

## Workarounds

1. **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)`).** (90% success)
   ```
   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)`).
   ```
2. **Use a JSON repair library like `json-repair` to fix malformed JSON from streaming chunks before validation.** (80% success)
   ```
   Use a JSON repair library like `json-repair` to fix malformed JSON from streaming chunks before validation.
   ```

## Dead Ends

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