fix(api): preserve nullable stream compatibility
This commit is contained in:
1 parent
12c2c95cbb
commit
b6b7caafd3
2 files changed
+27
-5
No files matched your search
@@ -316,9 +316,9 @@ async def handle_chat_completions(request: web.Request) -> web.Response | web.St
|
||||
return _error_json(400, "Invalid JSON body")
|
||||
body = cast(dict[str, Any], body)
|
||||
stream_value = body.get("stream", False)
|
||||
if not isinstance(stream_value, bool):
|
||||
if stream_value is not None and not isinstance(stream_value, bool):
|
||||
return _error_json(400, "stream must be a boolean")
|
||||
stream = stream_value
|
||||
stream = stream_value is True
|
||||
requested_model = body.get("model")
|
||||
text, media_paths = _parse_json_content(body)
|
||||
session_id = body.get("session_id")
|
||||
|
||||
@@ -173,8 +173,9 @@ async def test_stream_default_is_false(aiohttp_client) -> None:
|
||||
|
||||
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None:
|
||||
"""The OpenAI-compatible stream flag must be a JSON boolean."""
|
||||
@pytest.mark.parametrize("stream", ["false", "true", 0, 1, [], {}])
|
||||
async def test_stream_rejects_non_boolean_values(aiohttp_client, stream) -> None:
|
||||
"""Wrong JSON types must not select a response protocol or invoke the agent."""
|
||||
agent = MagicMock()
|
||||
agent.process_direct = AsyncMock(return_value="normal reply")
|
||||
agent.aclose = AsyncMock()
|
||||
@@ -185,7 +186,7 @@ async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None:
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": "false"},
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": stream},
|
||||
)
|
||||
|
||||
assert resp.status == 400
|
||||
@@ -194,6 +195,27 @@ async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None:
|
||||
agent.process_direct.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_null_preserves_non_streaming_compatibility(aiohttp_client) -> None:
|
||||
"""OpenAI clients may explicitly serialize the optional flag as null."""
|
||||
agent = MagicMock()
|
||||
agent.process_direct = AsyncMock(return_value="normal reply")
|
||||
agent.aclose = AsyncMock()
|
||||
client = await aiohttp_client(create_app(agent, model_name="m", api_key=API_KEY))
|
||||
|
||||
resp = await client.post(
|
||||
"/v1/chat/completions",
|
||||
headers=AUTH_HEADERS,
|
||||
json={"messages": [{"role": "user", "content": "hi"}], "stream": None},
|
||||
)
|
||||
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == "application/json"
|
||||
assert (await resp.json())["choices"][0]["message"]["content"] == "normal reply"
|
||||
agent.process_direct.assert_awaited_once()
|
||||
|
||||
|
||||
@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")
|
||||
@pytest.mark.asyncio
|
||||
async def test_stream_sse_chunk_ids_are_consistent(aiohttp_client) -> None:
|
||||
|
||||
Reference in new issue
Block a user