Repository navigation
Open
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 1Suggested 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)
Metadata
Metadata
Assignees
Labels
No labels
Activity
Shanmukh-l33t commentedon Nov 23, 2026
What breaks
A streamed strict function tool call whose first arguments chunk is whitespace (a lone
" "or"\n") kills the whole stream withValueError: EOF while parsing a value at line 1 column 1. The chunk that carries the actual JSON never gets handled, so instead of a parsed tool call the caller gets an exception. I reproduced this on main at f63f14b, the commit this report was filed against.Root cause
In
src/openai/lib/streaming/chat/_completions.py,ChatCompletionStreamState._accumulate_chunk()only partial parses message content after checkingchoice_snapshot.message.content.lstrip()(line 439, the guard added for #2082). The sibling call for strict tool arguments, lines 454 to 462, checks only thatargumentsis non-empty and then callsfrom_json(..., partial_mode=True). The JSON parser cannot handle a whitespace only buffer even in partial mode, so it raises before any real data arrives.Fix
One guard, with the same shape and the same comment as the content check, at line 459:
Parsing then resumes normally on the next chunk that carries real characters. Nothing else in the parser changed.
Test evidence
I added
test_strict_tool_call_arguments_with_leading_whitespacetotests/lib/chat/test_completions_streaming.py. It creates aChatCompletionStreamStatewith a strict pydantic tool, feeds a role chunk, then a tool call delta witharguments: " ", then the rest of the JSON, and assertsparsed_arguments == GetWeatherArgs(city="Paris").RED on unmodified f63f14b,
1 failed in 1.16s:GREEN with the fix,
1 passed in 1.12s.Wider runs:
tests/lib/chat/ tests/lib/streaming/ tests/lib/test_streaming_deltas.pygives475 passed, 14 skipped, andtests/test_streaming.py tests/lib/test_streaming_deltas.pygives79 passed, 34 skipped.ruff check, ruff format in check mode, andty checkall pass on the changed files.tests/api_resources/chat/needs the local mock server on 127.0.0.1 port 4010, which is not running in my environment, so every test in it fails on connection refused for that reason and not because of this change.The branch is here if it is useful, and I am glad to reshape it however you prefer:
main...Shanmukh-l33t:openai-python:fix/4308-strict-tool-args-whitespace
I did not open a pull request because PRs in this repo are limited to collaborators.
Written with AI assistance.