chat.completions.stream() crashes with ValueError: EOF while parsing a value on leading whitespace in strict function tool call arguments

Open Beginner friendly
#4,308 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

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
  1. Create a ChatCompletionStreamState (or client.chat.completions.stream()) with a strict tool (e.g., pydantic_function_tool(GetWeather)).
  2. Feed an initial role chunk followed by a tool call delta whose arguments starts with " " or "\n".
  3. 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

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-python

All issues in openai/openai-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.