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

オープン 初心者向け
#4,308 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
1/5
見積もり時間
1時間未満
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
api

調査の方向性

修正箇所は src/openai/lib/streaming/chat/_completions.py の ChatCompletionStreamState._accumulate_chunk() です。メッセージ内容のブランチでは from_json(..., partial_mode=True) の前に .lstrip() が既に呼ばれていますが、strict なツール呼び出しのブランチ(454〜462行目付近)では呼ばれていません。まず issue の再現手順を実行して ValueError を確認してください。先頭に空白がある strict ツール呼び出しの引数でエラーが発生しなくなり、JSON チャンクが届いた時点で解析済みの引数が表示されれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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)

主要言語
Python
スター
32.2k
フォーク
5.8k
平均マージ
12時間 29分
マージ済み PR(30日)
112

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

openai/openai-python のほかの issue

openai/openai-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。