# json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1023 (char 1022) in function call arguments stream

- **ID:** `llm/function-call-arguments-truncated-in-stream`
- **Domain:** llm
- **Category:** encoding_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

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

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai-python>=1.0.0 | active | — | — |
| gpt-4-0613 | active | — | — |
| gpt-3.5-turbo-0613 | active | — | — |

## Workarounds

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`** (85% success)
   ```
   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`.** (90% success)
   ```
   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.** (70% success)
   ```
   Set `stream_options={"include_usage": True}` to get a final chunk with complete function call info, though this may not always include full arguments.
   ```

## Dead Ends

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