chat.completions.stream() crashes with ValueError: EOF while parsing a value on leading whitespace in strict function tool call arguments
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 1/5
- Tiempo estimado
- Menos de una hora
- Aptitud para principiantes
- 82/100
Línea de trabajo
La corrección está en src/openai/lib/streaming/chat/_completions.py, en ChatCompletionStreamState._accumulate_chunk(). La rama de contenido del mensaje ya llama a .lstrip() antes de from_json(..., partial_mode=True), pero la rama de llamadas a herramientas en modo estricto (alrededor de las líneas 454-462) no lo hace. Empieza ejecutando la reproducción del issue para ver el ValueError. Está terminado cuando un argumento de llamada a herramienta en modo estricto con espacios al inicio ya no lanza error y los argumentos analizados aparecen una vez que llegan los fragmentos JSON.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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)
- Lenguaje dominante
- Python
- Estrellas
- 32.2k
- Forks
- 5.8k
- Merge medio
- 12 h 29 min
- PR fusionados (30 d)
- 112
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de openai/openai-python
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
openai/openai-python#4338 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
openai/openai-python#4337 ·
Los mantenedores suelen responder en 1 día
-
`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parametersPosiblemente ocupada @Rewaskc02-lang la tomó hace 1 día. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
openai/openai-python#4311 · 4 comentarios ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
openai/openai-python#4310 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
openai/openai-python#4309 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de openai/openai-python
Issues similares
-
ui
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Los mantenedores suelen responder en 1 día
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPosiblemente ocupada @ronaldtse la tomó hoy. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 95/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
pydantic/pytest-examples#102 · 1 comentario ·