Skip to content

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

Open

Description

@0718lol

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)

Activity

  1. Shanmukh-l33t commented on Nov 23, 2026

    @Shanmukh-l33t

    What breaks

    A streamed strict function tool call whose first arguments chunk is whitespace (a lone " " or "\n") kills the whole stream with ValueError: 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 checking choice_snapshot.message.content.lstrip() (line 439, the guard added for #2082). The sibling call for strict tool arguments, lines 454 to 462, checks only that arguments is non-empty and then calls from_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:

    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()
    ):

    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_whitespace to tests/lib/chat/test_completions_streaming.py. It creates a ChatCompletionStreamState with a strict pydantic tool, feeds a role chunk, then a tool call delta with arguments: " ", then the rest of the JSON, and asserts parsed_arguments == GetWeatherArgs(city="Paris").

    RED on unmodified f63f14b, 1 failed in 1.16s:

    src/openai/lib/streaming/chat/_completions.py:459: in _accumulate_chunk
        tool_call_snapshot.function.parsed_arguments = from_json(
    E   ValueError: EOF while parsing a value at line 1 column 1
    

    GREEN with the fix, 1 passed in 1.12s.

    Wider runs: tests/lib/chat/ tests/lib/streaming/ tests/lib/test_streaming_deltas.py gives 475 passed, 14 skipped, and tests/test_streaming.py tests/lib/test_streaming_deltas.py gives 79 passed, 34 skipped. ruff check, ruff format in check mode, and ty check all 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions