chat.completions.stream() crashes with ValueError: EOF while parsing a value on leading whitespace in strict function tool call arguments
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 82/100
Research direction
The fix is in src/openai/lib/streaming/chat/_completions.py, in ChatCompletionStreamState._accumulate_chunk(). The message-content branch already calls .lstrip() before from_json(..., partial_mode=True), but the strict tool-call branch (around lines 454-462) does not. Start by running the reproduction from the issue to see the ValueError. Done when a whitespace-prefixed strict tool-call argument no longer raises and the parsed arguments appear once the JSON chunks arrive.
Written by the indexing model from the issue text.
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
In src/openai/lib/streaming/chat/_completions.py, ChatCompletionStreamState._accumulate_chunk() already guards partial JSON parsing of message.content against whitespace-only prefixes (added for #2082):
if (
choice_snapshot.message.content
and not choice_snapshot.message.refusal
and is_given(self._rich_response_format)
# partial parsing fails on white-space
and choice_snapshot.message.content.lstrip()
):
choice_snapshot.message.parsed = from_json(
bytes(choice_snapshot.message.content, "utf-8"),
partial_mode=True,
)
However, 18 lines below (lines 454-462), the sibling from_json(..., partial_mode=True) call for strict function tool call arguments omits .lstrip():
if (
input_tool
and input_tool.get("function", {}).get("strict")
and tool_call_snapshot.function.arguments
):
tool_call_snapshot.function.parsed_arguments = from_json(
bytes(tool_call_snapshot.function.arguments, "utf-8"),
partial_mode=True,
)
If a streamed strict tool call begins with a whitespace chunk (such as " " or "\n"), jiter.from_json(b" ", partial_mode=True) immediately raises ValueError: EOF while parsing a value at line 1 column 1, aborting the stream before the JSON payload chunks arrive.
To Reproduce
- Create a
ChatCompletionStreamState(orclient.chat.completions.stream()) with a strict tool (e.g.,pydantic_function_tool(GetWeather)). - Feed an initial role chunk followed by a tool call delta whose
argumentsstarts with" "or"\n". - Observe
ValueError: EOF while parsing a value at line 1 column 1.
Code snippets
from pydantic import BaseModel
from openai import pydantic_function_tool
from openai.lib.streaming.chat import ChatCompletionStreamState
from openai.types.chat import ChatCompletionChunk
class GetWeather(BaseModel):
city: str
state = ChatCompletionStreamState(input_tools=[pydantic_function_tool(GetWeather)])
state.handle_chunk(
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-1",
"object": "chat.completion.chunk",
"created": 1,
"model": "gpt-4o",
"choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}],
}
)
)
state.handle_chunk(
ChatCompletionChunk.model_validate(
{
"id": "chatcmpl-1",
"object": "chat.completion.chunk",
"created": 1,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"delta": {
"tool_calls": [
{
"index": 0,
"id": "call_1",
"type": "function",
"function": {"name": "GetWeather", "arguments": " "},
}
]
},
"finish_reason": None,
}
],
}
)
)
# ValueError: EOF while parsing a value at line 1 column 1
Suggested fix in src/openai/lib/streaming/chat/_completions.py:
if (
input_tool
and input_tool.get("function", {}).get("strict")
and tool_call_snapshot.function.arguments
+ # partial parsing fails on white-space
+ and tool_call_snapshot.function.arguments.lstrip()
):
OS
macOS
Python version
Python 3.14
Library version
openai v3.57.0 (main@f63f14b)
- Dominant language
- Python
- Stars
- 32.2k
- Forks
- 5.8k
- Avg merge
- 12h 29m
- Merged PRs (30d)
- 112
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openai/openai-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
openai/openai-python#4338 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
openai/openai-python#4337 ·
Maintainers usually reply within 1 day
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersPossibly taken @Rewaskc02-lang claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openai/openai-python#4311 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openai/openai-python#4310 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
openai/openai-python#4309 · 1 comment ·
Maintainers usually reply within 1 day
All issues in openai/openai-python
Similar issues
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainers usually reply within 1 day
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPossibly taken @ronaldtse claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pydantic/pytest-examples#102 · 1 comment ·